From d73ff892bd4966d51a78f8d3835f7999e61de6b3 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Mon, 5 Jun 2023 13:21:01 +0200 Subject: Rename Onboot to always active Also try to keep that connection always active --- .../main/java/de/blinkt/openvpn/OnBootReceiver.java | 4 ++-- .../de/blinkt/openvpn/api/ExternalOpenVPNService.java | 2 +- .../java/de/blinkt/openvpn/core/ExtAuthHelper.java | 1 - .../java/de/blinkt/openvpn/core/OpenVPNService.java | 19 ++++++++++++++++++- .../java/de/blinkt/openvpn/core/ProfileManager.java | 2 +- main/src/main/res/values/strings.xml | 4 ++-- .../de/blinkt/openvpn/fragments/GeneralSettings.kt | 6 ++++-- .../de/blinkt/openvpn/fragments/SendDumpFragment.java | 7 ++++++- main/src/ui/res/xml/general_settings.xml | 4 ++-- 9 files changed, 36 insertions(+), 13 deletions(-) (limited to 'main/src') diff --git a/main/src/main/java/de/blinkt/openvpn/OnBootReceiver.java b/main/src/main/java/de/blinkt/openvpn/OnBootReceiver.java index 4827bf10..58c954c9 100644 --- a/main/src/main/java/de/blinkt/openvpn/OnBootReceiver.java +++ b/main/src/main/java/de/blinkt/openvpn/OnBootReceiver.java @@ -23,8 +23,8 @@ public class OnBootReceiver extends BroadcastReceiver { final String action = intent.getAction(); SharedPreferences prefs = Preferences.getDefaultSharedPreferences(context); - boolean useStartOnBoot = prefs.getBoolean("restartvpnonboot", false); - if (!useStartOnBoot) + boolean alwaysActive = prefs.getBoolean("restartvpnonboot", false); + if (!alwaysActive) return; if(Intent.ACTION_BOOT_COMPLETED.equals(action) || Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)) { diff --git a/main/src/main/java/de/blinkt/openvpn/api/ExternalOpenVPNService.java b/main/src/main/java/de/blinkt/openvpn/api/ExternalOpenVPNService.java index 8573af79..c6a4339a 100644 --- a/main/src/main/java/de/blinkt/openvpn/api/ExternalOpenVPNService.java +++ b/main/src/main/java/de/blinkt/openvpn/api/ExternalOpenVPNService.java @@ -341,7 +341,7 @@ public class ExternalOpenVPNService extends Service implements StateListener { - class UpdateMessage { + static class UpdateMessage { public String state; public String logmessage; public ConnectionStatus level; diff --git a/main/src/main/java/de/blinkt/openvpn/core/ExtAuthHelper.java b/main/src/main/java/de/blinkt/openvpn/core/ExtAuthHelper.java index d102dce2..39151646 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/ExtAuthHelper.java +++ b/main/src/main/java/de/blinkt/openvpn/core/ExtAuthHelper.java @@ -42,7 +42,6 @@ public class ExtAuthHelper { public static void setExternalAuthProviderSpinnerList(Spinner spinner, String selectedApp) { Context c = spinner.getContext(); - final PackageManager pm = c.getPackageManager(); ArrayList extProviders = getExternalAuthProviderList(c); int selectedPos = -1; diff --git a/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java b/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java index 9e42378b..39aaef57 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java +++ b/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java @@ -20,6 +20,7 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.pm.ShortcutManager; import android.content.res.Configuration; @@ -228,8 +229,22 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac endVpnService(); } + private boolean isAlwaysActiveEnabled() + { + SharedPreferences prefs = Preferences.getDefaultSharedPreferences(this); + return prefs.getBoolean("restartvpnonboot", false); + } + + boolean isVpnAlwaysOnEnabled() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + return isAlwaysOn(); + } + return false; + } + + private void endVpnService() { - if (!isAlwaysOn()) { + if (!isVpnAlwaysOnEnabled() && !isAlwaysActiveEnabled()) { /* if we should be an always on VPN, keep the timer running */ keepVPNAlive.unscheduleKeepVPNAliveJobService(this); } @@ -883,6 +898,8 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac /* On Tiramisu we install the routes exactly like promised */ VpnStatus.logDebug(R.string.routes_debug, TextUtils.join(", ", positiveIPv4Routes), TextUtils.join(", ", positiveIPv6Routes)); } + //VpnStatus.logInfo(String.format("Always active %s", isAlwaysOn() ? "on" : "off")); + setAllowedVpnPackages(builder); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { // VPN always uses the default network diff --git a/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java b/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java index 9f5c3ab5..1f12c2fa 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java +++ b/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java @@ -51,7 +51,7 @@ public class ProfileManager { return instance.profiles.get(key); } - private static void checkInstance(Context context) { + private synchronized static void checkInstance(Context context) { if (instance == null) { instance = new ProfileManager(); ProfileEncryption.initMasterCryptAlias(); diff --git a/main/src/main/res/values/strings.xml b/main/src/main/res/values/strings.xml index f68d6983..a83d091b 100755 --- a/main/src/main/res/values/strings.xml +++ b/main/src/main/res/values/strings.xml @@ -203,8 +203,8 @@ Using proxy %1$s %2$s Use system proxy Use the system wide configuration for HTTP/HTTPS proxies to connect. - OpenVPN will connect the specified VPN if it was active on system boot. Please read the connection warning FAQ before using this option on Android < 5.0. - Connect on boot + OpenVPN will connect the specified VPN on system boot and will try to keep the VPN connected. + Keep VPN connected Ignore Restart Configuration changes are applied after restarting the VPN. (Re)start the VPN now? diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt b/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt index 3c878d4d..6ba7c6fd 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt @@ -73,8 +73,10 @@ class GeneralSettings : PreferenceFragmentCompat(), Preference.OnPreferenceClick findPreference("restartvpnonboot") as CheckBoxPreference if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - val vpn:VpnService = VpnService() - startOnBoot.isChecked = vpn.isAlwaysOn + val vpn = VpnService() + if (vpn.isAlwaysOn) + /* This is not reliable when the VPN is not active */ + startOnBoot.isChecked } startOnBoot.onPreferenceChangeListener = diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/SendDumpFragment.java b/main/src/ui/java/de/blinkt/openvpn/fragments/SendDumpFragment.java index 6d4d2968..ae90f3d5 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/SendDumpFragment.java +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/SendDumpFragment.java @@ -36,7 +36,12 @@ public class SendDumpFragment extends Fragment { if (c.getCacheDir() == null) return null; - for (File f : c.getCacheDir().listFiles()) { + File[] filesList = c.getCacheDir().listFiles(); + + if (filesList == null) + return null; + + for (File f : filesList) { if (!f.getName().endsWith(".dmp")) continue; diff --git a/main/src/ui/res/xml/general_settings.xml b/main/src/ui/res/xml/general_settings.xml index 55eedb94..a1920aaf 100644 --- a/main/src/ui/res/xml/general_settings.xml +++ b/main/src/ui/res/xml/general_settings.xml @@ -39,8 +39,8 @@ + android:summary="@string/keep_vpn_connected_summary" + android:title="@string/keep_vpn_connected"/>