From b4b187c5be3bc4d588e397573b09527ae6630ab9 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Thu, 21 Apr 2016 14:03:48 +0200 Subject: Change the onBootlogic to always start the VPN. Also let the user explicitly device on the VPN for Android N(ew York Cheesecake?) --- .../java/de/blinkt/openvpn/OnBootReceiver.java | 10 ++++- .../de/blinkt/openvpn/core/OpenVPNService.java | 6 ++- .../de/blinkt/openvpn/core/ProfileManager.java | 13 ++++-- .../blinkt/openvpn/fragments/GeneralSettings.java | 51 +++++++++++++++++++++- main/src/main/res/values/strings.xml | 6 ++- main/src/main/res/xml/general_settings.xml | 10 ++++- 6 files changed, 84 insertions(+), 12 deletions(-) diff --git a/main/src/main/java/de/blinkt/openvpn/OnBootReceiver.java b/main/src/main/java/de/blinkt/openvpn/OnBootReceiver.java index fbd693b9..4f9d6f91 100644 --- a/main/src/main/java/de/blinkt/openvpn/OnBootReceiver.java +++ b/main/src/main/java/de/blinkt/openvpn/OnBootReceiver.java @@ -8,6 +8,9 @@ package de.blinkt.openvpn; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + import de.blinkt.openvpn.core.ProfileManager; @@ -18,9 +21,14 @@ public class OnBootReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + + boolean useStartOnBoot = prefs.getBoolean("restartvpnonboot", false); + if (!useStartOnBoot) + return; if(Intent.ACTION_BOOT_COMPLETED.equals(action) || Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)) { - VpnProfile bootProfile = ProfileManager.getLastConnectedProfile(context, true); + VpnProfile bootProfile = ProfileManager.getAlwaysOnVPN(context); if(bootProfile != null) { launchVPN(bootProfile, context); } 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 ef397fa8..3ab39a84 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java +++ b/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java @@ -363,12 +363,14 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac mProfile = ProfileManager.get(this, profileUUID); } else { /* The intent is null when we are set as always-on or the service has been restarted. */ - mProfile = ProfileManager.getLastConnectedProfile(this, false); + mProfile = ProfileManager.getLastConnectedProfile(this); VpnStatus.logInfo(R.string.service_restarted); /* Got no profile, just stop */ if (mProfile == null) { - Log.d("OpenVPN", "Got no last connected profile on null intent. Stopping"); + Log.d("OpenVPN", "Got no last connected profile on null intent. Assuming always on."); + mProfile = ProfileManager.getAlwaysOnVPN(this); + stopSelf(startId); return START_NOT_STICKY; } 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 4f9c219b..165bd647 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java +++ b/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java @@ -76,13 +76,10 @@ public class ProfileManager { } - public static VpnProfile getLastConnectedProfile(Context c, boolean onBoot) { + public static VpnProfile getLastConnectedProfile(Context c) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); - boolean useStartOnBoot = prefs.getBoolean("restartvpnonboot", false); - if (onBoot && !useStartOnBoot) - return null; String lastConnectedProfile = prefs.getString(LAST_CONNECTED_PROFILE, null); if (lastConnectedProfile != null) @@ -195,4 +192,12 @@ public class ProfileManager { return mLastConnectedVpn; } + public static VpnProfile getAlwaysOnVPN(Context context) { + checkInstance(context); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + + String uuid = prefs.getString("alwaysOnVpn", null); + return get(uuid); + + } } diff --git a/main/src/main/java/de/blinkt/openvpn/fragments/GeneralSettings.java b/main/src/main/java/de/blinkt/openvpn/fragments/GeneralSettings.java index 013369b9..529ae944 100644 --- a/main/src/main/java/de/blinkt/openvpn/fragments/GeneralSettings.java +++ b/main/src/main/java/de/blinkt/openvpn/fragments/GeneralSettings.java @@ -5,6 +5,7 @@ package de.blinkt.openvpn.fragments; import java.io.File; +import java.util.Collection; import android.app.AlertDialog; import android.app.AlertDialog.Builder; @@ -17,18 +18,24 @@ import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; import android.os.Bundle; import android.preference.CheckBoxPreference; +import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; import de.blinkt.openvpn.BuildConfig; import de.blinkt.openvpn.R; +import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.api.ExternalAppDatabase; +import de.blinkt.openvpn.core.ProfileManager; -public class GeneralSettings extends PreferenceFragment implements OnPreferenceClickListener, OnClickListener { + +public class GeneralSettings extends PreferenceFragment implements OnPreferenceClickListener, OnClickListener, Preference.OnPreferenceChangeListener { private ExternalAppDatabase mExtapp; + private ListPreference mAlwaysOnVPN; @Override public void onCreate(Bundle savedInstanceState) { @@ -40,6 +47,8 @@ public class GeneralSettings extends PreferenceFragment implements OnPreferenceC PreferenceCategory devHacks = (PreferenceCategory) findPreference("device_hacks"); + mAlwaysOnVPN = (ListPreference) findPreference("alwaysOnVpn"); + mAlwaysOnVPN.setOnPreferenceChangeListener(this); Preference loadtun = findPreference("loadTunModule"); @@ -70,7 +79,45 @@ public class GeneralSettings extends PreferenceFragment implements OnPreferenceC setClearApiSummary(); } - private void setClearApiSummary() { + @Override + public void onResume() { + super.onResume(); + + ProfileManager pm = ProfileManager.getInstance(getActivity()); + Collection profiles = pm.getProfiles(); + CharSequence[] entries = new CharSequence[profiles.size()]; + CharSequence[] entryValues = new CharSequence[profiles.size()];; + + int i=0; + for (VpnProfile p: profiles) + { + entries[i]=p.getName(); + entryValues[i]=p.getUUIDString(); + i++; + } + + mAlwaysOnVPN.setEntries(entries); + mAlwaysOnVPN.setEntryValues(entryValues); + + + VpnProfile vpn = ProfileManager.getAlwaysOnVPN(getActivity()); + if (vpn== null) + mAlwaysOnVPN.setSummary(R.string.novpn_selected); + else + mAlwaysOnVPN.setSummary(vpn.getName()); + + } + + @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()) { diff --git a/main/src/main/res/values/strings.xml b/main/src/main/res/values/strings.xml index ad905367..f68832e9 100755 --- a/main/src/main/res/values/strings.xml +++ b/main/src/main/res/values/strings.xml @@ -202,8 +202,8 @@ Use system proxy Use the system wide configuration for HTTP/HTTPS proxies to connect. You can <a href=\"https://www.paypal.com/cgi-bin/webscr?hosted_button_id=R2M6ZP9AF25LS&amp;cmd=_s-xclick\">donate with PayPal</a> - OpenVPN will reconnect a VPN if it was active on system reboot/shutdown. Please read the Connection warning FAQ before using this option. - Reconnect on reboot + 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 Ignore Restart Configuration changes are applied after restarting the VPN. (Re)start the VPN now? @@ -407,4 +407,6 @@ Reread (%d) log items from log cache file Even though Samsung phones are among the most selling Android phones, Samsung\'s firmware are also among the most buggy Android firmwares. The bugs are not limited to the VPN operation on these devices but many of them can be workarounded. In the following some of these bugs are described.\n\nDNS does not work unless the DNS server in the VPN range.\n\nOn many Samsung 5.x devices the allowed/disallowed apps feature does not work.\nOn Samsung 6.x VPN is reported not to work unless the VPN app is exempted from Powersave features. Samsung phones + No VPN selected. + VPN used on boot and for Always-On diff --git a/main/src/main/res/xml/general_settings.xml b/main/src/main/res/xml/general_settings.xml index 1e18b19c..bed282b0 100644 --- a/main/src/main/res/xml/general_settings.xml +++ b/main/src/main/res/xml/general_settings.xml @@ -4,7 +4,8 @@ ~ Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt --> - + + + +