Newer
Older
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package org.briarproject.android;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import org.briarproject.R;
import org.briarproject.android.controller.PasswordController;
import org.briarproject.android.controller.SetupController;
import org.briarproject.android.controller.handler.UiResultHandler;
import org.briarproject.android.util.AndroidUtils;
import org.briarproject.android.util.StrengthMeter;
import javax.inject.Inject;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
import static org.briarproject.api.crypto.PasswordStrengthEstimator.WEAK;
public class ChangePasswordActivity extends BaseActivity
implements OnClickListener,
OnEditorActionListener {
@Inject
protected PasswordController passwordController;
@Inject
protected SetupController setupController;
private TextInputLayout currentPasswordEntryWrapper;
private TextInputLayout newPasswordEntryWrapper;
private TextInputLayout newPasswordConfirmationWrapper;
private EditText currentPassword;
private EditText newPassword;
private EditText newPasswordConfirmation;
private StrengthMeter strengthMeter;
private Button changePasswordButton;
private ProgressBar progress;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_change_password);
currentPasswordEntryWrapper =
(TextInputLayout) findViewById(
R.id.current_password_entry_wrapper);
newPasswordEntryWrapper =
(TextInputLayout) findViewById(R.id.new_password_entry_wrapper);
newPasswordConfirmationWrapper =
(TextInputLayout) findViewById(
R.id.new_password_confirm_wrapper);
currentPassword = (EditText) findViewById(R.id.current_password_entry);
newPassword = (EditText) findViewById(R.id.new_password_entry);
newPasswordConfirmation =
(EditText) findViewById(R.id.new_password_confirm);
strengthMeter = (StrengthMeter) findViewById(R.id.strength_meter);
changePasswordButton = (Button) findViewById(R.id.change_password);
progress = (ProgressBar) findViewById(R.id.progress_wheel);
TextWatcher tw = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
enableOrDisableContinueButton();
}
@Override
public void afterTextChanged(Editable s) {
}
};
currentPassword.addTextChangedListener(tw);
newPassword.addTextChangedListener(tw);
newPasswordConfirmation.addTextChangedListener(tw);
newPasswordConfirmation.setOnEditorActionListener(this);
changePasswordButton.setOnClickListener(this);
}
@Override
public void injectActivity(ActivityComponent component) {
component.inject(this);
}
private void enableOrDisableContinueButton() {
if (progress == null) return; // Not created yet
if (newPassword.getText().length() > 0 && newPassword.hasFocus())
strengthMeter.setVisibility(VISIBLE);
else strengthMeter.setVisibility(INVISIBLE);
String firstPassword = newPassword.getText().toString();
String secondPassword = newPasswordConfirmation.getText().toString();
boolean passwordsMatch = firstPassword.equals(secondPassword);
float strength =
setupController.estimatePasswordStrength(firstPassword);
strengthMeter.setStrength(strength);
AndroidUtils.setError(newPasswordEntryWrapper,
getString(R.string.password_too_weak),
firstPassword.length() > 0 && strength < WEAK);
AndroidUtils.setError(newPasswordConfirmationWrapper,
getString(R.string.passwords_do_not_match),
secondPassword.length() > 0 && !passwordsMatch);
changePasswordButton.setEnabled(
!currentPassword.getText().toString().isEmpty() &&
passwordsMatch && strength >= WEAK);
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
hideSoftKeyboard(v);
return true;
}
@Override
public void onClick(View view) {
// Replace the button with a progress bar
changePasswordButton.setVisibility(INVISIBLE);
progress.setVisibility(VISIBLE);
passwordController.changePassword(currentPassword.getText().toString(),
newPassword.getText().toString(),
new UiResultHandler<Boolean>(this) {
@Override
public void onResultUi(@NonNull Boolean result) {
if (result) {
Toast.makeText(ChangePasswordActivity.this,
R.string.password_changed,
Toast.LENGTH_LONG).show();
setResult(RESULT_OK);
finish();
} else {
tryAgain();
}
}
});
}
private void tryAgain() {
AndroidUtils.setError(currentPasswordEntryWrapper,
getString(R.string.try_again), true);
changePasswordButton.setVisibility(VISIBLE);
progress.setVisibility(INVISIBLE);
currentPassword.setText("");
// show the keyboard again
showSoftKeyboard(currentPassword);
}
}