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 com.google.android.material.textfield.TextInputLayout;
 11
 12import eu.siacs.conversations.R;
 13import eu.siacs.conversations.entities.Account;
 14import eu.siacs.conversations.services.XmppConnectionService;
 15import eu.siacs.conversations.ui.widget.DisabledActionModeCallback;
 16
 17public class ChangePasswordActivity extends XmppActivity implements XmppConnectionService.OnAccountPasswordChanged {
 18
 19	private Button mChangePasswordButton;
 20	private final View.OnClickListener mOnChangePasswordButtonClicked = new View.OnClickListener() {
 21		@Override
 22		public void onClick(View view) {
 23			if (mAccount != null) {
 24				final String currentPassword = mCurrentPassword.getText().toString();
 25				final String newPassword = mNewPassword.getText().toString();
 26				if (!(mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) || didUnlock) && !currentPassword.equals(mAccount.getPassword())) {
 27					mCurrentPassword.requestFocus();
 28					mCurrentPasswordLayout.setError(getString(R.string.account_status_unauthorized));
 29					removeErrorsOnAllBut(mCurrentPasswordLayout);
 30				} else if (newPassword.trim().isEmpty()) {
 31					mNewPassword.requestFocus();
 32					mNewPasswordLayout.setError(getString(R.string.password_should_not_be_empty));
 33					removeErrorsOnAllBut(mNewPasswordLayout);
 34				} else {
 35					mCurrentPasswordLayout.setError(null);
 36					mNewPasswordLayout.setError(null);
 37					xmppConnectionService.updateAccountPasswordOnServer(mAccount, newPassword, ChangePasswordActivity.this);
 38					mChangePasswordButton.setEnabled(false);
 39					mChangePasswordButton.setText(R.string.updating);
 40				}
 41			}
 42		}
 43	};
 44	private EditText mCurrentPassword;
 45	private EditText mNewPassword;
 46	private TextInputLayout mNewPasswordLayout;
 47	private TextInputLayout mCurrentPasswordLayout;
 48	private Account mAccount;
 49	private boolean didUnlock = false;
 50
 51	@Override
 52	void onBackendConnected() {
 53		this.mAccount = extractAccount(getIntent());
 54		if (this.mAccount != null && (this.mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) || didUnlock)) {
 55			this.mCurrentPasswordLayout.setVisibility(View.GONE);
 56		} else {
 57			this.mCurrentPassword.setVisibility(View.VISIBLE);
 58		}
 59	}
 60
 61	@Override
 62	protected void onCreate(final Bundle savedInstanceState) {
 63		super.onCreate(savedInstanceState);
 64		setContentView(R.layout.activity_change_password);
 65		setSupportActionBar(findViewById(R.id.toolbar));
 66		configureActionBar(getSupportActionBar());
 67		Button mCancelButton = findViewById(R.id.left_button);
 68		mCancelButton.setOnClickListener(view -> finish());
 69		this.mChangePasswordButton = findViewById(R.id.right_button);
 70		this.mChangePasswordButton.setOnClickListener(this.mOnChangePasswordButtonClicked);
 71		this.mCurrentPassword = findViewById(R.id.current_password);
 72		this.mCurrentPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
 73		this.mNewPassword = findViewById(R.id.new_password);
 74		this.mNewPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
 75		this.mCurrentPasswordLayout = findViewById(R.id.current_password_layout);
 76		this.mNewPasswordLayout = findViewById(R.id.new_password_layout);
 77	}
 78
 79	@Override
 80	protected void onStart() {
 81		super.onStart();
 82		Intent intent = getIntent();
 83		this.didUnlock = intent.getBooleanExtra("did_unlock", false);
 84		String password = intent != null ? intent.getStringExtra("password") : null;
 85		if (password != null) {
 86			this.mNewPassword.getEditableText().clear();
 87			this.mNewPassword.getEditableText().append(password);
 88		}
 89	}
 90
 91	@Override
 92	public void onPasswordChangeSucceeded() {
 93		runOnUiThread(() -> {
 94			Toast.makeText(ChangePasswordActivity.this,R.string.password_changed,Toast.LENGTH_LONG).show();
 95			finish();
 96		});
 97	}
 98
 99	@Override
100	public void onPasswordChangeFailed() {
101		runOnUiThread(() -> {
102			mNewPasswordLayout.setError(getString(R.string.could_not_change_password));
103			mChangePasswordButton.setEnabled(true);
104			mChangePasswordButton.setText(R.string.change_password);
105		});
106
107	}
108
109	private void removeErrorsOnAllBut(TextInputLayout exception) {
110		if (this.mCurrentPasswordLayout != exception) {
111			this.mCurrentPasswordLayout.setErrorEnabled(false);
112			this.mCurrentPasswordLayout.setError(null);
113		}
114		if (this.mNewPasswordLayout != exception) {
115			this.mNewPasswordLayout.setErrorEnabled(false);
116			this.mNewPasswordLayout.setError(null);
117		}
118
119	}
120
121	public void refreshUiReal() {
122
123	}
124}