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.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		Button mCancelButton = findViewById(R.id.left_button);
 64		mCancelButton.setOnClickListener(view -> finish());
 65		this.mChangePasswordButton = findViewById(R.id.right_button);
 66		this.mChangePasswordButton.setOnClickListener(this.mOnChangePasswordButtonClicked);
 67		this.mCurrentPassword = findViewById(R.id.current_password);
 68		this.mCurrentPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
 69		this.mNewPassword = findViewById(R.id.new_password);
 70		this.mNewPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
 71		this.mCurrentPasswordLayout = findViewById(R.id.current_password_layout);
 72		this.mNewPasswordLayout = findViewById(R.id.new_password_layout);
 73	}
 74
 75	@Override
 76	protected void onStart() {
 77		super.onStart();
 78		Intent intent = getIntent();
 79		String password = intent != null ? intent.getStringExtra("password") : null;
 80		if (password != null) {
 81			this.mNewPassword.getEditableText().clear();
 82			this.mNewPassword.getEditableText().append(password);
 83		}
 84	}
 85
 86	@Override
 87	public void onPasswordChangeSucceeded() {
 88		runOnUiThread(() -> {
 89			Toast.makeText(ChangePasswordActivity.this,R.string.password_changed,Toast.LENGTH_LONG).show();
 90			finish();
 91		});
 92	}
 93
 94	@Override
 95	public void onPasswordChangeFailed() {
 96		runOnUiThread(() -> {
 97			mNewPasswordLayout.setError(getString(R.string.could_not_change_password));
 98			mChangePasswordButton.setEnabled(true);
 99			mChangePasswordButton.setText(R.string.change_password);
100		});
101
102	}
103
104	private void removeErrorsOnAllBut(TextInputLayout exception) {
105		if (this.mCurrentPasswordLayout != exception) {
106			this.mCurrentPasswordLayout.setErrorEnabled(false);
107			this.mCurrentPasswordLayout.setError(null);
108		}
109		if (this.mNewPasswordLayout != exception) {
110			this.mNewPasswordLayout.setErrorEnabled(false);
111			this.mNewPasswordLayout.setError(null);
112		}
113
114	}
115
116	public void refreshUiReal() {
117
118	}
119}