android: Avoid ProgressDialogs in VPN state fragment
authorTobias Brunner <tobias@strongswan.org>
Thu, 28 Apr 2016 12:46:10 +0000 (14:46 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 2 May 2016 14:43:15 +0000 (16:43 +0200)
Instead we use a ProgressBar directly in the fragment and use the
existing button to cancel the process.

src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnStateFragment.java
src/frontends/android/app/src/main/res/layout/vpn_state_fragment.xml
src/frontends/android/app/src/main/res/values-de/strings.xml
src/frontends/android/app/src/main/res/values-pl/strings.xml
src/frontends/android/app/src/main/res/values-ru/strings.xml
src/frontends/android/app/src/main/res/values-ua/strings.xml
src/frontends/android/app/src/main/res/values/strings.xml

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();
index e347c4c..86a2f13 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-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
         </TextView>
     </GridLayout>
 
+    <ProgressBar
+        android:id="@+id/progress"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginBottom="10dp"
+        android:layout_marginLeft="20dp"
+        android:layout_marginRight="20dp"
+        android:indeterminate="true"
+        android:visibility="gone"
+        style="@style/Widget.AppCompat.ProgressBar.Horizontal" />
+
     <Button
         android:id="@+id/action"
         android:layout_width="match_parent"
index 8b6ff95..076bea3 100644 (file)
     <string name="error_auth_failed">Benutzerauthentifizierung ist fehlgeschlagen.</string>
     <string name="error_assessment_failed">Sicherheitsassessment ist fehlgeschlagen.</string>
     <string name="error_generic">Unbekannter Fehler während des Verbindens.</string>
-    <string name="connecting_title">Verbinden: %1$s</string>
-    <string name="connecting_message">Verbinde mit \""%1$s\".</string>
     <string name="vpn_connected">VPN verbunden</string>
     <string name="vpn_profile_connected">Dieses VPN Profil ist momentan verbunden!</string>
     <string name="reconnect">Neu verbinden</string>
index 2638719..6a1e346 100644 (file)
     <string name="error_auth_failed">Błąd przy autoryzacji użytkownika</string>
     <string name="error_assessment_failed">Security assessment failed.</string>
     <string name="error_generic">Nieznany błąd w czasie połączenia</string>
-    <string name="connecting_title">Łączenie: %1$s</string>
-    <string name="connecting_message">Tworzenie tunelu VPN z \""%1$s\".</string>
     <string name="vpn_connected">Połączenie z VPN</string>
     <string name="vpn_profile_connected">Ten profil VPN jest obecnie połaczony!</string>
     <string name="reconnect">Połączyć ponownie</string>
index ec9d663..098bdf9 100644 (file)
     <string name="error_auth_failed">Ошибка авторизации пользователя.</string>
     <string name="error_assessment_failed">Security assessment failed.</string>
     <string name="error_generic">Неизвестная ошибка.</string>
-    <string name="connecting_title">Подключение: %1$s</string>
-    <string name="connecting_message">Подключение к VPN с \""%1$s\".</string>
     <string name="vpn_connected">Соединение с VPN установлено</string>
     <string name="vpn_profile_connected">Подключение к этому профилю VPN уже существует!</string>
     <string name="reconnect">Переподключить</string>
index a5e668b..d6b9772 100644 (file)
     <string name="error_auth_failed">Помилка аутентифікації користувача.</string>
     <string name="error_assessment_failed">Security assessment failed.</string>
     <string name="error_generic">Невідома помилка під час підключення.</string>
-    <string name="connecting_title">Підключення: %1$s</string>
-    <string name="connecting_message">Підключення VPN з \""%1$s\".</string>
     <string name="vpn_connected">VPN підключено</string>
     <string name="vpn_profile_connected">Цей VPN профіль зараз підключений!</string>
     <string name="reconnect">Перепідключитися</string>
index 2b0049f..be90dc9 100644 (file)
     <string name="error_auth_failed">User authentication failed.</string>
     <string name="error_assessment_failed">Security assessment 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>
     <string name="vpn_connected">VPN connected</string>
     <string name="vpn_profile_connected">This VPN profile is currently connected!</string>
     <string name="reconnect">Reconnect</string>