android: Show confirmation dialog also when connecting
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / ui / MainActivity.java
index e1b3e07..8904342 100644 (file)
 
 package org.strongswan.android.ui;
 
-import org.strongswan.android.R;
-import org.strongswan.android.data.VpnProfile;
-import org.strongswan.android.data.VpnProfileDataSource;
-import org.strongswan.android.data.VpnType.VpnTypeFeature;
-import org.strongswan.android.logic.CharonVpnService;
-import org.strongswan.android.logic.TrustedCertificateManager;
-import org.strongswan.android.logic.VpnStateService;
-import org.strongswan.android.logic.VpnStateService.State;
-import org.strongswan.android.ui.VpnProfileListFragment.OnVpnProfileSelectedListener;
-
-import android.app.ActionBar;
-import android.app.Activity;
-import android.app.AlertDialog;
-import android.app.AlertDialog.Builder;
 import android.app.Dialog;
-import android.app.DialogFragment;
-import android.app.Fragment;
-import android.app.FragmentManager;
-import android.app.FragmentTransaction;
 import android.app.Service;
 import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
@@ -46,6 +28,13 @@ import android.net.VpnService;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentTransaction;
+import android.support.v7.app.ActionBar;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.app.AppCompatActivity;
+import android.support.v7.app.AppCompatDialogFragment;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
@@ -54,12 +43,24 @@ import android.view.Window;
 import android.widget.EditText;
 import android.widget.Toast;
 
-public class MainActivity extends Activity implements OnVpnProfileSelectedListener
+import org.strongswan.android.R;
+import org.strongswan.android.data.VpnProfile;
+import org.strongswan.android.data.VpnProfileDataSource;
+import org.strongswan.android.data.VpnType.VpnTypeFeature;
+import org.strongswan.android.logic.CharonVpnService;
+import org.strongswan.android.logic.TrustedCertificateManager;
+import org.strongswan.android.logic.VpnStateService;
+import org.strongswan.android.logic.VpnStateService.State;
+import org.strongswan.android.ui.VpnProfileListFragment.OnVpnProfileSelectedListener;
+
+public class MainActivity extends AppCompatActivity implements OnVpnProfileSelectedListener
 {
        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 EXTRA_VPN_PROFILE_ID = "org.strongswan.android.VPN_PROFILE_ID";
-       /** Use "bring your own device" (BYOD) features */
+       /**
+        * Use "bring your own device" (BYOD) features
+        */
        public static final boolean USE_BYOD = true;
        private static final int PREPARE_VPN_SERVICE = 0;
        private static final String PROFILE_NAME = "org.strongswan.android.MainActivity.PROFILE_NAME";
@@ -69,7 +70,8 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
 
        private Bundle mProfileInfo;
        private VpnStateService mService;
-       private final ServiceConnection mServiceConnection = new ServiceConnection() {
+       private final ServiceConnection mServiceConnection = new ServiceConnection()
+       {
                @Override
                public void onServiceDisconnected(ComponentName name)
                {
@@ -91,16 +93,18 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
-               super.onCreate(savedInstanceState);
                requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
+               super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
 
+               ActionBar bar = getSupportActionBar();
+               bar.setDisplayShowHomeEnabled(true);
+               bar.setDisplayShowTitleEnabled(false);
+               bar.setIcon(R.drawable.ic_launcher);
+
                this.bindService(new Intent(this, VpnStateService.class),
                                                 mServiceConnection, Service.BIND_AUTO_CREATE);
 
-               ActionBar bar = getActionBar();
-               bar.setDisplayShowTitleEnabled(false);
-
                /* load CA certificates in a background task */
                new LoadCertificatesTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
@@ -157,6 +161,7 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
        /**
         * Prepare the VpnService. If this succeeds the current VPN profile is
         * started.
+        *
         * @param profileInfo a bundle containing the information about the profile to be started
         */
        protected void prepareVpnService(Bundle profileInfo)
@@ -225,13 +230,13 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
 
                removeFragmentByTag(DIALOG_TAG);
 
-               if (mService != null && mService.getState() == State.CONNECTED)
+               if (mService != null && (mService.getState() == State.CONNECTED || mService.getState() == State.CONNECTING))
                {
                        profileInfo.putBoolean(PROFILE_RECONNECT, mService.getProfile().getId() == profile.getId());
 
                        ConfirmationDialog dialog = new ConfirmationDialog();
                        dialog.setArguments(profileInfo);
-                       dialog.show(this.getFragmentManager(), DIALOG_TAG);
+                       dialog.show(this.getSupportFragmentManager(), DIALOG_TAG);
                        return;
                }
                startVpnProfile(profileInfo);
@@ -239,6 +244,7 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
 
        /**
         * Start the given VPN profile asking the user for a password if required.
+        *
         * @param profileInfo data about the profile
         */
        private void startVpnProfile(Bundle profileInfo)
@@ -248,7 +254,7 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                {
                        LoginDialog login = new LoginDialog();
                        login.setArguments(profileInfo);
-                       login.show(getFragmentManager(), DIALOG_TAG);
+                       login.show(getSupportFragmentManager(), DIALOG_TAG);
                        return;
                }
                prepareVpnService(profileInfo);
@@ -257,6 +263,7 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
        /**
         * Start the VPN profile referred to by the given intent. Displays an error
         * if the profile doesn't exist.
+        *
         * @param intent Intent that caused us to start this
         */
        private void startVpnProfile(Intent intent)
@@ -291,11 +298,13 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                {
                        setProgressBarIndeterminateVisibility(true);
                }
+
                @Override
                protected TrustedCertificateManager doInBackground(Void... params)
                {
                        return TrustedCertificateManager.getInstance().load();
                }
+
                @Override
                protected void onPostExecute(TrustedCertificateManager result)
                {
@@ -308,7 +317,7 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
         */
        public void removeFragmentByTag(String tag)
        {
-               FragmentManager fm = getFragmentManager();
+               FragmentManager fm = getSupportFragmentManager();
                Fragment login = fm.findFragmentByTag(tag);
                if (login != null)
                {
@@ -322,7 +331,7 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
         * Class that displays a confirmation dialog if a VPN profile is already connected
         * and then initiates the selected VPN profile if the user confirms the dialog.
         */
-       public static class ConfirmationDialog extends DialogFragment
+       public static class ConfirmationDialog extends AppCompatDialogFragment
        {
                @Override
                public Dialog onCreateDialog(Bundle savedInstanceState)
@@ -345,7 +354,8 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                                .setIcon(icon)
                                .setTitle(String.format(getString(title), profileInfo.getString(PROFILE_NAME)))
                                .setMessage(message)
-                               .setPositiveButton(button, new DialogInterface.OnClickListener() {
+                               .setPositiveButton(button, new DialogInterface.OnClickListener()
+                               {
                                        @Override
                                        public void onClick(DialogInterface dialog, int whichButton)
                                        {
@@ -353,7 +363,8 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                                                activity.startVpnProfile(profileInfo);
                                        }
                                })
-                               .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
+                               .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
+                               {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which)
                                        {
@@ -367,7 +378,7 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
         * Class that displays a login dialog and initiates the selected VPN
         * profile if the user confirms the dialog.
         */
-       public static class LoginDialog extends DialogFragment
+       public static class LoginDialog extends AppCompatDialogFragment
        {
                @Override
                public Dialog onCreateDialog(Bundle savedInstanceState)
@@ -379,10 +390,11 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                        username.setText(profileInfo.getString(VpnProfileDataSource.KEY_USERNAME));
                        final EditText password = (EditText)view.findViewById(R.id.password);
 
-                       Builder adb = new AlertDialog.Builder(getActivity());
+                       AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
                        adb.setView(view);
                        adb.setTitle(getString(R.string.login_title));
-                       adb.setPositiveButton(R.string.login_confirm, new DialogInterface.OnClickListener() {
+                       adb.setPositiveButton(R.string.login_confirm, new DialogInterface.OnClickListener()
+                       {
                                @Override
                                public void onClick(DialogInterface dialog, int whichButton)
                                {
@@ -391,7 +403,8 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                                        activity.prepareVpnService(profileInfo);
                                }
                        });
-                       adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
+                       adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
+                       {
                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
@@ -406,17 +419,17 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
         * Class representing an error message which is displayed if VpnService is
         * not supported on the current device.
         */
-       public static class VpnNotSupportedError extends DialogFragment
+       public static class VpnNotSupportedError extends AppCompatDialogFragment
        {
                static final String ERROR_MESSAGE_ID = "org.strongswan.android.VpnNotSupportedError.MessageId";
 
-               public static void showWithMessage(Activity activity, int messageId)
+               public static void showWithMessage(AppCompatActivity activity, int messageId)
                {
                        Bundle bundle = new Bundle();
                        bundle.putInt(ERROR_MESSAGE_ID, messageId);
                        VpnNotSupportedError dialog = new VpnNotSupportedError();
                        dialog.setArguments(bundle);
-                       dialog.show(activity.getFragmentManager(), DIALOG_TAG);
+                       dialog.show(activity.getSupportFragmentManager(), DIALOG_TAG);
                }
 
                @Override
@@ -428,7 +441,8 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                                .setTitle(R.string.vpn_not_supported_title)
                                .setMessage(messageId)
                                .setCancelable(false)
-                               .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+                               .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
+                               {
                                        @Override
                                        public void onClick(DialogInterface dialog, int id)
                                        {