aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/ChangePasswordActivity.java
blob: df911344879c8528f7f794ea154b602f5397ac35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package de.pixart.messenger.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import de.pixart.messenger.R;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.services.XmppConnectionService;
import de.pixart.messenger.ui.widget.DisabledActionModeCallback;
import de.pixart.messenger.ui.widget.TextInputEditText;

public class ChangePasswordActivity extends XmppActivity implements XmppConnectionService.OnAccountPasswordChanged {

    private Button mChangePasswordButton;
    private View.OnClickListener mOnChangePasswordButtonClicked = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mAccount != null) {
                final String currentPassword = mCurrentPassword.getText().toString();
                final String newPassword = mNewPassword.getText().toString();
                if (!mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && !currentPassword.equals(mAccount.getPassword())) {
                    mCurrentPassword.requestFocus();
                    mCurrentPasswordLayout.setError(getString(R.string.account_status_unauthorized));
                    removeErrorsOnAllBut(mCurrentPasswordLayout);
                } else if (newPassword.trim().isEmpty()) {
                    mNewPassword.requestFocus();
                    mNewPasswordLayout.setError(getString(R.string.password_should_not_be_empty));
                    removeErrorsOnAllBut(mNewPasswordLayout);
                } else {
                    mCurrentPasswordLayout.setError(null);
                    mNewPasswordLayout.setError(null);
                    xmppConnectionService.updateAccountPasswordOnServer(mAccount, newPassword, ChangePasswordActivity.this);
                    mChangePasswordButton.setEnabled(false);
                    mChangePasswordButton.setText(R.string.updating);
                }
            }
        }
    };
    private TextInputEditText mCurrentPassword;
    private TextInputEditText mNewPassword;
    private TextInputLayout mNewPasswordLayout;
    private TextInputLayout mCurrentPasswordLayout;
    private Account mAccount;

    @Override
    void onBackendConnected() {
        this.mAccount = extractAccount(getIntent());
        if (this.mAccount != null && this.mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)) {
            this.mCurrentPasswordLayout.setVisibility(View.GONE);
        } else {
            this.mCurrentPassword.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        setSupportActionBar(findViewById(R.id.toolbar));
        configureActionBar(getSupportActionBar());
        Button mCancelButton = findViewById(R.id.left_button);
        mCancelButton.setOnClickListener(view -> finish());
        this.mChangePasswordButton = findViewById(R.id.right_button);
        this.mChangePasswordButton.setOnClickListener(this.mOnChangePasswordButtonClicked);
        this.mCurrentPassword = findViewById(R.id.current_password);
        this.mCurrentPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
        this.mNewPassword = findViewById(R.id.new_password);
        this.mNewPassword.setCustomSelectionActionModeCallback(new DisabledActionModeCallback());
        this.mCurrentPasswordLayout = findViewById(R.id.current_password_layout);
        this.mNewPasswordLayout = findViewById(R.id.new_password_layout);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = getIntent();
        String password = intent != null ? intent.getStringExtra("password") : null;
        if (password != null) {
            this.mNewPassword.getEditableText().clear();
            this.mNewPassword.getEditableText().append(password);
        }
    }

    @Override
    public void onPasswordChangeSucceeded() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(ChangePasswordActivity.this, R.string.password_changed, Toast.LENGTH_LONG).show();
                finish();
            }
        });
    }

    @Override
    public void onPasswordChangeFailed() {
        runOnUiThread(() -> {
            mNewPasswordLayout.setError(getString(R.string.could_not_change_password));
            mChangePasswordButton.setEnabled(true);
            mChangePasswordButton.setText(R.string.change_password);
        });

    }

    private void removeErrorsOnAllBut(TextInputLayout exception) {
        if (this.mCurrentPasswordLayout != exception) {
            this.mCurrentPasswordLayout.setErrorEnabled(false);
            this.mCurrentPasswordLayout.setError(null);
        }
        if (this.mNewPasswordLayout != exception) {
            this.mNewPasswordLayout.setErrorEnabled(false);
            this.mNewPasswordLayout.setError(null);
        }

    }

    public void refreshUiReal() {

    }
}