From 0c22a8d1c6a984ccd0080d74a07d500879ee8242 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 25 Dec 2014 22:08:13 +0100 Subject: [PATCH] added callback to change account password to notify UI on success / failure --- .../conversations/generator/IqGenerator.java | 2 +- .../services/XmppConnectionService.java | 30 ++++++---- .../conversations/ui/EditAccountActivity.java | 58 ++++++++++++++----- src/main/res/values/strings.xml | 3 + 4 files changed, 66 insertions(+), 27 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/generator/IqGenerator.java b/src/main/java/eu/siacs/conversations/generator/IqGenerator.java index 4bfc0963d..f94dc5d7d 100644 --- a/src/main/java/eu/siacs/conversations/generator/IqGenerator.java +++ b/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; } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 640074ba6..dc895e5b6 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/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) { diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index 4b6bea09b..3bd4dd1eb 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/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(); + } + }); + } } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 22e39a89d..6634c96a4 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -389,4 +389,7 @@ Copy OTR fingerprint to clipboard Fetching history from server No more history on server + Updating… + Password changed! + Could not change password