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