android: Avoid ProgressDialogs in VPN state fragment
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / ui / VpnStateFragment.java
index 10bf2c3..0b093d7 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (C) 2012-2013 Tobias Brunner
+ * Copyright (C) 2012-2016 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
@@ -17,7 +17,6 @@
 
 package org.strongswan.android.ui;
 
-import android.app.ProgressDialog;
 import android.app.Service;
 import android.content.ComponentName;
 import android.content.Context;
@@ -34,6 +33,7 @@ import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.widget.Button;
+import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import org.strongswan.android.R;
@@ -60,9 +60,7 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
        private int mColorStateError;
        private int mColorStateSuccess;
        private Button mActionButton;
-       private ProgressDialog mConnectDialog;
-       private ProgressDialog mDisconnectDialog;
-       private ProgressDialog mProgressDialog;
+       private ProgressBar mProgress;
        private AlertDialog mErrorDialog;
        private long mErrorConnectionID;
        private long mDismissedConnectionID;
@@ -133,8 +131,9 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
                                }
                        }
                });
-               enableActionButton(false);
+               enableActionButton(null);
 
+               mProgress = (ProgressBar)view.findViewById(R.id.progress);
                mStateView = (TextView)view.findViewById(R.id.vpn_state);
                mColorStateBase = mStateView.getCurrentTextColor();
                mProfileView = (TextView)view.findViewById(R.id.vpn_profile_label);
@@ -163,7 +162,6 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
                        mService.unregisterListener(this);
                }
                hideErrorDialog();
-               hideProgressDialog();
        }
 
        @Override
@@ -202,33 +200,35 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
                        return;
                }
 
-               enableActionButton(false);
                mProfileNameView.setText(name);
 
                switch (state)
                {
                        case DISABLED:
                                showProfile(false);
-                               hideProgressDialog();
+                               mProgress.setVisibility(View.GONE);
+                               enableActionButton(null);
                                mStateView.setText(R.string.state_disabled);
                                mStateView.setTextColor(mColorStateBase);
                                break;
                        case CONNECTING:
                                showProfile(true);
-                               showConnectDialog(name, gateway);
+                               mProgress.setVisibility(View.VISIBLE);
+                               enableActionButton(getString(android.R.string.cancel));
                                mStateView.setText(R.string.state_connecting);
                                mStateView.setTextColor(mColorStateBase);
                                break;
                        case CONNECTED:
                                showProfile(true);
-                               hideProgressDialog();
-                               enableActionButton(true);
+                               mProgress.setVisibility(View.GONE);
+                               enableActionButton(getString(R.string.disconnect));
                                mStateView.setText(R.string.state_connected);
                                mStateView.setTextColor(mColorStateSuccess);
                                break;
                        case DISCONNECTING:
                                showProfile(true);
-                               showDisconnectDialog(name);
+                               mProgress.setVisibility(View.VISIBLE);
+                               enableActionButton(null);
                                mStateView.setText(R.string.state_disconnecting);
                                mStateView.setTextColor(mColorStateBase);
                                break;
@@ -254,10 +254,10 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
                {       /* we already show the dialog */
                        return true;
                }
-               hideProgressDialog();
                mProfileNameView.setText(name);
                showProfile(true);
-               enableActionButton(false);
+               mProgress.setVisibility(View.GONE);
+               enableActionButton(null);
                mStateView.setText(R.string.state_error);
                mStateView.setTextColor(mColorStateError);
                switch (error)
@@ -294,20 +294,11 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
                mProfileNameView.setVisibility(show ? View.VISIBLE : View.GONE);
        }
 
-       private void enableActionButton(boolean enable)
+       private void enableActionButton(String text)
        {
-               mActionButton.setEnabled(enable);
-               mActionButton.setVisibility(enable ? View.VISIBLE : View.GONE);
-       }
-
-       private void hideProgressDialog()
-       {
-               if (mProgressDialog != null)
-               {
-                       mProgressDialog.dismiss();
-                       mProgressDialog = null;
-                       mDisconnectDialog = mConnectDialog = null;
-               }
+               mActionButton.setText(text);
+               mActionButton.setEnabled(text != null);
+               mActionButton.setVisibility(text != null ? View.VISIBLE : View.GONE);
        }
 
        private void hideErrorDialog()
@@ -329,50 +320,6 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
                updateView();
        }
 
-       private void showConnectDialog(String profile, String gateway)
-       {
-               if (mConnectDialog != null)
-               {       /* already showing the dialog */
-                       return;
-               }
-               hideProgressDialog();
-               mConnectDialog = new ProgressDialog(getActivity());
-               mConnectDialog.setTitle(String.format(getString(R.string.connecting_title), profile));
-               mConnectDialog.setMessage(String.format(getString(R.string.connecting_message), gateway));
-               mConnectDialog.setIndeterminate(true);
-               mConnectDialog.setCancelable(false);
-               mConnectDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
-                                                                getString(android.R.string.cancel),
-                                                                new DialogInterface.OnClickListener()
-                                                                {
-                                                                        @Override
-                                                                        public void onClick(DialogInterface dialog, int which)
-                                                                        {
-                                                                                if (mService != null)
-                                                                                {
-                                                                                        mService.disconnect();
-                                                                                }
-                                                                        }
-                                                                });
-               mConnectDialog.show();
-               mProgressDialog = mConnectDialog;
-       }
-
-       private void showDisconnectDialog(String profile)
-       {
-               if (mDisconnectDialog != null)
-               {       /* already showing the dialog */
-                       return;
-               }
-               hideProgressDialog();
-               mDisconnectDialog = new ProgressDialog(getActivity());
-               mDisconnectDialog.setMessage(getString(R.string.state_disconnecting));
-               mDisconnectDialog.setIndeterminate(true);
-               mDisconnectDialog.setCancelable(false);
-               mDisconnectDialog.show();
-               mProgressDialog = mDisconnectDialog;
-       }
-
        private void showErrorDialog(int textid)
        {
                final List<RemediationInstruction> instructions = mService.getRemediationInstructions();