From 2ce18467858f429c8528c5af0de49c91ea45b318 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Thu, 23 Apr 2020 12:33:07 +0200 Subject: Warn if default VPN is not enabled when enabling on boot connect Closes #1110 --- main/src/main/res/values/strings.xml | 1 + .../blinkt/openvpn/fragments/GeneralSettings.java | 156 +++++++++++---------- 2 files changed, 85 insertions(+), 72 deletions(-) (limited to 'main/src') diff --git a/main/src/main/res/values/strings.xml b/main/src/main/res/values/strings.xml index 59a443d5..ca7cd0bf 100755 --- a/main/src/main/res/values/strings.xml +++ b/main/src/main/res/values/strings.xml @@ -496,5 +496,6 @@ AS servername Request autologin profile Import Profile from Access Server + Default VPN not set. Please set the Default VPN before enabling this option. diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.java b/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.java index 1d8b5f77..35179f87 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.java +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.java @@ -18,6 +18,7 @@ import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; import android.os.Bundle; +import android.widget.Toast; import androidx.preference.CheckBoxPreference; @@ -91,96 +92,107 @@ public class GeneralSettings extends PreferenceFragmentCompat implements Prefere ovpn3.setChecked(false); } + ((CheckBoxPreference)findPreference("restartvpnonboot")).setOnPreferenceChangeListener((pref, newValue) -> { + if (newValue.equals(true)) { + VpnProfile vpn = ProfileManager.getAlwaysOnVPN(requireActivity()); + if (vpn == null) { + Toast.makeText(requireContext(), R.string.no_default_vpn_set, Toast.LENGTH_LONG).show(); + return false; + } - setClearApiSummary(); - } + } + return true; + }); + setClearApiSummary(); + } - @Override - public void onResume() { - super.onResume(); + @Override + public void onResume () { + super.onResume(); - VpnProfile vpn = ProfileManager.getAlwaysOnVPN(getActivity()); - StringBuffer sb = new StringBuffer(getString(R.string.defaultvpnsummary)); - sb.append('\n'); - if (vpn == null) - sb.append(getString(R.string.novpn_selected)); - else - sb.append(getString(R.string.vpnselected, vpn.getName())); - mAlwaysOnVPN.setSummary(sb.toString()); - } + VpnProfile vpn = ProfileManager.getAlwaysOnVPN(getActivity()); + StringBuffer sb = new StringBuffer(getString(R.string.defaultvpnsummary)); + sb.append('\n'); + if (vpn == null) + sb.append(getString(R.string.novpn_selected)); + else + sb.append(getString(R.string.vpnselected, vpn.getName())); + mAlwaysOnVPN.setSummary(sb.toString()); - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (preference == mAlwaysOnVPN) { - VpnProfile vpn = ProfileManager.get(getActivity(), (String) newValue); - mAlwaysOnVPN.setSummary(vpn.getName()); } - return true; - } - private void setClearApiSummary() { - Preference clearapi = findPreference("clearapi"); - - if (mExtapp.getExtAppList().isEmpty()) { - clearapi.setEnabled(false); - clearapi.setSummary(R.string.no_external_app_allowed); - } else { - clearapi.setEnabled(true); - clearapi.setSummary(getString(R.string.allowed_apps, getExtAppList(", "))); + @Override + public boolean onPreferenceChange (Preference preference, Object newValue){ + if (preference == mAlwaysOnVPN) { + VpnProfile vpn = ProfileManager.get(getActivity(), (String) newValue); + mAlwaysOnVPN.setSummary(vpn.getName()); + } + return true; } - } - - private String getExtAppList(String delim) { - ApplicationInfo app; - PackageManager pm = getActivity().getPackageManager(); - - StringBuilder applist = new StringBuilder(); - for (String packagename : mExtapp.getExtAppList()) { - try { - app = pm.getApplicationInfo(packagename, 0); - if (applist.length() != 0) - applist.append(delim); - applist.append(app.loadLabel(pm)); - - } catch (NameNotFoundException e) { - // App not found. Remove it from the list - mExtapp.removeApp(packagename); + + private void setClearApiSummary () { + Preference clearapi = findPreference("clearapi"); + + if (mExtapp.getExtAppList().isEmpty()) { + clearapi.setEnabled(false); + clearapi.setSummary(R.string.no_external_app_allowed); + } else { + clearapi.setEnabled(true); + clearapi.setSummary(getString(R.string.allowed_apps, getExtAppList(", "))); } } - return applist.toString(); - } + private String getExtAppList (String delim){ + ApplicationInfo app; + PackageManager pm = getActivity().getPackageManager(); + + StringBuilder applist = new StringBuilder(); + for (String packagename : mExtapp.getExtAppList()) { + try { + app = pm.getApplicationInfo(packagename, 0); + if (applist.length() != 0) + applist.append(delim); + applist.append(app.loadLabel(pm)); + + } catch (NameNotFoundException e) { + // App not found. Remove it from the list + mExtapp.removeApp(packagename); + } + } - private boolean isTunModuleAvailable() { - // Check if the tun module exists on the file system - return new File("/system/lib/modules/tun.ko").length() > 10; - } + return applist.toString(); + } - @Override - public boolean onPreferenceClick(Preference preference) { - if (preference.getKey().equals("clearapi")) { - Builder builder = new AlertDialog.Builder(getActivity()); - builder.setPositiveButton(R.string.clear, this); - builder.setNegativeButton(android.R.string.cancel, null); - builder.setMessage(getString(R.string.clearappsdialog, getExtAppList("\n"))); - builder.show(); - } else if (preference.getKey().equals("osslspeed")) { - startActivity(new Intent(getActivity(), OpenSSLSpeed.class)); + private boolean isTunModuleAvailable () { + // Check if the tun module exists on the file system + return new File("/system/lib/modules/tun.ko").length() > 10; } - return true; - } + @Override + public boolean onPreferenceClick (Preference preference){ + if (preference.getKey().equals("clearapi")) { + Builder builder = new AlertDialog.Builder(getActivity()); + builder.setPositiveButton(R.string.clear, this); + builder.setNegativeButton(android.R.string.cancel, null); + builder.setMessage(getString(R.string.clearappsdialog, getExtAppList("\n"))); + builder.show(); + } else if (preference.getKey().equals("osslspeed")) { + startActivity(new Intent(getActivity(), OpenSSLSpeed.class)); + } - @Override - public void onClick(DialogInterface dialog, int which) { - if (which == Dialog.BUTTON_POSITIVE) { - mExtapp.clearAllApiApps(); - setClearApiSummary(); + return true; + } + + @Override + public void onClick (DialogInterface dialog,int which){ + if (which == Dialog.BUTTON_POSITIVE) { + mExtapp.clearAllApiApps(); + setClearApiSummary(); + } } - } -} \ No newline at end of file + } \ No newline at end of file -- cgit v1.2.3