From 918f17fa3558912970483b73474dcab955306e20 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Thu, 5 May 2016 12:52:39 +0200 Subject: Give the user a choice if the vpn is already connected. --- .../blinkt/openvpn/activities/DisconnectVPN.java | 23 ++++++++++------------ .../de/blinkt/openvpn/core/ProfileManager.java | 14 ++++++++----- .../java/de/blinkt/openvpn/core/VpnStatus.java | 3 +++ .../blinkt/openvpn/fragments/VPNProfileList.java | 14 +++++++++++-- main/src/main/res/values/strings.xml | 1 + 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/main/src/main/java/de/blinkt/openvpn/activities/DisconnectVPN.java b/main/src/main/java/de/blinkt/openvpn/activities/DisconnectVPN.java index cc7e2266..b955532a 100644 --- a/main/src/main/java/de/blinkt/openvpn/activities/DisconnectVPN.java +++ b/main/src/main/java/de/blinkt/openvpn/activities/DisconnectVPN.java @@ -10,7 +10,9 @@ import android.app.AlertDialog; import android.content.*; import android.os.IBinder; +import de.blinkt.openvpn.LaunchVPN; import de.blinkt.openvpn.R; +import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.OpenVPNService; import de.blinkt.openvpn.core.ProfileManager; @@ -53,24 +55,13 @@ public class DisconnectVPN extends Activity implements DialogInterface.OnClickLi unbindService(mConnection); } - // if (getIntent() !=null && OpenVpnService.DISCONNECT_VPN.equals(getIntent().getAction())) - - // setIntent(null); - - /* - @Override - protected void onNewIntent(Intent intent) { - super.onNewIntent(intent); - setIntent(intent); - } - */ - private void showDisconnectDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.title_cancel); builder.setMessage(R.string.cancel_connection_query); builder.setNegativeButton(android.R.string.no, this); - builder.setPositiveButton(android.R.string.yes,this); + builder.setNeutralButton(R.string.reconnect, this); + builder.setPositiveButton(android.R.string.yes, this); builder.setOnCancelListener(this); builder.show(); @@ -78,10 +69,16 @@ public class DisconnectVPN extends Activity implements DialogInterface.OnClickLi @Override public void onClick(DialogInterface dialog, int which) { + VpnProfile lastVPN = ProfileManager.getLastConnectedVpn(); if (which == DialogInterface.BUTTON_POSITIVE) { ProfileManager.setConntectedVpnProfileDisconnected(this); if (mService != null && mService.getManagement() != null) mService.getManagement().stopVPN(false); + } else if (which == DialogInterface.BUTTON_NEUTRAL && lastVPN !=null) { + Intent intent = new Intent(this, LaunchVPN.class); + intent.putExtra(LaunchVPN.EXTRA_KEY, lastVPN.getUUID().toString()); + intent.setAction(Intent.ACTION_MAIN); + startActivity(intent); } finish(); } 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 165bd647..2856a069 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java +++ b/main/src/main/java/de/blinkt/openvpn/core/ProfileManager.java @@ -66,21 +66,25 @@ public class ProfileManager { } - public static void setConnectedVpnProfile(Context c, VpnProfile connectedrofile) { + /** + * Sets the profile that is connected (to connect if the service restarts) + */ + public static void setConnectedVpnProfile(Context c, VpnProfile connectedProfile) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); Editor prefsedit = prefs.edit(); - prefsedit.putString(LAST_CONNECTED_PROFILE, connectedrofile.getUUIDString()); + prefsedit.putString(LAST_CONNECTED_PROFILE, connectedProfile.getUUIDString()); prefsedit.apply(); - mLastConnectedVpn = connectedrofile; + mLastConnectedVpn = connectedProfile; } + /** + * Returns the profile that was last connected (to connect if the service restarts) + */ public static VpnProfile getLastConnectedProfile(Context c) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); - - String lastConnectedProfile = prefs.getString(LAST_CONNECTED_PROFILE, null); if (lastConnectedProfile != null) return get(c, lastConnectedProfile); diff --git a/main/src/main/java/de/blinkt/openvpn/core/VpnStatus.java b/main/src/main/java/de/blinkt/openvpn/core/VpnStatus.java index d23f0013..8b076437 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/VpnStatus.java +++ b/main/src/main/java/de/blinkt/openvpn/core/VpnStatus.java @@ -59,6 +59,9 @@ public class VpnStatus { static final int MAXLOGENTRIES = 1000; + public static boolean isVPNActive() { + return mLastLevel != ConnectionStatus.LEVEL_AUTH_FAILED && !(mLastLevel == ConnectionStatus.LEVEL_NOTCONNECTED); + } public static String getLastCleanLogMessage(Context c) { String message = mLaststatemsg; diff --git a/main/src/main/java/de/blinkt/openvpn/fragments/VPNProfileList.java b/main/src/main/java/de/blinkt/openvpn/fragments/VPNProfileList.java index 3f5e806e..0323ba76 100644 --- a/main/src/main/java/de/blinkt/openvpn/fragments/VPNProfileList.java +++ b/main/src/main/java/de/blinkt/openvpn/fragments/VPNProfileList.java @@ -28,6 +28,7 @@ import android.widget.Toast; import de.blinkt.openvpn.*; import de.blinkt.openvpn.activities.ConfigConverter; +import de.blinkt.openvpn.activities.DisconnectVPN; import de.blinkt.openvpn.activities.FileSelect; import de.blinkt.openvpn.activities.VPNPreferences; import de.blinkt.openvpn.core.ProfileManager; @@ -82,7 +83,7 @@ public class VPNProfileList extends ListFragment implements OnClickListener, Vpn titleview.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - startVPN(profile); + startOrStopVPN(profile); } }); @@ -110,6 +111,15 @@ public class VPNProfileList extends ListFragment implements OnClickListener, Vpn } } + private void startOrStopVPN(VpnProfile profile) { + if (VpnStatus.isVPNActive()) { + Intent disconnectVPN = new Intent(getActivity(), DisconnectVPN.class); + startActivity(disconnectVPN); + } else { + startVPN(profile); + } + } + private ArrayAdapter mArrayadapter; @@ -212,7 +222,7 @@ public class VPNProfileList extends ListFragment implements OnClickListener, Vpn } private void setListAdapter() { - if (mArrayadapter==null) { + if (mArrayadapter == null) { mArrayadapter = new VPNArrayAdapter(getActivity(), R.layout.vpn_list_item, R.id.vpn_item_title); } diff --git a/main/src/main/res/values/strings.xml b/main/src/main/res/values/strings.xml index 6ba546e1..aca6c317 100755 --- a/main/src/main/res/values/strings.xml +++ b/main/src/main/res/values/strings.xml @@ -408,4 +408,5 @@ Samsung phones No VPN selected. VPN used on boot and for Always-On + Reconnect -- cgit v1.2.3