android: Simplified handling of error dialog that is displayed if VpnService API...
authorTobias Brunner <tobias@strongswan.org>
Fri, 24 Aug 2012 13:06:39 +0000 (15:06 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 27 Aug 2012 08:48:13 +0000 (10:48 +0200)
src/frontends/android/src/org/strongswan/android/ui/MainActivity.java

index b02c609..4a1bb6d 100644 (file)
@@ -46,10 +46,8 @@ 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 AlertDialog mErrorDialog;
        private Bundle mProfileInfo;
 
        @Override
        private Bundle mProfileInfo;
 
        @Override
@@ -62,33 +60,11 @@ 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);
@@ -134,7 +110,7 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                                 * don't have the VPN components built into the system image.
                                 * com.android.vpndialogs/com.android.vpndialogs.ConfirmDialog
                                 * will not be found then */
                                 * don't have the VPN components built into the system image.
                                 * com.android.vpndialogs/com.android.vpndialogs.ConfirmDialog
                                 * will not be found then */
-                               showVpnNotSupportedError();
+                               new VpnNotSupportedError().show(getFragmentManager(), "ErrorDialog");
                        }
                }
                else
                        }
                }
                else
@@ -181,25 +157,6 @@ 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>
@@ -263,4 +220,27 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                        return adb.create();
                }
        }
                        return adb.create();
                }
        }
+
+       /**
+        * Class representing an error message which is displayed if VpnService is
+        * not supported on the current device.
+        */
+       public static class VpnNotSupportedError extends DialogFragment
+       {
+               @Override
+               public Dialog onCreateDialog(Bundle savedInstanceState)
+               {
+                       return new AlertDialog.Builder(getActivity())
+                               .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)
+                                       {
+                                               dialog.dismiss();
+                                       }
+                               }).create();
+               }
+       }
 }
 }