migrated change password into separate activity

Daniel Gultsch created

Change summary

src/main/AndroidManifest.xml                                        |   3 
src/main/java/eu/siacs/conversations/ui/ChangePasswordActivity.java | 107 
src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java    |  99 
src/main/res/layout/activity_edit_account.xml                       |   9 
src/main/res/menu/editaccount.xml                                   |   4 
src/main/res/values/strings.xml                                     |   4 
6 files changed, 138 insertions(+), 88 deletions(-)

Detailed changes

src/main/AndroidManifest.xml 🔗

@@ -79,6 +79,9 @@
         <activity
             android:name=".ui.BlocklistActivity"
             android:label="@string/title_activity_block_list" />
+	    <activity
+		    android:name=".ui.ChangePasswordActivity"
+		    android:label="@string/change_password_on_server" />
         <activity
             android:name=".ui.ManageAccountActivity"
             android:configChanges="orientation|screenSize"

src/main/java/eu/siacs/conversations/ui/ChangePasswordActivity.java 🔗

@@ -0,0 +1,107 @@
+package eu.siacs.conversations.ui;
+
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import eu.siacs.conversations.R;
+import eu.siacs.conversations.entities.Account;
+import eu.siacs.conversations.services.XmppConnectionService;
+import eu.siacs.conversations.xmpp.jid.InvalidJidException;
+import eu.siacs.conversations.xmpp.jid.Jid;
+
+public class ChangePasswordActivity extends XmppActivity implements XmppConnectionService.OnAccountPasswordChanged {
+
+	private Button mChangePasswordButton;
+	private View.OnClickListener mOnChangePasswordButtonClicked = new View.OnClickListener() {
+		@Override
+		public void onClick(View view) {
+			if (mAccount != null) {
+				final String currentPassword = mCurrentPassword.getText().toString();
+				final String newPassword = mNewPassword.getText().toString();
+				final String newPasswordConfirm = mNewPasswordConfirm.getText().toString();
+				if (!currentPassword.equals(mAccount.getPassword())) {
+					mCurrentPassword.requestFocus();
+					mCurrentPassword.setError(getString(R.string.account_status_unauthorized));
+				} else if (!newPassword.equals(newPasswordConfirm)) {
+					mNewPasswordConfirm.requestFocus();
+					mNewPasswordConfirm.setError(getString(R.string.passwords_do_not_match));
+				} else if (newPassword.trim().isEmpty()) {
+					mNewPassword.requestFocus();
+					mNewPassword.setError(getString(R.string.password_should_not_be_empty));
+				} else {
+					mCurrentPassword.setError(null);
+					mNewPassword.setError(null);
+					mNewPasswordConfirm.setError(null);
+					xmppConnectionService.updateAccountPasswordOnServer(mAccount, newPassword, ChangePasswordActivity.this);
+					mChangePasswordButton.setEnabled(false);
+					mChangePasswordButton.setTextColor(getSecondaryTextColor());
+					mChangePasswordButton.setText(R.string.updating);
+				}
+			}
+		}
+	};
+	private EditText mCurrentPassword;
+	private EditText mNewPassword;
+	private EditText mNewPasswordConfirm;
+	private Account mAccount;
+
+	@Override
+	void onBackendConnected() {
+		try {
+			final String jid = getIntent() == null ? null : getIntent().getStringExtra("account");
+			if (jid != null) {
+				this.mAccount = xmppConnectionService.findAccountByJid(Jid.fromString(jid));
+			}
+		} catch (final InvalidJidException ignored) {
+
+		}
+
+	}
+
+	@Override
+	protected void onCreate(final Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_change_password);
+		Button mCancelButton = (Button) findViewById(R.id.left_button);
+		mCancelButton.setOnClickListener(new View.OnClickListener() {
+			@Override
+			public void onClick(View view) {
+				finish();
+			}
+		});
+		this.mChangePasswordButton = (Button) findViewById(R.id.right_button);
+		this.mChangePasswordButton.setOnClickListener(this.mOnChangePasswordButtonClicked);
+		this.mCurrentPassword = (EditText) findViewById(R.id.current_password);
+		this.mNewPassword = (EditText) findViewById(R.id.new_password);
+		this.mNewPasswordConfirm = (EditText) findViewById(R.id.new_password_confirm);
+	}
+
+	@Override
+	public void onPasswordChangeSucceeded() {
+		runOnUiThread(new Runnable() {
+			@Override
+			public void run() {
+				Toast.makeText(ChangePasswordActivity.this,R.string.password_changed,Toast.LENGTH_LONG).show();
+				finish();
+			}
+		});
+	}
+
+	@Override
+	public void onPasswordChangeFailed() {
+		runOnUiThread(new Runnable() {
+			@Override
+			public void run() {
+				mNewPassword.setError(getString(R.string.could_not_change_password));
+				mChangePasswordButton.setEnabled(true);
+				mChangePasswordButton.setTextColor(getPrimaryTextColor());
+				mChangePasswordButton.setText(R.string.change_password);
+			}
+		});
+
+	}
+}

src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java 🔗

@@ -25,7 +25,6 @@ import android.widget.Toast;
 
 import eu.siacs.conversations.R;
 import eu.siacs.conversations.entities.Account;
-import eu.siacs.conversations.services.XmppConnectionService;
 import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
 import eu.siacs.conversations.utils.CryptoHelper;
@@ -35,13 +34,12 @@ import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 import eu.siacs.conversations.xmpp.jid.Jid;
 import eu.siacs.conversations.xmpp.pep.Avatar;
 
-public class EditAccountActivity extends XmppActivity implements OnAccountUpdate, XmppConnectionService.OnAccountPasswordChanged {
+public class EditAccountActivity extends XmppActivity implements OnAccountUpdate{
 
 	private AutoCompleteTextView mAccountJid;
 	private EditText mPassword;
 	private EditText mPasswordConfirm;
 	private CheckBox mRegisterNew;
-	private CheckBox mChangePassword;
 	private Button mCancelButton;
 	private Button mSaveButton;
 	private TableLayout mMoreTable;
@@ -64,7 +62,6 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 	private Account mAccount;
 
 	private boolean mFetchingAvatar = false;
-	private boolean mChangingPassword = false;
 
 	private final OnClickListener mSaveButtonClickListener = new OnClickListener() {
 
@@ -76,7 +73,6 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 				return;
 			}
 			final boolean registerNewAccount = mRegisterNew.isChecked();
-			final boolean changePassword = mChangePassword.isChecked();
 			final Jid jid;
 			try {
 				jid = Jid.fromString(mAccountJid.getText().toString());
@@ -92,7 +88,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 			}
 			final String password = mPassword.getText().toString();
 			final String passwordConfirm = mPasswordConfirm.getText().toString();
-			if (registerNewAccount || changePassword) {
+			if (registerNewAccount) {
 				if (!password.equals(passwordConfirm)) {
 					mPasswordConfirm.setError(getString(R.string.passwords_do_not_match));
 					mPasswordConfirm.requestFocus();
@@ -104,21 +100,11 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 					mAccount.setUsername(jid.hasLocalpart() ? jid.getLocalpart() : "");
 					mAccount.setServer(jid.getDomainpart());
 				} catch (final InvalidJidException ignored) {
-				}
-				if (changePassword) {
-					if (mAccount.isOnlineAndConnected()) {
-						xmppConnectionService.updateAccountPasswordOnServer(mAccount, mPassword.getText().toString(),EditAccountActivity.this);
-						mChangingPassword = true;
-						updateSaveButton();
-					} else {
-						Toast.makeText(EditAccountActivity.this,R.string.not_connected_try_again,Toast.LENGTH_SHORT).show();
-					}
 					return;
-				} else {
-					mAccount.setPassword(password);
-					mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
-					xmppConnectionService.updateAccount(mAccount);
 				}
+				mAccount.setPassword(password);
+				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
+				xmppConnectionService.updateAccount(mAccount);
 			} else {
 				try {
 					if (xmppConnectionService.findAccountByJid(Jid.fromString(mAccountJid.getText().toString())) != null) {
@@ -157,6 +143,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 
 			@Override
 			public void run() {
+				invalidateOptionsMenu();
 				if (mAccount != null
 						&& mAccount.getStatus() != Account.State.ONLINE
 						&& mFetchingAvatar) {
@@ -209,26 +196,10 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 
 		@Override
 		public void afterTextChanged(final Editable s) {
-			toggleChangePasswordCheckbox();
-		}
-	};
 
-	private void toggleChangePasswordCheckbox() {
-		final boolean registrationReady = mAccount != null &&
-				mAccount.isOnlineAndConnected() &&
-				mAccount.getXmppConnection().getFeatures().register();
-		if (passwordFieldEdited() && registrationReady) {
-			mChangePassword.setVisibility(View.VISIBLE);
-		} else {
-			mChangePassword.setVisibility(View.INVISIBLE);
-			mChangePassword.setChecked(false);
 		}
-	}
+	};
 
-	private boolean passwordFieldEdited() {
-		final String password = this.mPassword.getText().toString();
-		return jidToEdit != null && mAccount != null && !password.isEmpty() && !mAccount.getPassword().equals(password);
-	}
 	private final OnClickListener mAvatarClickListener = new OnClickListener() {
 		@Override
 		public void onClick(final View view) {
@@ -263,11 +234,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 	}
 
 	protected void updateSaveButton() {
-		if (mChangingPassword) {
-			this.mSaveButton.setEnabled(false);
-			this.mSaveButton.setTextColor(getSecondaryTextColor());
-			this.mSaveButton.setText(R.string.updating);
-		} else if (mAccount != null && mAccount.getStatus() == Account.State.CONNECTING) {
+		if (mAccount != null && mAccount.getStatus() == Account.State.CONNECTING) {
 			this.mSaveButton.setEnabled(false);
 			this.mSaveButton.setTextColor(getSecondaryTextColor());
 			this.mSaveButton.setText(R.string.account_status_connecting);
@@ -322,7 +289,6 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 		this.mAvatar = (ImageView) findViewById(R.id.avater);
 		this.mAvatar.setOnClickListener(this.mAvatarClickListener);
 		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
-		this.mChangePassword = (CheckBox) findViewById(R.id.account_change_password);
 		this.mStats = (LinearLayout) findViewById(R.id.stats);
 		this.mSessionEst = (TextView) findViewById(R.id.session_est);
 		this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
@@ -353,7 +319,6 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 			}
 		};
 		this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
-		this.mChangePassword.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
 	}
 
 	@Override
@@ -363,13 +328,16 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 		final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
 		final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
 		final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
+		final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
 		if (mAccount == null) {
 			showQrCode.setVisible(false);
 			showBlocklist.setVisible(false);
 			showMoreInfo.setVisible(false);
+			changePassword.setVisible(false);
 		} else if (mAccount.getStatus() != Account.State.ONLINE) {
 			showBlocklist.setVisible(false);
 			showMoreInfo.setVisible(false);
+			changePassword.setVisible(false);
 		} else if (!mAccount.getXmppConnection().getFeatures().blocking()) {
 			showBlocklist.setVisible(false);
 		}
@@ -396,8 +364,6 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 					getActionBar().setTitle(R.string.action_add_account);
 				}
 			}
-			this.mChangePassword.setVisibility(View.GONE);
-			this.mChangePassword.setChecked(false);
 		}
 	}
 
@@ -425,13 +391,19 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 	public boolean onOptionsItemSelected(final MenuItem item) {
 		switch (item.getItemId()) {
 			case R.id.action_show_block_list:
-				final Intent intent = new Intent(this, BlocklistActivity.class);
-				intent.putExtra("account", mAccount.getJid().toString());
-				startActivity(intent);
+				final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
+				showBlocklistIntent.putExtra("account", mAccount.getJid().toString());
+				startActivity(showBlocklistIntent);
 				break;
 			case R.id.action_server_info_show_more:
 				mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
 				item.setChecked(!item.isChecked());
+				break;
+			case R.id.action_change_password_on_server:
+				final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
+				changePasswordIntent.putExtra("account", mAccount.getJid().toString());
+				startActivity(changePasswordIntent);
+				break;
 		}
 		return super.onOptionsItemSelected(item);
 	}
@@ -445,18 +417,13 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 		}
 		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
 			this.mRegisterNew.setVisibility(View.VISIBLE);
-			this.mChangePassword.setVisibility(View.GONE);
-			this.mChangePassword.setChecked(false);
 			this.mRegisterNew.setChecked(true);
 			this.mPasswordConfirm.setText(this.mAccount.getPassword());
 		} else {
 			this.mRegisterNew.setVisibility(View.GONE);
 			this.mRegisterNew.setChecked(false);
-			this.mChangePassword.setVisibility(View.GONE);
-			this.mChangePassword.setChecked(false);
 		}
 		if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
-			toggleChangePasswordCheckbox();
 			this.mStats.setVisibility(View.VISIBLE);
 			this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
 						.getLastSessionEstablished()));
@@ -528,30 +495,4 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 			this.mStats.setVisibility(View.GONE);
 		}
 	}
-
-	@Override
-	public void onPasswordChangeSucceeded() {
-		this.mChangingPassword = false;
-		runOnUiThread(new Runnable() {
-			@Override
-			public void run() {
-				Toast.makeText(EditAccountActivity.this,R.string.password_changed,Toast.LENGTH_SHORT).show();
-				updateSaveButton();
-				updateAccountInformation();
-			}
-		});
-	}
-
-	@Override
-	public void onPasswordChangeFailed() {
-		this.mChangingPassword = false;
-		runOnUiThread(new Runnable() {
-			@Override
-			public void run() {
-				mPassword.requestFocus();
-				mPassword.setError(getString(R.string.could_not_change_password));
-				updateSaveButton();
-			}
-		});
-	}
 }

src/main/res/layout/activity_edit_account.xml 🔗

@@ -79,15 +79,6 @@
                     android:textColor="@color/primarytext"
                     android:textSize="?attr/TextSizeBody" />
 
-                <CheckBox
-                    android:id="@+id/account_change_password"
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    android:layout_marginTop="8dp"
-                    android:text="@string/change_password_on_server"
-                    android:textColor="@color/primarytext"
-                    android:textSize="?attr/TextSizeBody" />
-
                 <TextView
                     android:id="@+id/account_confirm_password_desc"
                     android:layout_width="wrap_content"

src/main/res/menu/editaccount.xml 🔗

@@ -16,4 +16,8 @@
         android:checkable="true"
         android:checked="false"
         android:showAsAction="never" />
+
+	<item android:id="@+id/action_change_password_on_server"
+          android:title="@string/change_password"
+          android:showAsAction="never" />
 </menu>

src/main/res/values/strings.xml 🔗

@@ -399,4 +399,8 @@
     <string name="shared_secret_hint_should_not_be_empty">Your hint should not be empty</string>
     <string name="shared_secret_can_not_be_empty">Your shared secret can not be empty</string>
     <string name="manual_verification_explanation">Carefully compare the fingerprint shown below with the fingerprint of your contact.\nYou can use any trusted form of communication like an encrypted e-mail or a telephone call to exchange those.</string>
+	<string name="change_password">Change password</string>
+	<string name="current_password">Current password</string>
+	<string name="new_password">New password</string>
+	<string name="password_should_not_be_empty">Password should not be empty</string>
 </resources>