added callback to change account password to notify UI on success / failure

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/generator/IqGenerator.java          |  2 
src/main/java/eu/siacs/conversations/services/XmppConnectionService.java | 30 
src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java         | 58 
src/main/res/values/strings.xml                                          |  3 
4 files changed, 66 insertions(+), 27 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/generator/IqGenerator.java πŸ”—

@@ -144,7 +144,7 @@ public class IqGenerator extends AbstractGenerator {
 		packet.setTo(account.getServer());
 		final Element query = packet.addChild("query", Xmlns.REGISTER);
 		final Jid jid = account.getJid();
-		query.addChild("username").setContent(jid.isDomainJid() ? jid.toString() : jid.getLocalpart());
+		query.addChild("username").setContent(jid.getLocalpart());
 		query.addChild("password").setContent(newPassword);
 		return packet;
 	}

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java πŸ”—

@@ -1100,19 +1100,25 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
 		getNotificationService().updateErrorNotification();
 	}
 
-	public void updateAccountPasswordOnServer(final Account account, final String newPassword) {
-		if (account.isOnlineAndConnected()) {
-			final IqPacket iq = getIqGenerator().generateSetPassword(account, newPassword);
-			sendIqPacket(account, iq, new OnIqPacketReceived() {
-				@Override
-				public void onIqPacketReceived(final Account account, final IqPacket packet) {
-					if (packet.getType() == IqPacket.TYPE_RESULT) {
-						account.setPassword(newPassword);
-						updateAccount(account);
-					}
+	public void updateAccountPasswordOnServer(final Account account, final String newPassword, final OnAccountPasswordChanged callback) {
+		final IqPacket iq = getIqGenerator().generateSetPassword(account, newPassword);
+		sendIqPacket(account, iq, new OnIqPacketReceived() {
+			@Override
+			public void onIqPacketReceived(final Account account, final IqPacket packet) {
+				if (packet.getType() == IqPacket.TYPE_RESULT) {
+					account.setPassword(newPassword);
+					databaseBackend.updateAccount(account);
+					callback.onPasswordChangeSucceeded();
+				} else {
+					callback.onPasswordChangeFailed();
 				}
-			});
-		}
+			}
+		});
+	}
+
+	public interface OnAccountPasswordChanged {
+		public void onPasswordChangeSucceeded();
+		public void onPasswordChangeFailed();
 	}
 
 	public void deleteAccount(final Account account) {

src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java πŸ”—

@@ -25,6 +25,7 @@ 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;
@@ -34,7 +35,7 @@ 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 {
+public class EditAccountActivity extends XmppActivity implements OnAccountUpdate, XmppConnectionService.OnAccountPasswordChanged {
 
 	private AutoCompleteTextView mAccountJid;
 	private EditText mPassword;
@@ -63,17 +64,17 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 	private Account mAccount;
 
 	private boolean mFetchingAvatar = false;
+	private boolean mChangingPassword = false;
 
 	private final OnClickListener mSaveButtonClickListener = new OnClickListener() {
 
 		@Override
 		public void onClick(final View v) {
-			if (mAccount != null
-					&& mAccount.getStatus() == Account.State.DISABLED) {
+			if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED) {
 				mAccount.setOption(Account.OPTION_DISABLED, false);
 				xmppConnectionService.updateAccount(mAccount);
 				return;
-					}
+			}
 			final boolean registerNewAccount = mRegisterNew.isChecked();
 			final boolean changePassword = mChangePassword.isChecked();
 			final Jid jid;
@@ -107,10 +108,13 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 				}
 				if (changePassword) {
 					if (mAccount.isOnlineAndConnected()) {
-						xmppConnectionService.updateAccountPasswordOnServer(mAccount, mPassword.getText().toString());
+						xmppConnectionService.updateAccountPasswordOnServer(mAccount, mPassword.getText().toString(),EditAccountActivity.this);
+						mChangingPassword = true;
+						updateSaveButton();
 					} else {
-						mPassword.setError(getResources().getString(R.string.account_status_no_internet));
+						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);
@@ -119,8 +123,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 			} else {
 				try {
 					if (xmppConnectionService.findAccountByJid(Jid.fromString(mAccountJid.getText().toString())) != null) {
-						mAccountJid
-							.setError(getString(R.string.account_already_exists));
+						mAccountJid.setError(getString(R.string.account_already_exists));
 						mAccountJid.requestFocus();
 						return;
 					}
@@ -253,13 +256,15 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 	}
 
 	protected void updateSaveButton() {
-		if (mAccount != null
-				&& mAccount.getStatus() == Account.State.CONNECTING) {
+		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) {
 			this.mSaveButton.setEnabled(false);
 			this.mSaveButton.setTextColor(getSecondaryTextColor());
 			this.mSaveButton.setText(R.string.account_status_connecting);
-		} else if (mAccount != null
-				&& mAccount.getStatus() == Account.State.DISABLED) {
+		} else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED) {
 			this.mSaveButton.setEnabled(true);
 			this.mSaveButton.setTextColor(getPrimaryTextColor());
 			this.mSaveButton.setText(R.string.enable);
@@ -446,8 +451,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 		if (this.mAccount.getStatus() == Account.State.ONLINE
 				&& !this.mFetchingAvatar) {
 			this.mStats.setVisibility(View.VISIBLE);
-			this.mSessionEst.setText(UIHelper.readableTimeDifference(
-						getApplicationContext(), this.mAccount.getXmppConnection()
+			this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
 						.getLastSessionEstablished()));
 			Features features = this.mAccount.getXmppConnection().getFeatures();
 			if (features.rosterVersioning()) {
@@ -517,4 +521,30 @@ 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/values/strings.xml πŸ”—

@@ -389,4 +389,7 @@
     <string name="copy_otr_clipboard_description">Copy OTR fingerprint to clipboard</string>
     <string name="fetching_history_from_server">Fetching history from server</string>
     <string name="no_more_history_on_server">No more history on server</string>
+    <string name="updating">Updating…</string>
+    <string name="password_changed">Password changed!</string>
+    <string name="could_not_change_password">Could not change password</string>
 </resources>