Show an error dialog when errors occur while establishing the VPN
authorTobias Brunner <tobias@strongswan.org>
Thu, 9 Aug 2012 09:33:22 +0000 (11:33 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 13 Aug 2012 09:18:51 +0000 (11:18 +0200)
src/frontends/android/res/values/strings.xml
src/frontends/android/src/org/strongswan/android/ui/VpnStateFragment.java

index 4a719ef..04e0926 100644 (file)
     <string name="state_connected">Connected</string>
     <string name="state_disconnecting">Disconnecting&#8230;</string>
     <string name="state_disabled">No active VPN</string>
+    <string name="state_error">Error</string>
 
     <!-- Dialogs -->
     <string name="login_title">Enter password to connect</string>
     <string name="login_confirm">Connect</string>
+    <string name="error_introduction">Failed to establish VPN:</string>
+    <string name="error_lookup_failed">Gateway address lookup failed.</string>
+    <string name="error_unreachable">Gateway is unreachable.</string>
+    <string name="error_peer_auth_failed">Verifying gateway authentication failed.</string>
+    <string name="error_auth_failed">User authentication failed.</string>
+    <string name="error_generic">Unspecified failure while connecting.</string>
     <string name="connecting_title">Connecting: %1$s</string>
     <string name="connecting_message">Establishing VPN with \""%1$s\".</string>
 
index 94475a3..a164ff1 100644 (file)
@@ -20,9 +20,11 @@ package org.strongswan.android.ui;
 import org.strongswan.android.R;
 import org.strongswan.android.data.VpnProfile;
 import org.strongswan.android.logic.VpnStateService;
+import org.strongswan.android.logic.VpnStateService.ErrorState;
 import org.strongswan.android.logic.VpnStateService.State;
 import org.strongswan.android.logic.VpnStateService.VpnStateListener;
 
+import android.app.AlertDialog;
 import android.app.Fragment;
 import android.app.ProgressDialog;
 import android.app.Service;
@@ -50,6 +52,7 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
        private Button mActionButton;
        private ProgressDialog mProgressDialog;
        private State mState;
+       private AlertDialog mErrorDialog;
        private VpnStateService mService;
        private final ServiceConnection mServiceConnection = new ServiceConnection() {
                @Override
@@ -119,6 +122,7 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
        public void onStop()
        {
                super.onStop();
+               hideErrorDialog();
                hideProgressDialog();
        }
 
@@ -142,6 +146,7 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
        public void updateView()
        {
                State state = mService.getState();
+               ErrorState error = ErrorState.NO_ERROR;
                String name = "", gateway = "";
 
                if (state != State.DISABLED)
@@ -152,6 +157,12 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
                                name = profile.getName();
                                gateway = profile.getGateway();
                        }
+                       error = mService.getErrorState();
+               }
+
+               if (reportError(name, state, error))
+               {
+                       return;
                }
 
                if (state == mState)
@@ -192,6 +203,44 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
                }
        }
 
+       private boolean reportError(String name, State state, ErrorState error)
+       {
+               if (error == ErrorState.NO_ERROR || state != State.CONNECTING && state != State.CONNECTED)
+               {       /* we only report errors while initiating */
+                       hideErrorDialog();
+                       return false;
+               }
+               else if (mErrorDialog != null)
+               {       /* we already show the dialog */
+                       return true;
+               }
+               hideProgressDialog();
+               mProfileNameView.setText(name);
+               showProfile(true);
+               enableActionButton(false);
+               mStateView.setText(R.string.state_error);
+               mStateView.setTextColor(Color.RED);
+               switch (error)
+               {
+                       case AUTH_FAILED:
+                               showErrorDialog(R.string.error_auth_failed);
+                               break;
+                       case PEER_AUTH_FAILED:
+                               showErrorDialog(R.string.error_peer_auth_failed);
+                               break;
+                       case LOOKUP_FAILED:
+                               showErrorDialog(R.string.error_lookup_failed);
+                               break;
+                       case UNREACHABLE:
+                               showErrorDialog(R.string.error_unreachable);
+                               break;
+                       default:
+                               showErrorDialog(R.string.error_generic);
+                               break;
+               }
+               return true;
+       }
+
        private void showProfile(boolean show)
        {
                mProfileView.setVisibility(show ? View.VISIBLE : View.GONE);
@@ -213,6 +262,15 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
                }
        }
 
+       private void hideErrorDialog()
+       {
+               if (mErrorDialog != null)
+               {
+                       mErrorDialog.dismiss();
+                       mErrorDialog = null;
+               }
+       }
+
        private void showConnectDialog(String profile, String gateway)
        {
                mProgressDialog = new ProgressDialog(getActivity());
@@ -243,4 +301,20 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
                mProgressDialog.setCancelable(false);
                mProgressDialog.show();
        }
+
+       private void showErrorDialog(int textid)
+       {
+               mErrorDialog = new AlertDialog.Builder(getActivity())
+                       .setMessage(getString(R.string.error_introduction) + " " + getString(textid))
+                       .setCancelable(false)
+                       .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+                               @Override
+                               public void onClick(DialogInterface dialog, int id)
+                               {
+                                       mErrorDialog = null;
+                                       dialog.dismiss();
+                               }
+                       }).create();
+               mErrorDialog.show();
+       }
 }