ChangePasswordActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.content.Intent;
  4import android.os.Bundle;
  5import android.view.View;
  6import android.widget.Button;
  7import android.widget.EditText;
  8import android.widget.Toast;
  9
 10import eu.siacs.conversations.R;
 11import eu.siacs.conversations.entities.Account;
 12import eu.siacs.conversations.services.XmppConnectionService;
 13import eu.siacs.conversations.ui.widget.DisabledActionModeCallback;
 14
 15public class ChangePasswordActivity extends XmppActivity implements XmppConnectionService.OnAccountPasswordChanged {
 16
 17	private Button mChangePasswordButton;
 18	private View.OnClickListener mOnChangePasswordButtonClicked = new View.OnClickListener() {
 19		@Override
 20		public void onClick(View view) {
 21			if (mAccount != null) {
 22				final String currentPassword = mCurrentPassword.getText().toString();
 23				final String newPassword = mNewPassword.getText().toString();
 24				if (!mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && !currentPassword.equals(mAccount.getPassword())) {
 25					mCurrentPassword.requestFocus();
 26					mCurrentPassword.setError(getString(R.string.account_status_unauthorized));
 27				} else if (newPassword.trim().isEmpty()) {
 28					mNewPassword.requestFocus();
 29					mNewPassword.setError(getString(R.string.password_should_not_be_empty));
 30				} else {
 31					mCurrentPassword.setError(null);
 32					mNewPassword.setError(null);
 33					xmppConnectionService.updateAccountPasswordOnServer(mAccount, newPassword, ChangePasswordActivity.this);
 34					mChangePasswordButton.setEnabled(false);
 35					mChangePasswordButton.setTextColor(getSecondaryTextColor());
 36					mChangePasswordButton.setText(R.string.updating);
 37				}
 38			}
 39		}
 40	};
 41	private EditText mCurrentPassword;
 42	private EditText mNewPassword;
 43	private Account mAccount;
 44
 45	@Override
 46	void onBackendConnected() {
 47		this.mAccount = extractAccount(getIntent());
 48		if (this.mAccount != null && this.mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)) {
 49			this.mCurrentPassword.setVisibility(View.GONE);
 50		} else {
 51			this.mCurrentPassword.setVisibility(View.VISIBLE);
 52		}
 53	}
 54
 55	@Override
 56	protected void onCreate(final Bundle savedInstanceState) {
 57		super.onCreate(savedInstanceState);
 58		setContentView(R.layout.activity_change_password);
 59		Button mCancelButton = findViewById(R.id.left_button);
 60		mCancelButton.setOnClickListener(view -> finish());
 61		this.mChangePasswordButton = findViewById(R.id.right_button);
 62		this.mChangePasswordButton.setOnClickListener(this.mOnChangePasswordButtonClicked);
 63		this.mCurrentPassword = findViewById(R.id.current_password);
 64		this.mCurrentPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
 65		this.mNewPassword = findViewById(R.id.new_password);
 66		this.mNewPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
 67	}
 68
 69	@Override
 70	protected void onStart() {
 71		super.onStart();
 72		Intent intent = getIntent();
 73		String password = intent != null ? intent.getStringExtra("password") : null;
 74		if (password != null) {
 75			this.mNewPassword.getEditableText().clear();
 76			this.mNewPassword.getEditableText().append(password);
 77		}
 78	}
 79
 80	@Override
 81	public void onPasswordChangeSucceeded() {
 82		runOnUiThread(() -> {
 83			Toast.makeText(ChangePasswordActivity.this,R.string.password_changed,Toast.LENGTH_LONG).show();
 84			finish();
 85		});
 86	}
 87
 88	@Override
 89	public void onPasswordChangeFailed() {
 90		runOnUiThread(() -> {
 91			mNewPassword.setError(getString(R.string.could_not_change_password));
 92			mChangePasswordButton.setEnabled(true);
 93			mChangePasswordButton.setTextColor(getPrimaryTextColor());
 94			mChangePasswordButton.setText(R.string.change_password);
 95		});
 96
 97	}
 98
 99	public void refreshUiReal() {
100
101	}
102}