Added option to require validate captive portal connection

This commit is contained in:
M66B 2022-09-01 08:45:52 +02:00
parent d1c53c81b6
commit a782fa3598
4 changed files with 31 additions and 4 deletions

View File

@ -73,7 +73,7 @@ public class ConnectionHelper {
static final int MAX_REDIRECTS = 5; // https://www.freesoft.org/CIE/RFC/1945/46.htm
static final List<String> PREF_NETWORK = Collections.unmodifiableList(Arrays.asList(
"metered", "roaming", "rlah", "require_validated", "vpn_only" // update network state
"metered", "roaming", "rlah", "require_validated", "require_validated_captive", "vpn_only" // update network state
));
// Roam like at home
@ -252,6 +252,7 @@ public class ConnectionHelper {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean standalone_vpn = prefs.getBoolean("standalone_vpn", false);
boolean require_validated = prefs.getBoolean("require_validated", false);
boolean require_validated_captive = prefs.getBoolean("require_validated_captive", true);
boolean vpn_only = prefs.getBoolean("vpn_only", false);
ConnectivityManager cm = Helper.getSystemService(context, ConnectivityManager.class);
@ -300,7 +301,7 @@ public class ConnectionHelper {
return null;
}
boolean captive = caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL);
if ((require_validated || captive) &&
if ((require_validated || (require_validated_captive && captive)) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
Log.i("isMetered: not validated captive=" + captive);

View File

@ -69,6 +69,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
private SwitchCompat swDownloadEml;
private SwitchCompat swDownloadPlain;
private SwitchCompat swValidated;
private SwitchCompat swValidatedCaptive;
private SwitchCompat swVpnOnly;
private EditText etTimeout;
private SwitchCompat swPreferIp4;
@ -92,7 +93,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
private final static String[] RESET_OPTIONS = new String[]{
"metered", "download", "roaming", "rlah",
"download_headers", "download_eml", "download_plain",
"require_validated", "vpn_only",
"require_validated", "require_validated_captive", "vpn_only",
"timeout", "prefer_ip4", "bind_socket", "standalone_vpn", "tcp_keep_alive",
"ssl_harden", "ssl_harden_strict", "cert_strict", "open_safe"
};
@ -116,6 +117,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
swDownloadEml = view.findViewById(R.id.swDownloadEml);
swDownloadPlain = view.findViewById(R.id.swDownloadPlain);
swValidated = view.findViewById(R.id.swValidated);
swValidatedCaptive = view.findViewById(R.id.swValidatedCaptive);
swVpnOnly = view.findViewById(R.id.swVpnOnly);
etTimeout = view.findViewById(R.id.etTimeout);
swPreferIp4 = view.findViewById(R.id.swPreferIp4);
@ -212,6 +214,14 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("require_validated", checked).apply();
swValidatedCaptive.setEnabled(!checked);
}
});
swValidatedCaptive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("require_validated_captive", checked).apply();
}
});
@ -436,6 +446,8 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
swDownloadPlain.setChecked(prefs.getBoolean("download_plain", false));
swValidated.setChecked(prefs.getBoolean("require_validated", false));
swValidatedCaptive.setChecked(prefs.getBoolean("require_validated_captive", true));
swValidatedCaptive.setEnabled(!swValidated.isChecked());
swVpnOnly.setChecked(prefs.getBoolean("vpn_only", false));
int timeout = prefs.getInt("timeout", 0);

View File

@ -301,10 +301,23 @@
app:layout_constraintTop_toBottomOf="@id/tvPlainHint"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swValidatedCaptive"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_validated_captive"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swValidated"
app:switchPadding="12dp" />
<TextView
android:id="@+id/tvValidatedHint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginEnd="48dp"
android:text="@string/title_advanced_validate_hint"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
@ -312,7 +325,7 @@
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swValidated" />
app:layout_constraintTop_toBottomOf="@id/swValidatedCaptive" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swVpnOnly"

View File

@ -468,6 +468,7 @@
<string name="title_advanced_download_eml">Download raw message files</string>
<string name="title_advanced_download_plain">Download plain text only parts by default (if available)</string>
<string name="title_advanced_validated">Require a validated (checked) connection</string>
<string name="title_advanced_validated_captive">Require a validated captive portal connection</string>
<string name="title_advanced_vpn_only">Connect only via a VPN</string>
<string name="title_advanced_timeout">Connection timeout (seconds)</string>
<string name="title_advanced_prefer_ip4">Prefer IPv4 over IPv6</string>