18
18
19
19
import androidx .annotation .NonNull ;
20
20
import androidx .annotation .Nullable ;
21
- import androidx .annotation .StringRes ;
22
21
import androidx .appcompat .app .AlertDialog ;
23
22
import androidx .appcompat .app .AppCompatActivity ;
24
23
import androidx .appcompat .widget .AppCompatButton ;
41
40
import static com .simperium .android .AuthenticationActivity .EXTRA_IS_LOGIN ;
42
41
43
42
public class CredentialsActivity extends AppCompatActivity {
44
- private static final Pattern PATTERN_PASSWORD = Pattern .compile ("^(.){4,}$" , Pattern .DOTALL );
45
43
private static final Pattern PATTERN_WHITESPACE = Pattern .compile ("(\\ s)" );
46
44
private static final String EXTRA_AUTOMATE_LOGIN = "EXTRA_AUTOMATE_LOGIN" ;
47
45
private static final String EXTRA_PASSWORD = "EXTRA_PASSWORD" ;
48
46
private static final String STATE_EMAIL = "STATE_EMAIL" ;
49
47
private static final String STATE_PASSWORD = "STATE_PASSWORD" ;
50
48
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 ;
51
51
52
52
protected ProgressDialogFragment mProgressDialogFragment ;
53
53
@@ -70,11 +70,11 @@ public void run() {
70
70
break ;
71
71
case INVALID_ACCOUNT :
72
72
default :
73
- showDialogError (
73
+ showDialogError (getString (
74
74
mIsLogin ?
75
75
R .string .simperium_dialog_message_login :
76
76
R .string .simperium_dialog_message_signup
77
- );
77
+ )) ;
78
78
}
79
79
80
80
Logger .log (error .getMessage (), error );
@@ -165,7 +165,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
165
165
new View .OnFocusChangeListener () {
166
166
@ Override
167
167
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 ())) {
169
169
mInputEmail .setError (getString (R .string .simperium_error_email ));
170
170
} else {
171
171
mInputEmail .setError ("" );
@@ -202,10 +202,10 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
202
202
new View .OnFocusChangeListener () {
203
203
@ Override
204
204
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 ) {
208
206
mInputPassword .setError ("" );
207
+ } else if (!isValidPasswordLength ()) {
208
+ mInputPassword .setError (getString (R .string .simperium_error_password ));
209
209
}
210
210
}
211
211
}
@@ -228,7 +228,7 @@ public void onClick(View view) {
228
228
startSignup ();
229
229
}
230
230
} else {
231
- showDialogError (R .string .simperium_dialog_message_network );
231
+ showDialogError (getString ( R .string .simperium_dialog_message_network ) );
232
232
}
233
233
}
234
234
}
@@ -311,20 +311,28 @@ private void hideDialogProgress() {
311
311
}
312
312
}
313
313
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 ();
316
316
}
317
-
317
+
318
318
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
+ );
320
328
}
321
329
322
330
private void setButtonState () {
323
331
mButton .setEnabled (
324
332
mInputEmail .getEditText () != null &&
325
333
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 ( )
328
336
);
329
337
}
330
338
@@ -334,7 +342,7 @@ private void setEditTextString(@NonNull TextInputLayout inputLayout, String text
334
342
}
335
343
}
336
344
337
- private void showDialogError (@ StringRes int message ) {
345
+ private void showDialogError (String message ) {
338
346
hideDialogProgress ();
339
347
Context context = new ContextThemeWrapper (CredentialsActivity .this , getTheme ());
340
348
new AlertDialog .Builder (context )
@@ -378,7 +386,7 @@ private void startLogin() {
378
386
mProgressDialogFragment .show (getSupportFragmentManager (), ProgressDialogFragment .TAG );
379
387
mSimperium .authorizeUser (email , password , mAuthListener );
380
388
} else {
381
- showDialogError (R .string .simperium_dialog_message_password );
389
+ showDialogError (getString ( R .string .simperium_dialog_message_password , PASSWORD_LENGTH_LOGIN ) );
382
390
}
383
391
}
384
392
@@ -392,7 +400,7 @@ private void startSignup() {
392
400
mProgressDialogFragment .show (getSupportFragmentManager (), ProgressDialogFragment .TAG );
393
401
mSimperium .createUser (email , password , mAuthListener );
394
402
} else {
395
- showDialogError (R .string .simperium_dialog_message_password );
403
+ showDialogError (getString ( R .string .simperium_dialog_message_password , PASSWORD_LENGTH_SIGNUP ) );
396
404
}
397
405
}
398
406
}
0 commit comments