From cd3268f4ff9fc6b9a63e14191ef4e5d77f4dd8f3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Wed, 17 Jun 2015 16:25:44 -0400
Subject: [PATCH] ui: make account creation UI-blocking and asynchronous

During account creation a waiting dialog is presented.

The method call itself is done asynchronously in a
background thread using AsyncTask instead of blocking
the UI.

Refs #75354

Change-Id: I94b8984553cc1f3c4de0e69e0328d26974ade491
---
 .../fragments/AccountCreationFragment.java    | 55 ++++++++++++++-----
 .../main/res/values-fr/strings_account.xml    |  2 +
 .../src/main/res/values/strings_account.xml   |  2 +
 3 files changed, 44 insertions(+), 15 deletions(-)

diff --git a/ring-android/app/src/main/java/cx/ring/fragments/AccountCreationFragment.java b/ring-android/app/src/main/java/cx/ring/fragments/AccountCreationFragment.java
index 161d2b7a6..14c5d09f7 100644
--- a/ring-android/app/src/main/java/cx/ring/fragments/AccountCreationFragment.java
+++ b/ring-android/app/src/main/java/cx/ring/fragments/AccountCreationFragment.java
@@ -9,7 +9,9 @@ import cx.ring.service.ISipService;
 
 import android.app.Activity;
 import android.app.Fragment;
+import android.app.ProgressDialog;
 import android.content.Intent;
+import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.RemoteException;
 import android.text.TextUtils;
@@ -23,11 +25,11 @@ import android.widget.Spinner;
 import android.widget.TextView;
 import android.widget.TextView.OnEditorActionListener;
 import android.widget.Toast;
-import android.widget.ViewFlipper;
 
 import cx.ring.views.PasswordEditText;
 
 public class AccountCreationFragment extends Fragment {
+    static final String TAG = AccountCreationFragment.class.getSimpleName();
 
     // Values for email and password at the time of the login attempt.
     private String mAlias;
@@ -42,7 +44,6 @@ public class AccountCreationFragment extends Fragment {
     private EditText mUsernameView;
     private PasswordEditText mPasswordView;
     private Spinner mAccountTypeView;
-    private ViewGroup mFieldFlipper;
     private ViewGroup mFieldsSip;
     private ViewGroup mFieldsRing;
 
@@ -70,7 +71,6 @@ public class AccountCreationFragment extends Fragment {
     public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
         View inflatedView = inflater.inflate(R.layout.frag_account_creation, parent, false);
 
-        mFieldFlipper = (ViewGroup) inflatedView.findViewById(R.id.field_flipper);
         mFieldsSip = (ViewGroup) inflatedView.findViewById(R.id.sip_fields);
         mFieldsRing = (ViewGroup) inflatedView.findViewById(R.id.ring_fields);
 
@@ -78,7 +78,7 @@ public class AccountCreationFragment extends Fragment {
         mHostnameView = (EditText) inflatedView.findViewById(R.id.hostname);
         mUsernameView = (EditText) inflatedView.findViewById(R.id.username);
         mPasswordView = (PasswordEditText) inflatedView.findViewById(R.id.password);
-		mAccountTypeView = (Spinner) inflatedView.findViewById(R.id.account_type);
+        mAccountTypeView = (Spinner) inflatedView.findViewById(R.id.account_type);
         mAccountTypeView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
             @Override
             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
@@ -231,21 +231,46 @@ public class AccountCreationFragment extends Fragment {
             e.printStackTrace();
         }
 
-        Intent resultIntent = new Intent(getActivity(), HomeActivity.class);
-        getActivity().setResult(Activity.RESULT_OK, resultIntent);
-        resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
-        startActivity(resultIntent);
-        getActivity().finish();
-
     }
 
     private void createNewAccount(HashMap<String, String> accountDetails) {
-        try {
+        //noinspection unchecked
+        new AsyncTask<HashMap<String, String>, Void, String>() {
+            private ProgressDialog progress = null;
 
-            mCallbacks.getService().addAccount(accountDetails);
-        } catch (RemoteException e) {
-            e.printStackTrace();
-        }
+            @Override
+            protected void onPreExecute() {
+                progress = new ProgressDialog(getActivity());
+                progress.setTitle(R.string.dialog_wait_create);
+                progress.setMessage(getString(R.string.dialog_wait_create_details));
+                progress.setCancelable(false);
+                progress.setCanceledOnTouchOutside(false);
+                progress.show();
+            }
+
+            @Override
+            protected String doInBackground(HashMap<String, String>... accs) {
+                try {
+                    return mCallbacks.getService().addAccount(accs[0]);
+                } catch (RemoteException e) {
+                    e.printStackTrace();
+                }
+                return null;
+            }
+
+            @Override
+            protected void onPostExecute(String s) {
+                if (progress != null) {
+                    progress.dismiss();
+                    progress = null;
+                }
+                Intent resultIntent = new Intent(getActivity(), HomeActivity.class);
+                getActivity().setResult(s.isEmpty() ? Activity.RESULT_CANCELED : Activity.RESULT_OK, resultIntent);
+                //resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
+                //startActivity(resultIntent);
+                getActivity().finish();
+            }
+        }.execute(accountDetails);
     }
 
 }
diff --git a/ring-android/app/src/main/res/values-fr/strings_account.xml b/ring-android/app/src/main/res/values-fr/strings_account.xml
index 92971c37f..3211c2828 100644
--- a/ring-android/app/src/main/res/values-fr/strings_account.xml
+++ b/ring-android/app/src/main/res/values-fr/strings_account.xml
@@ -39,6 +39,8 @@ as that of the covered work.
     <string name="action_create">Ajouter le compte</string>
     <string name="action_create_short">Enregistrer</string>
     <string name="error_field_required">Ce champ est requis</string>
+    <string name="dialog_wait_create">Ajout du compte</string>
+    <string name="dialog_wait_create_details">Merci de patienter pendant l\'ajout de votre compte…</string>
 
     <!-- AccountManagementFragment -->
     <string name="preference_section1">Comptes</string>
diff --git a/ring-android/app/src/main/res/values/strings_account.xml b/ring-android/app/src/main/res/values/strings_account.xml
index 974290f87..ff09f59b1 100644
--- a/ring-android/app/src/main/res/values/strings_account.xml
+++ b/ring-android/app/src/main/res/values/strings_account.xml
@@ -39,6 +39,8 @@ as that of the covered work.
     <string name="action_create">Add account</string>
     <string name="action_create_short">Register</string>
     <string name="error_field_required">This field is required</string>
+    <string name="dialog_wait_create">Adding account</string>
+    <string name="dialog_wait_create_details">Please wait while your new account is added…</string>
 
     <!-- AccountManagementFragment -->
     <string name="preference_section1">Accounts</string>
-- 
GitLab