Show an error message if VPN is not supported
authorTobias Brunner <tobias@strongswan.org>
Wed, 15 Aug 2012 08:51:30 +0000 (10:51 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 15 Aug 2012 09:19:43 +0000 (11:19 +0200)
Some devices have Android 4 installed but the system images still seem to
lack the components that are required for VPN support. One such
component is the dialog used to grant permission to create .

src/frontends/android/res/values-de/strings.xml
src/frontends/android/res/values/strings.xml
src/frontends/android/src/org/strongswan/android/ui/MainActivity.java

index 13a34b2..9f3f637 100644 (file)
@@ -23,6 +23,8 @@
     <string name="reload_trusted_certs">CA-Zertifikate neu laden</string>
     <string name="show_log">Log anzeigen</string>
     <string name="search">Suchen</string>
     <string name="reload_trusted_certs">CA-Zertifikate neu laden</string>
     <string name="show_log">Log anzeigen</string>
     <string name="search">Suchen</string>
+    <string name="vpn_not_supported_title">VPN nicht unterstützt</string>
+    <string name="vpn_not_supported">Ihr Gerät unterstützt keine VPN Anwendungen.\nBitte kontaktieren Sie den Hersteller.</string>
 
     <!-- Log view -->
     <string name="log_title">Log</string>
 
     <!-- Log view -->
     <string name="log_title">Log</string>
index 07dd8c2..bc7fa4a 100644 (file)
@@ -23,6 +23,8 @@
     <string name="reload_trusted_certs">Reload CA certificates</string>
     <string name="show_log">View log</string>
     <string name="search">Search</string>
     <string name="reload_trusted_certs">Reload CA certificates</string>
     <string name="show_log">View log</string>
     <string name="search">Search</string>
+    <string name="vpn_not_supported_title">VPN not supported</string>
+    <string name="vpn_not_supported">Your device does not support VPN applications.\nPlease contact the manufacturer.</string>
 
     <!-- Log view -->
     <string name="log_title">Log</string>
 
     <!-- Log view -->
     <string name="log_title">Log</string>
index 80f1a27..bc5030e 100644 (file)
@@ -30,6 +30,7 @@ import android.app.AlertDialog;
 import android.app.AlertDialog.Builder;
 import android.app.Dialog;
 import android.app.DialogFragment;
 import android.app.AlertDialog.Builder;
 import android.app.Dialog;
 import android.app.DialogFragment;
+import android.content.ActivityNotFoundException;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
@@ -46,8 +47,11 @@ import android.widget.EditText;
 public class MainActivity extends Activity implements OnVpnProfileSelectedListener
 {
        public static final String CONTACT_EMAIL = "android@strongswan.org";
 public class MainActivity extends Activity implements OnVpnProfileSelectedListener
 {
        public static final String CONTACT_EMAIL = "android@strongswan.org";
+       private static final String SHOW_ERROR_DIALOG = "errordialog";
        private static final int PREPARE_VPN_SERVICE = 0;
        private static final int PREPARE_VPN_SERVICE = 0;
+
        private VpnProfile activeProfile;
        private VpnProfile activeProfile;
+       private AlertDialog mErrorDialog;
 
        @Override
        public void onCreate(Bundle savedInstanceState)
 
        @Override
        public void onCreate(Bundle savedInstanceState)
@@ -59,11 +63,33 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                ActionBar bar = getActionBar();
                bar.setDisplayShowTitleEnabled(false);
 
                ActionBar bar = getActionBar();
                bar.setDisplayShowTitleEnabled(false);
 
+               if (savedInstanceState != null && savedInstanceState.getBoolean(SHOW_ERROR_DIALOG))
+               {
+                       showVpnNotSupportedError();
+               }
+
                /* load CA certificates in a background task */
                new CertificateLoadTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, false);
        }
 
        @Override
                /* load CA certificates in a background task */
                new CertificateLoadTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, false);
        }
 
        @Override
+       protected void onSaveInstanceState(Bundle outState)
+       {
+               super.onSaveInstanceState(outState);
+               outState.putBoolean(SHOW_ERROR_DIALOG, mErrorDialog != null);
+       }
+
+       @Override
+       protected void onDestroy()
+       {
+               super.onDestroy();
+               if (mErrorDialog != null)
+               {       /* avoid any errors about leaked windows */
+                       mErrorDialog.dismiss();
+               }
+       }
+
+       @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
                getMenuInflater().inflate(R.menu.main, menu);
        public boolean onCreateOptionsMenu(Menu menu)
        {
                getMenuInflater().inflate(R.menu.main, menu);
@@ -96,7 +122,18 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                Intent intent = VpnService.prepare(this);
                if (intent != null)
                {
                Intent intent = VpnService.prepare(this);
                if (intent != null)
                {
-                       startActivityForResult(intent, PREPARE_VPN_SERVICE);
+                       try
+                       {
+                               startActivityForResult(intent, PREPARE_VPN_SERVICE);
+                       }
+                       catch (ActivityNotFoundException ex)
+                       {
+                               /* it seems some devices, even though they come with Android 4,
+                                * don't have the VPN components built into the system image.
+                                * com.android.vpndialogs/com.android.vpndialogs.ConfirmDialog
+                                * will not be found then */
+                               showVpnNotSupportedError();
+                       }
                }
                else
                {
                }
                else
                {
@@ -139,6 +176,25 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
        }
 
        /**
        }
 
        /**
+        * Show an error dialog if case the device lacks VPN support.
+        */
+       private void showVpnNotSupportedError()
+       {
+               mErrorDialog = new AlertDialog.Builder(this)
+                       .setTitle(R.string.vpn_not_supported_title)
+                       .setMessage(getString(R.string.vpn_not_supported))
+                       .setCancelable(false)
+                       .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+                               @Override
+                               public void onClick(DialogInterface dialog, int id)
+                               {
+                                       mErrorDialog = null;
+                                       dialog.dismiss();
+                               }
+                       }).show();
+       }
+
+       /**
         * Class that loads or reloads the cached CA certificates.
         */
        private class CertificateLoadTask extends AsyncTask<Boolean, Void, TrustedCertificateManager>
         * Class that loads or reloads the cached CA certificates.
         */
        private class CertificateLoadTask extends AsyncTask<Boolean, Void, TrustedCertificateManager>