mirror of https://github.com/M66B/FairEmail.git
Check for connectivity changes
This commit is contained in:
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?
|
|
||||||
boolean transition = false;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
EntityLog.log(ServiceSynchronize.this,
|
|
||||||
"Available network=" + network + " info=" + ni + " caps=" + caps + " transition=" + transition);
|
|
||||||
|
|
||||||
if (transition)
|
|
||||||
reload(ServiceSynchronize.this, -1L, false, "unmetered");
|
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
Log.w(ex);
|
Log.w(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateState(network, null);
|
updateState(network, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities capabilities) {
|
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities caps) {
|
||||||
updateState(network, capabilities);
|
updateState(network, caps);
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
lastActiveCaps = caps;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
Log.e(ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties properties) {
|
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties props) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
||||||
try {
|
try {
|
||||||
// Monitor IP v4/v6 availability
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
boolean has4 = false;
|
Network active = cm.getActiveNetwork();
|
||||||
boolean has6 = false;
|
if (active != null && active.equals(network)) {
|
||||||
String name = properties.getInterfaceName();
|
boolean ahas4 = false;
|
||||||
NetworkInterface ni = NetworkInterface.getByName(name);
|
boolean ahas6 = false;
|
||||||
if (ni != null)
|
boolean lhas4 = false;
|
||||||
for (InterfaceAddress iaddr : ni.getInterfaceAddresses()) {
|
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();
|
InetAddress addr = iaddr.getAddress();
|
||||||
if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress())
|
if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress())
|
||||||
if (addr instanceof Inet4Address)
|
if (addr instanceof Inet4Address)
|
||||||
has4 = true;
|
ahas4 = true;
|
||||||
else if (addr instanceof Inet6Address)
|
else if (addr instanceof Inet6Address)
|
||||||
has6 = true;
|
ahas6 = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityLog.log(ServiceSynchronize.this,
|
for (InterfaceAddress iaddr : lintf.getInterfaceAddresses()) {
|
||||||
"IP intf=" + name + "/" + ni.getDisplayName() + " v4=" + last4 + "/" + has4 + " v6=" + last6 + "/" + has6);
|
InetAddress addr = iaddr.getAddress();
|
||||||
if ((last4 != null && last4 && !has4) || (last6 != null && last6 && !has6))
|
if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress())
|
||||||
reload(ServiceSynchronize.this, -1L, false, "IP");
|
if (addr instanceof Inet4Address)
|
||||||
|
lhas4 = true;
|
||||||
|
else if (addr instanceof Inet6Address)
|
||||||
|
lhas6 = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean reload = ((ahas4 && !lhas4) || (ahas6 && !lhas6));
|
||||||
|
|
||||||
|
if (reload || BuildConfig.DEBUG)
|
||||||
|
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…
Reference in New Issue