ChangePasswordActivity.java

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