Skip to content

Commit 4a27d02

Browse files
authored
Merge pull request #210 from Simperium/update-password-length
Update Password Length
2 parents 01842d4 + b66d5e9 commit 4a27d02

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

Simperium/src/main/java/com/simperium/android/CredentialsActivity.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import androidx.annotation.NonNull;
2020
import androidx.annotation.Nullable;
21-
import androidx.annotation.StringRes;
2221
import androidx.appcompat.app.AlertDialog;
2322
import androidx.appcompat.app.AppCompatActivity;
2423
import androidx.appcompat.widget.AppCompatButton;
@@ -41,13 +40,14 @@
4140
import static com.simperium.android.AuthenticationActivity.EXTRA_IS_LOGIN;
4241

4342
public class CredentialsActivity extends AppCompatActivity {
44-
private static final Pattern PATTERN_PASSWORD = Pattern.compile("^(.){4,}$", Pattern.DOTALL);
4543
private static final Pattern PATTERN_WHITESPACE = Pattern.compile("(\\s)");
4644
private static final String EXTRA_AUTOMATE_LOGIN = "EXTRA_AUTOMATE_LOGIN";
4745
private static final String EXTRA_PASSWORD = "EXTRA_PASSWORD";
4846
private static final String STATE_EMAIL = "STATE_EMAIL";
4947
private static final String STATE_PASSWORD = "STATE_PASSWORD";
5048
private static final int DELAY_AUTOMATE_LOGIN = 600;
49+
private static final int PASSWORD_LENGTH_LOGIN = 4;
50+
private static final int PASSWORD_LENGTH_SIGNUP = 6;
5151

5252
protected ProgressDialogFragment mProgressDialogFragment;
5353

@@ -70,11 +70,11 @@ public void run() {
7070
break;
7171
case INVALID_ACCOUNT:
7272
default:
73-
showDialogError(
73+
showDialogError(getString(
7474
mIsLogin ?
7575
R.string.simperium_dialog_message_login :
7676
R.string.simperium_dialog_message_signup
77-
);
77+
));
7878
}
7979

8080
Logger.log(error.getMessage(), error);
@@ -165,7 +165,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
165165
new View.OnFocusChangeListener() {
166166
@Override
167167
public void onFocusChange(View view, boolean hasFocus) {
168-
if (!hasFocus && !isValid(Patterns.EMAIL_ADDRESS, mInputEmail.getEditText().getText().toString())) {
168+
if (!hasFocus && !isValidEmail(mInputEmail.getEditText().getText().toString())) {
169169
mInputEmail.setError(getString(R.string.simperium_error_email));
170170
} else {
171171
mInputEmail.setError("");
@@ -202,10 +202,10 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
202202
new View.OnFocusChangeListener() {
203203
@Override
204204
public void onFocusChange(View view, boolean hasFocus) {
205-
if (!hasFocus && !isValid(PATTERN_PASSWORD, mInputPassword.getEditText().getText().toString())) {
206-
mInputPassword.setError(getString(R.string.simperium_error_password));
207-
} else {
205+
if (hasFocus) {
208206
mInputPassword.setError("");
207+
} else if (!isValidPasswordLength()) {
208+
mInputPassword.setError(getString(R.string.simperium_error_password));
209209
}
210210
}
211211
}
@@ -228,7 +228,7 @@ public void onClick(View view) {
228228
startSignup();
229229
}
230230
} else {
231-
showDialogError(R.string.simperium_dialog_message_network);
231+
showDialogError(getString(R.string.simperium_dialog_message_network));
232232
}
233233
}
234234
}
@@ -311,20 +311,28 @@ private void hideDialogProgress() {
311311
}
312312
}
313313

314-
private boolean isValid(Pattern pattern, String text) {
315-
return pattern.matcher(text).matches();
314+
private boolean isValidEmail(String text) {
315+
return Patterns.EMAIL_ADDRESS.matcher(text).matches();
316316
}
317-
317+
318318
private boolean isValidPassword(String password) {
319-
return password.length() >= 4 && !PATTERN_WHITESPACE.matcher(password).find();
319+
return isValidPasswordLength() && !PATTERN_WHITESPACE.matcher(password).find();
320+
}
321+
322+
private boolean isValidPasswordLength() {
323+
return mInputPassword.getEditText() != null &&
324+
(mIsLogin ?
325+
mInputPassword.getEditText().getText().toString().length() >= PASSWORD_LENGTH_LOGIN :
326+
mInputPassword.getEditText().getText().toString().length() >= PASSWORD_LENGTH_SIGNUP
327+
);
320328
}
321329

322330
private void setButtonState() {
323331
mButton.setEnabled(
324332
mInputEmail.getEditText() != null &&
325333
mInputPassword.getEditText() != null &&
326-
isValid(Patterns.EMAIL_ADDRESS, mInputEmail.getEditText().getText().toString()) &&
327-
isValid(PATTERN_PASSWORD, mInputPassword.getEditText().getText().toString())
334+
isValidEmail(mInputEmail.getEditText().getText().toString()) &&
335+
isValidPasswordLength()
328336
);
329337
}
330338

@@ -334,7 +342,7 @@ private void setEditTextString(@NonNull TextInputLayout inputLayout, String text
334342
}
335343
}
336344

337-
private void showDialogError(@StringRes int message) {
345+
private void showDialogError(String message) {
338346
hideDialogProgress();
339347
Context context = new ContextThemeWrapper(CredentialsActivity.this, getTheme());
340348
new AlertDialog.Builder(context)
@@ -378,7 +386,7 @@ private void startLogin() {
378386
mProgressDialogFragment.show(getSupportFragmentManager(), ProgressDialogFragment.TAG);
379387
mSimperium.authorizeUser(email, password, mAuthListener);
380388
} else {
381-
showDialogError(R.string.simperium_dialog_message_password);
389+
showDialogError(getString(R.string.simperium_dialog_message_password, PASSWORD_LENGTH_LOGIN));
382390
}
383391
}
384392

@@ -392,7 +400,7 @@ private void startSignup() {
392400
mProgressDialogFragment.show(getSupportFragmentManager(), ProgressDialogFragment.TAG);
393401
mSimperium.createUser(email, password, mAuthListener);
394402
} else {
395-
showDialogError(R.string.simperium_dialog_message_password);
403+
showDialogError(getString(R.string.simperium_dialog_message_password, PASSWORD_LENGTH_SIGNUP));
396404
}
397405
}
398406
}

Simperium/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<string name="simperium_button_signup">Sign Up</string>
1111
<string name="simperium_dialog_message_login">Could not log in with the provided email address and password.</string>
1212
<string name="simperium_dialog_message_network">There is no network available. Please, connect to a network and try again.</string>
13-
<string name="simperium_dialog_message_password">Passwords must be a minimum of four characters excluding new lines, spaces, and tabs.</string>
13+
<string name="simperium_dialog_message_password">Passwords must be a minimum of %1$d characters excluding new lines, spaces, and tabs.</string>
1414
<string name="simperium_dialog_message_signup">Could not sign up with the provided email address and password.</string>
1515
<string name="simperium_dialog_message_signup_existing">The email address you entered is already associated with a Simperium account.</string>
1616
<string name="simperium_dialog_progress_logging_in">Logging in&#8230;</string>

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ def gitDescribe() {
3535
}
3636

3737
def static gitVersion() {
38-
'0.8.0'
38+
'0.9.0'
3939
}

0 commit comments

Comments
 (0)