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;
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 mCurrentPassword.setError(getString(R.string.account_status_unauthorized));
28 } else if (newPassword.trim().isEmpty()) {
29 mNewPassword.requestFocus();
30 mNewPassword.setError(getString(R.string.password_should_not_be_empty));
31 } else {
32 mCurrentPassword.setError(null);
33 mNewPassword.setError(null);
34 xmppConnectionService.updateAccountPasswordOnServer(mAccount, newPassword, ChangePasswordActivity.this);
35 mChangePasswordButton.setEnabled(false);
36 mChangePasswordButton.setTextColor(getSecondaryTextColor());
37 mChangePasswordButton.setText(R.string.updating);
38 }
39 }
40 }
41 };
42 private TextView mCurrentPasswordLabel;
43 private EditText mCurrentPassword;
44 private EditText mNewPassword;
45 private Account mAccount;
46
47 @Override
48 void onBackendConnected() {
49 this.mAccount = extractAccount(getIntent());
50 if (this.mAccount != null && this.mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)) {
51 this.mCurrentPasswordLabel.setVisibility(View.GONE);
52 this.mCurrentPassword.setVisibility(View.GONE);
53 } else {
54 this.mCurrentPasswordLabel.setVisibility(View.VISIBLE);
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.mCurrentPasswordLabel = findViewById(R.id.current_password_label);
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 }
73
74 @Override
75 protected void onStart() {
76 super.onStart();
77 Intent intent = getIntent();
78 String password = intent != null ? intent.getStringExtra("password") : null;
79 if (password != null) {
80 this.mNewPassword.getEditableText().clear();
81 this.mNewPassword.getEditableText().append(password);
82 }
83 }
84
85 @Override
86 public void onPasswordChangeSucceeded() {
87 runOnUiThread(() -> {
88 Toast.makeText(ChangePasswordActivity.this,R.string.password_changed,Toast.LENGTH_LONG).show();
89 finish();
90 });
91 }
92
93 @Override
94 public void onPasswordChangeFailed() {
95 runOnUiThread(() -> {
96 mNewPassword.setError(getString(R.string.could_not_change_password));
97 mChangePasswordButton.setEnabled(true);
98 mChangePasswordButton.setTextColor(getPrimaryTextColor());
99 mChangePasswordButton.setText(R.string.change_password);
100 });
101
102 }
103
104 public void refreshUiReal() {
105
106 }
107}