android: Allow disconnecting via MainActivity but display a confirmation dialog
authorTobias Brunner <tobias@strongswan.org>
Fri, 23 Jun 2017 08:35:08 +0000 (10:35 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 3 Jul 2017 08:39:26 +0000 (10:39 +0200)
src/frontends/android/app/src/main/java/org/strongswan/android/logic/CharonVpnService.java
src/frontends/android/app/src/main/java/org/strongswan/android/ui/MainActivity.java
src/frontends/android/app/src/main/res/values-de/strings.xml
src/frontends/android/app/src/main/res/values-pl/strings.xml
src/frontends/android/app/src/main/res/values-ru/strings.xml
src/frontends/android/app/src/main/res/values-ua/strings.xml
src/frontends/android/app/src/main/res/values-zh-rCN/strings.xml
src/frontends/android/app/src/main/res/values-zh-rTW/strings.xml
src/frontends/android/app/src/main/res/values/strings.xml

index ecc69ae..cfed2e3 100644 (file)
@@ -365,10 +365,10 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                builder.setContentTitle(getString(s));
                if (!publicVersion)
                {
-                       Intent intent = new Intent(getApplicationContext(), CharonVpnService.class);
-                       intent.setAction(CharonVpnService.DISCONNECT_ACTION);
-                       PendingIntent pending = PendingIntent.getService(getApplicationContext(), 0, intent,
-                                                                                                                        PendingIntent.FLAG_ONE_SHOT);
+                       Intent intent = new Intent(getApplicationContext(), MainActivity.class);
+                       intent.setAction(MainActivity.DISCONNECT);
+                       PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, intent,
+                                                                                                                         PendingIntent.FLAG_UPDATE_CURRENT);
                        builder.addAction(R.drawable.ic_notification_disconnect, getString(R.string.disconnect), pending);
                        builder.setContentText(name);
                        builder.setPublicVersion(buildNotification(true));
index 5ba1061..76aef72 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 2012-2017 Tobias Brunner
  * Copyright (C) 2012 Giuliano Grassi
  * Copyright (C) 2012 Ralf Sager
- * Hochschule fuer Technik Rapperswil
+ * HSR Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -58,6 +58,7 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
 {
        public static final String CONTACT_EMAIL = "android@strongswan.org";
        public static final String START_PROFILE = "org.strongswan.android.action.START_PROFILE";
+       public static final String DISCONNECT = "org.strongswan.android.action.DISCONNECT";
        public static final String EXTRA_VPN_PROFILE_ID = "org.strongswan.android.VPN_PROFILE_ID";
        /**
         * Use "bring your own device" (BYOD) features
@@ -67,6 +68,7 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
        private static final String PROFILE_NAME = "org.strongswan.android.MainActivity.PROFILE_NAME";
        private static final String PROFILE_REQUIRES_PASSWORD = "org.strongswan.android.MainActivity.REQUIRES_PASSWORD";
        private static final String PROFILE_RECONNECT = "org.strongswan.android.MainActivity.RECONNECT";
+       private static final String PROFILE_DISCONNECT = "org.strongswan.android.MainActivity.DISCONNECT";
        private static final String DIALOG_TAG = "Dialog";
 
        private Bundle mProfileInfo;
@@ -88,6 +90,10 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
                        {
                                startVpnProfile(getIntent());
                        }
+                       else if (DISCONNECT.equals(getIntent().getAction()))
+                       {
+                               disconnect();
+                       }
                }
        };
 
@@ -132,6 +138,10 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
                {
                        startVpnProfile(intent);
                }
+               else if (DISCONNECT.equals(intent.getAction()))
+               {
+                       disconnect();
+               }
        }
 
        @Override
@@ -304,6 +314,25 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
        }
 
        /**
+        * Disconnect the current connection, if any (silently ignored if there is no connection).
+        */
+       private void disconnect()
+       {
+               removeFragmentByTag(DIALOG_TAG);
+
+               if (mService != null && (mService.getState() == State.CONNECTED || mService.getState() == State.CONNECTING))
+               {
+                       Bundle args = new Bundle();
+                       args.putBoolean(PROFILE_DISCONNECT, true);
+
+                       ConfirmationDialog dialog = new ConfirmationDialog();
+                       dialog.setArguments(args);
+                       dialog.show(this.getSupportFragmentManager(), DIALOG_TAG);
+                       return;
+               }
+       }
+
+       /**
         * Class that loads the cached CA certificates.
         */
        private class LoadCertificatesTask extends AsyncTask<Void, Void, TrustedCertificateManager>
@@ -364,6 +393,12 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
                                message = R.string.vpn_profile_connected;
                                button = R.string.reconnect;
                        }
+                       else if (profileInfo.getBoolean(PROFILE_DISCONNECT))
+                       {
+                               title = R.string.disconnect_question;
+                               message = R.string.disconnect_active_connection;
+                               button = R.string.disconnect;
+                       }
 
                        return new AlertDialog.Builder(getActivity())
                                .setIcon(icon)
@@ -375,7 +410,17 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
                                        public void onClick(DialogInterface dialog, int whichButton)
                                        {
                                                MainActivity activity = (MainActivity)getActivity();
-                                               activity.startVpnProfile(profileInfo);
+                                               if (profileInfo.getBoolean(PROFILE_DISCONNECT))
+                                               {
+                                                       if (activity.mService != null)
+                                                       {
+                                                               activity.mService.disconnect();
+                                                       }
+                                               }
+                                               else
+                                               {
+                                                       activity.startVpnProfile(profileInfo);
+                                               }
                                        }
                                })
                                .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
index 34fe846..bc6927d 100644 (file)
     <string name="reconnect">Neu verbinden</string>
     <string name="connect_profile_question">Verbinde %1$s?</string>
     <string name="replaces_active_connection">Dies ersetzt die aktuelle VPN Verbindung!</string>
+    <string name="disconnect_question">VPN Verbindung trennen?</string>
+    <string name="disconnect_active_connection">Dies trennt die aktuelle VPN Verbindung!</string>
     <string name="connect">Verbinden</string>
 
 </resources>
index a87381a..424837a 100644 (file)
     <string name="reconnect">Połączyć ponownie</string>
     <string name="connect_profile_question">Połącz %1$s?</string>
     <string name="replaces_active_connection">To zastąpi aktywne połączenie VPN!</string>
+    <string name="disconnect_question">Disconnect VPN?</string>
+    <string name="disconnect_active_connection">This will disconnect the active VPN connection!</string>
     <string name="connect">Połącz</string>
 
 </resources>
index 0ce54e5..a537720 100644 (file)
     <string name="reconnect">Переподключить</string>
     <string name="connect_profile_question">Подключить %1$s?</string>
     <string name="replaces_active_connection">Это заменит ваше текущее VPN соединение!</string>
+    <string name="disconnect_question">Disconnect VPN?</string>
+    <string name="disconnect_active_connection">This will disconnect the active VPN connection!</string>
     <string name="connect">Соединить</string>
 
 </resources>
index 5b769b7..4d36009 100644 (file)
     <string name="reconnect">Перепідключитися</string>
     <string name="connect_profile_question">Підключити %1$s?</string>
     <string name="replaces_active_connection">Ця дія замінить ваше поточне VPN з\'єднання!</string>
+    <string name="disconnect_question">Disconnect VPN?</string>
+    <string name="disconnect_active_connection">This will disconnect the active VPN connection!</string>
     <string name="connect">Підключити</string>
 
 </resources>
index 4202c5f..606bd27 100644 (file)
     <string name="reconnect">重连</string>
     <string name="connect_profile_question">是否连接%1$s?</string>
     <string name="replaces_active_connection">这将覆盖您当前活跃的VPN连接!</string>
+    <string name="disconnect_question">Disconnect VPN?</string>
+    <string name="disconnect_active_connection">This will disconnect the active VPN connection!</string>
     <string name="connect">连接</string>
 
 </resources>
index 6c0e104..52f21f8 100644 (file)
     <string name="reconnect">重新連線</string>
     <string name="connect_profile_question">是否連線%1$s?</string>
     <string name="replaces_active_connection">這將會覆蓋您當前運作的VPN連線!</string>
+    <string name="disconnect_question">Disconnect VPN?</string>
+    <string name="disconnect_active_connection">This will disconnect the active VPN connection!</string>
     <string name="connect">連線</string>
 
 </resources>
index f99f7de..4ee6be3 100644 (file)
     <string name="reconnect">Reconnect</string>
     <string name="connect_profile_question">Connect %1$s?</string>
     <string name="replaces_active_connection">This will replace your active VPN connection!</string>
+    <string name="disconnect_question">Disconnect VPN?</string>
+    <string name="disconnect_active_connection">This will disconnect the active VPN connection!</string>
     <string name="connect">Connect</string>
 
 </resources>