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) && !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
 50	@Override
 51	void onBackendConnected() {
 52		this.mAccount = extractAccount(getIntent());
 53		if (this.mAccount != null && this.mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)) {
 54			this.mCurrentPasswordLayout.setVisibility(View.GONE);
 55		} else {
 56			this.mCurrentPassword.setVisibility(View.VISIBLE);
 57		}
 58	}
 59
 60	@Override
 61	protected void onCreate(final Bundle savedInstanceState) {
 62		super.onCreate(savedInstanceState);
 63		setContentView(R.layout.activity_change_password);
 64		setSupportActionBar(findViewById(R.id.toolbar));
 65		configureActionBar(getSupportActionBar());
 66		Button mCancelButton = findViewById(R.id.left_button);
 67		mCancelButton.setOnClickListener(view -> finish());
 68		this.mChangePasswordButton = findViewById(R.id.right_button);
 69		this.mChangePasswordButton.setOnClickListener(this.mOnChangePasswordButtonClicked);
 70		this.mCurrentPassword = findViewById(R.id.current_password);
 71		this.mCurrentPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
 72		this.mNewPassword = findViewById(R.id.new_password);
 73		this.mNewPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
 74		this.mCurrentPasswordLayout = findViewById(R.id.current_password_layout);
 75		this.mNewPasswordLayout = findViewById(R.id.new_password_layout);
 76	}
 77
 78	@Override
 79	protected void onStart() {
 80		super.onStart();
 81		Intent intent = getIntent();
 82		String password = intent != null ? intent.getStringExtra("password") : null;
 83		if (password != null) {
 84			this.mNewPassword.getEditableText().clear();
 85			this.mNewPassword.getEditableText().append(password);
 86		}
 87	}
 88
 89	@Override
 90	public void onPasswordChangeSucceeded() {
 91		runOnUiThread(() -> {
 92			Toast.makeText(ChangePasswordActivity.this,R.string.password_changed,Toast.LENGTH_LONG).show();
 93			finish();
 94		});
 95	}
 96
 97	@Override
 98	public void onPasswordChangeFailed() {
 99		runOnUiThread(() -> {
100			mNewPasswordLayout.setError(getString(R.string.could_not_change_password));
101			mChangePasswordButton.setEnabled(true);
102			mChangePasswordButton.setText(R.string.change_password);
103		});
104
105	}
106
107	private void removeErrorsOnAllBut(TextInputLayout exception) {
108		if (this.mCurrentPasswordLayout != exception) {
109			this.mCurrentPasswordLayout.setErrorEnabled(false);
110			this.mCurrentPasswordLayout.setError(null);
111		}
112		if (this.mNewPasswordLayout != exception) {
113			this.mNewPasswordLayout.setErrorEnabled(false);
114			this.mNewPasswordLayout.setError(null);
115		}
116
117	}
118
119	public void refreshUiReal() {
120
121	}
122}