account creation: fix issues after first attempt, cleanup

Change-Id: I1d0ce2b1ab5fbfaa8310cd1cb00fb191ee04e6ae
diff --git a/ring-android/libringclient/src/main/java/cx/ring/account/AccountWizardPresenter.java b/ring-android/libringclient/src/main/java/cx/ring/account/AccountWizardPresenter.java
index 78d6c2d..bd7bea4 100644
--- a/ring-android/libringclient/src/main/java/cx/ring/account/AccountWizardPresenter.java
+++ b/ring-android/libringclient/src/main/java/cx/ring/account/AccountWizardPresenter.java
@@ -32,23 +32,19 @@
 import cx.ring.services.AccountService;
 import cx.ring.services.DeviceRuntimeService;
 import cx.ring.utils.Log;
+import cx.ring.utils.StringUtils;
 import io.reactivex.observers.DisposableObserver;
 
-public class AccountWizardPresenter extends RootPresenter<AccountWizardView>{
+public class AccountWizardPresenter extends RootPresenter<AccountWizardView> {
 
     public static final String TAG = AccountWizardPresenter.class.getSimpleName();
 
-    protected AccountService mAccountService;
-    protected DeviceRuntimeService mDeviceRuntimeService;
+    private final AccountService mAccountService;
+    private final DeviceRuntimeService mDeviceRuntimeService;
 
-    private boolean mLinkAccount = false;
     private boolean mCreationError = false;
     private boolean mCreatingAccount = false;
-
     private String mAccountType;
-    private Account mAccount;
-    private String mCreatedAccountId;
-
     private RingAccountViewModel mRingAccountViewModel;
 
     @Inject
@@ -57,16 +53,6 @@
         this.mDeviceRuntimeService = deviceRuntimeService;
     }
 
-    @Override
-    public void bindView(AccountWizardView view) {
-        super.bindView(view);
-    }
-
-    @Override
-    public void unbindView() {
-        super.unbindView();
-    }
-
     public void init(String accountType) {
         mAccountType = accountType;
         if (AccountConfig.ACCOUNT_TYPE_SIP.equals(mAccountType)) {
@@ -124,15 +110,12 @@
     private HashMap<String, String> initAccountDetails() {
         try {
             HashMap<String, String> accountDetails = (HashMap<String, String>) mAccountService.getAccountTemplate(mAccountType);
-            for (Map.Entry<String, String> e : accountDetails.entrySet()) {
+            for (Map.Entry<String, String> e: accountDetails.entrySet()) {
                 Log.d(TAG, "Default account detail: " + e.getKey() + " -> " + e.getValue());
             }
 
             boolean hasCameraPermission = mDeviceRuntimeService.hasVideoPermission();
             accountDetails.put(ConfigKey.VIDEO_ENABLED.key(), Boolean.toString(hasCameraPermission));
-
-            //~ Sipinfo is forced for any sipaccount since overrtp is not supported yet.
-            //~ This will have to be removed when it will be supported.
             accountDetails.put(ConfigKey.ACCOUNT_DTMF_TYPE.key(), "sipinfo");
             return accountDetails;
         } catch (Exception e) {
@@ -153,82 +136,69 @@
         getView().blockOrientation();
         getView().displayProgress(true);
 
-        if (mAccountType.equals(AccountConfig.ACCOUNT_TYPE_RING) || mAccount == null) {
-            mCompositeDisposable.add(mAccountService.addAccount(accountDetails).subscribeWith(new DisposableObserver<Account>() {
-                @Override
-                public void onNext(Account account) {
-                    if (!handleCreationState(account)) {
+        mCompositeDisposable.add(mAccountService.addAccount(accountDetails)
+                .subscribeWith(new DisposableObserver<Account>() {
+                    @Override
+                    public void onNext(Account account) {
+                        if (!handleCreationState(account)) {
+                            mCreatingAccount = false;
+                            dispose();
+                        }
+                    }
+
+                    @Override
+                    public void onError(Throwable e) {
+                        handleCreationState(null);
+                        mCreatingAccount = false;
                         dispose();
                     }
-                }
-                @Override
-                public void onError(Throwable e) {
-                    handleCreationState(null);
-                    dispose();
-                }
-                @Override
-                public void onComplete() {}
-            }));
-        } else {
-            mAccount.setDetail(ConfigKey.ACCOUNT_ALIAS, accountDetails.get(ConfigKey.ACCOUNT_ALIAS.key()));
-            if (accountDetails.containsKey(ConfigKey.ACCOUNT_HOSTNAME.key())) {
-                mAccount.setDetail(ConfigKey.ACCOUNT_HOSTNAME, accountDetails.get(ConfigKey.ACCOUNT_HOSTNAME.key()));
-                mAccount.setDetail(ConfigKey.ACCOUNT_USERNAME, accountDetails.get(ConfigKey.ACCOUNT_USERNAME.key()));
-                mAccount.setDetail(ConfigKey.ACCOUNT_PASSWORD, accountDetails.get(ConfigKey.ACCOUNT_PASSWORD.key()));
-            }
 
-            mAccountService.setAccountDetails(mAccount.getAccountID(), mAccount.getDetails());
-        }
-
-        mCreatingAccount = false;
+                    @Override
+                    public void onComplete() {
+                    }
+                }));
     }
 
     private boolean handleCreationState(final Account account) {
+        AccountWizardView view = getView();
         if (account == null) {
-            getView().displayProgress(false);
-            getView().displayCannotBeFoundError();
+            view.displayProgress(false);
+            view.displayCannotBeFoundError();
             return false;
         }
         String newState = account.getRegistrationState();
         if (account.isRing() && (newState.isEmpty() || newState.contentEquals(AccountConfig.STATE_INITIALIZING))) {
             return true;
         }
-        getView().displayProgress(false);
+        view.displayProgress(false);
 
         if (!mCreationError) {
             switch (newState) {
                 case AccountConfig.STATE_ERROR_GENERIC:
-                    getView().displayGenericError();
+                    view.displayGenericError();
                     mCreationError = true;
                     break;
                 case AccountConfig.STATE_UNREGISTERED:
-                    if (mLinkAccount) {
-                        getView().displayCannotBeFoundError();
-                        mCreationError = true;
-                    } else {
-                        getView().displaySuccessDialog();
-                        getView().saveProfile(account.getAccountID(), mRingAccountViewModel);
-                        mCreationError = false;
-                        mAccountService.setCurrentAccount(account);
-                        break;
-                    }
+                    view.displaySuccessDialog();
+                    view.saveProfile(account.getAccountID(), mRingAccountViewModel);
+                    mCreationError = false;
                     break;
                 case AccountConfig.STATE_ERROR_NETWORK:
-                    getView().displayNetworkError();
+                    view.displayNetworkError();
                     mCreationError = true;
                     break;
                 default:
-                    getView().displaySuccessDialog();
-                    getView().saveProfile(account.getAccountID(), mRingAccountViewModel);
+                    view.displaySuccessDialog();
+                    view.saveProfile(account.getAccountID(), mRingAccountViewModel);
                     mCreationError = false;
-                    mAccountService.setCurrentAccount(account);
                     break;
             }
 
-            if (mRingAccountViewModel.getUsername() != null && !mRingAccountViewModel.getUsername().contentEquals("")) {
+            if (account.isRing() && !StringUtils.isEmpty(mRingAccountViewModel.getUsername())) {
                 Log.i(TAG, "Account created, registering " + mRingAccountViewModel.getUsername());
-                mAccountService.registerName(account, "", mRingAccountViewModel.getUsername());
+                mAccountService.registerName(account, mRingAccountViewModel.getPassword(), mRingAccountViewModel.getUsername());
             }
+            mAccountService.setCurrentAccount(account);
         }
         return false;
     }
diff --git a/ring-android/libringclient/src/main/java/cx/ring/account/RingAccountCreationPresenter.java b/ring-android/libringclient/src/main/java/cx/ring/account/RingAccountCreationPresenter.java
index 7d4b85d..9843345 100644
--- a/ring-android/libringclient/src/main/java/cx/ring/account/RingAccountCreationPresenter.java
+++ b/ring-android/libringclient/src/main/java/cx/ring/account/RingAccountCreationPresenter.java
@@ -126,16 +126,21 @@
     }
 
     public void createAccount() {
-        getView().enableNextButton(false);
-        getView().goToAccountCreation(mRingAccountViewModel);
+        if (isInputValid()) {
+            RingAccountCreationView view = getView();
+            view.enableNextButton(false);
+            view.goToAccountCreation(mRingAccountViewModel);
+        }
+    }
+
+    private boolean isInputValid() {
+        boolean passwordOk = isPasswordCorrect && isConfirmCorrect;
+        boolean usernameOk = !isRegisterUsernameChecked || isRingUserNameCorrect;
+        return passwordOk && usernameOk;
     }
 
     private void checkForms() {
-        if (isRegisterUsernameChecked) {
-            getView().enableNextButton(isRingUserNameCorrect && isPasswordCorrect && isConfirmCorrect);
-        } else {
-            getView().enableNextButton(isPasswordCorrect && isConfirmCorrect);
-        }
+        getView().enableNextButton(isInputValid());
     }
 
     private void handleBlockchainResult(String name, String address, int state) {