Explicitly reload on active network changes

This commit is contained in:
M66B 2023-10-04 07:54:40 +02:00
parent 1bbecfd223
commit eaea05832e
1 changed files with 19 additions and 4 deletions

View File

@ -104,6 +104,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
@ -2599,6 +2600,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
ConnectivityManager.NetworkCallback nc = new ConnectivityManager.NetworkCallback() {
private Network last_active = null;
private Boolean last_connected = null;
private Boolean last_unmetered = null;
private String last_generation = null;
@ -2633,20 +2635,32 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
Log.i(TAG, "Changed capabilities=" + network + " caps=" + networkCapabilities);
Network active = (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? null : cm.getActiveNetwork());
boolean connected = Util.isConnected(ServiceSinkhole.this);
boolean unmetered = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
String generation = Util.getNetworkGeneration(ServiceSinkhole.this);
Log.i(TAG, "Connected=" + connected + "/" + last_connected +
Log.i(TAG, "Active=" + network +
" connected=" + connected + "/" + last_connected +
" unmetered=" + unmetered + "/" + last_unmetered +
" generation=" + generation + "/" + last_generation);
if (last_connected != null && !last_connected.equals(connected))
boolean reloading = false;
if (!Objects.equals(active, last_active)) {
reloading = true;
reload("Active network changed", ServiceSinkhole.this, false);
}
if (last_connected != null && !last_connected.equals(connected) && !reloading) {
reloading = true;
reload("Connected state changed", ServiceSinkhole.this, false);
}
if (last_unmetered != null && !last_unmetered.equals(unmetered))
if (last_unmetered != null && !last_unmetered.equals(unmetered) && !reloading) {
reloading = true;
reload("Unmetered state changed", ServiceSinkhole.this, false);
}
if (last_generation != null && !last_generation.equals(generation)) {
if (last_generation != null && !last_generation.equals(generation) && !reloading) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSinkhole.this);
if (prefs.getBoolean("unmetered_2g", false) ||
prefs.getBoolean("unmetered_3g", false) ||
@ -2654,6 +2668,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
reload("Generation changed", ServiceSinkhole.this, false);
}
last_active = active;
last_connected = connected;
last_unmetered = unmetered;
last_generation = generation;