Repository: thundernest/k-9
Issue: Username isn't trimmed

**Describe the bug**
When adding a new email account, the specified username is not trimmed. Thus when C&P'ing a username that happens to contain a space (at the end), the authentication will fail and it is very hard to figure out that the training space is the issue, since it is not rendered (as a visible character)

**Expected behavior**
The username should be trimmed to remove leading and training spaces. Alternatively leading or training whitespace should be highlighted so it is easier to spot.


**Environment (please complete the following information):**
 - K-9 Mail version: 5.806
 - Android version: 8
 - Device: Shiftphone 6m
 - Account type: IMAP

Changed files:
app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupOutgoing.java, app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java

diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java
index b5cba2113..c3f27bcf5 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupIncoming.java
@@ -21,6 +21,7 @@ import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.Spinner;
 import android.widget.Toast;
 
+import androidx.annotation.NonNull;
 import com.fsck.k9.Account;
 import com.fsck.k9.DI;
 import com.fsck.k9.LocalKeyStoreManager;
@@ -395,7 +396,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
     }
 
     @Override
-    public void onSaveInstanceState(Bundle outState) {
+    public void onSaveInstanceState(@NonNull Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putString(EXTRA_ACCOUNT, mAccount.getUuid());
         outState.putInt(STATE_SECURITY_TYPE_POSITION, mCurrentSecurityTypeViewPosition);
@@ -550,7 +551,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
                  * Set the username and password for the outgoing settings to the username and
                  * password the user just set for incoming.
                  */
-                String username = mUsernameView.getText().toString();
+                String username = mUsernameView.getText().toString().trim();
 
                 String password = null;
                 String clientCertificateAlias = null;
@@ -579,7 +580,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
         try {
             ConnectionSecurity connectionSecurity = getSelectedSecurity();
 
-            String username = mUsernameView.getText().toString();
+            String username = mUsernameView.getText().toString().trim();
             String password = null;
             String clientCertificateAlias = null;
 
@@ -662,12 +663,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
         }
     };
 
-    OnClientCertificateChangedListener clientCertificateChangedListener = new OnClientCertificateChangedListener() {
-        @Override
-        public void onClientCertificateChanged(String alias) {
-            validateFields();
-        }
-    };
+    OnClientCertificateChangedListener clientCertificateChangedListener = alias -> validateFields();
 
     private AuthType getSelectedAuthType() {
         AuthTypeHolder holder = (AuthTypeHolder) mAuthTypeView.getSelectedItem();
diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupOutgoing.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupOutgoing.java
index 72eebcbed..8e64281ae 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupOutgoing.java
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/setup/AccountSetupOutgoing.java
@@ -22,6 +22,7 @@ import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.Spinner;
 import android.widget.Toast;
 
+import androidx.annotation.NonNull;
 import com.fsck.k9.Account;
 import com.fsck.k9.DI;
 import com.fsck.k9.LocalKeyStoreManager;
@@ -189,7 +190,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
             }
             mSecurityTypeView.setSelection(mCurrentSecurityTypeViewPosition, false);
 
-            if (settings.username != null && !settings.username.isEmpty()) {
+            if (!settings.username.isEmpty()) {
                 mUsernameView.setText(settings.username);
                 mRequireLoginView.setChecked(true);
                 mRequireLoginSettingsView.setVisibility(View.VISIBLE);
@@ -316,7 +317,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
     }
 
     @Override
-    public void onSaveInstanceState(Bundle outState) {
+    public void onSaveInstanceState(@NonNull Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putString(EXTRA_ACCOUNT, mAccount.getUuid());
         outState.putInt(STATE_SECURITY_TYPE_POSITION, mCurrentSecurityTypeViewPosition);
@@ -491,7 +492,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
             clientCertificateAlias = mClientCertificateSpinner.getAlias();
         }
         if (mRequireLoginView.isChecked()) {
-            username = mUsernameView.getText().toString();
+            username = mUsernameView.getText().toString().trim();
             authType = getSelectedAuthType();
 
             if (AuthType.EXTERNAL != authType) {
@@ -543,12 +544,7 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
         }
     };
 
-    OnClientCertificateChangedListener clientCertificateChangedListener = new OnClientCertificateChangedListener() {
-        @Override
-        public void onClientCertificateChanged(String alias) {
-            validateFields();
-        }
-    };
+    OnClientCertificateChangedListener clientCertificateChangedListener = alias -> validateFields();
 
     private AuthType getSelectedAuthType() {
         AuthTypeHolder holder = (AuthTypeHolder) mAuthTypeView.getSelectedItem();