ChangePasswordActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.content.Intent;
  4import android.os.Bundle;
  5import android.support.design.widget.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.setTextColor(getSecondaryTextColor());
 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.mCurrentPassword.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		Button mCancelButton = findViewById(R.id.left_button);
 65		mCancelButton.setOnClickListener(view -> finish());
 66		this.mChangePasswordButton = findViewById(R.id.right_button);
 67		this.mChangePasswordButton.setOnClickListener(this.mOnChangePasswordButtonClicked);
 68		this.mCurrentPassword = findViewById(R.id.current_password);
 69		this.mCurrentPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
 70		this.mNewPassword = findViewById(R.id.new_password);
 71		this.mNewPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
 72		this.mCurrentPasswordLayout = findViewById(R.id.current_password_layout);
 73		this.mNewPasswordLayout = findViewById(R.id.new_password_layout);
 74	}
 75
 76	@Override
 77	protected void onStart() {
 78		super.onStart();
 79		Intent intent = getIntent();
 80		String password = intent != null ? intent.getStringExtra("password") : null;
 81		if (password != null) {
 82			this.mNewPassword.getEditableText().clear();
 83			this.mNewPassword.getEditableText().append(password);
 84		}
 85	}
 86
 87	@Override
 88	public void onPasswordChangeSucceeded() {
 89		runOnUiThread(() -> {
 90			Toast.makeText(ChangePasswordActivity.this,R.string.password_changed,Toast.LENGTH_LONG).show();
 91			finish();
 92		});
 93	}
 94
 95	@Override
 96	public void onPasswordChangeFailed() {
 97		runOnUiThread(() -> {
 98			mNewPasswordLayout.setError(getString(R.string.could_not_change_password));
 99			mChangePasswordButton.setEnabled(true);
100			mChangePasswordButton.setTextColor(getPrimaryTextColor());
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}