14
14
import android .view .View ;
15
15
import android .view .ViewGroup ;
16
16
import android .view .inputmethod .InputMethodManager ;
17
- import android .widget .Button ;
18
- import android .widget .EditText ;
19
- import android .widget .TextView ;
20
17
18
+ import android .widget .TextView ;
21
19
import androidx .annotation .ColorRes ;
22
20
import androidx .annotation .NonNull ;
23
21
import androidx .annotation .Nullable ;
27
25
import androidx .core .app .NavUtils ;
28
26
import androidx .core .content .ContextCompat ;
29
27
30
- import com .google .android .material .textfield .TextInputLayout ;
31
-
28
+ import fr .free .nrw .commons .databinding .ActivityLoginBinding ;
32
29
import fr .free .nrw .commons .utils .ActivityUtils ;
33
30
import java .util .Locale ;
34
31
import org .wikipedia .AppAdapter ;
42
39
import javax .inject .Inject ;
43
40
import javax .inject .Named ;
44
41
45
- import butterknife .BindView ;
46
- import butterknife .ButterKnife ;
47
- import butterknife .OnClick ;
48
- import butterknife .OnEditorAction ;
49
- import butterknife .OnFocusChange ;
50
42
import fr .free .nrw .commons .BuildConfig ;
51
43
import fr .free .nrw .commons .R ;
52
44
import fr .free .nrw .commons .Utils ;
53
- import fr .free .nrw .commons .WelcomeActivity ;
54
45
import fr .free .nrw .commons .contributions .MainActivity ;
55
46
import fr .free .nrw .commons .di .ApplicationlessInjection ;
56
47
import fr .free .nrw .commons .kvstore .JsonKvStore ;
@@ -87,30 +78,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
87
78
@ Inject
88
79
SystemThemeUtils systemThemeUtils ;
89
80
90
- @ BindView (R .id .login_button )
91
- Button loginButton ;
92
-
93
- @ BindView (R .id .login_username )
94
- EditText usernameEdit ;
95
-
96
- @ BindView (R .id .login_password )
97
- EditText passwordEdit ;
98
-
99
- @ BindView (R .id .login_two_factor )
100
- EditText twoFactorEdit ;
101
-
102
- @ BindView (R .id .error_message_container )
103
- ViewGroup errorMessageContainer ;
104
-
105
- @ BindView (R .id .error_message )
106
- TextView errorMessage ;
107
-
108
- @ BindView (R .id .login_credentials )
109
- TextView loginCredentials ;
110
-
111
- @ BindView (R .id .two_factor_container )
112
- TextInputLayout twoFactorContainer ;
113
-
81
+ private ActivityLoginBinding binding ;
114
82
ProgressDialog progressDialog ;
115
83
private AppCompatDelegate delegate ;
116
84
private LoginTextWatcher textWatcher = new LoginTextWatcher ();
@@ -120,6 +88,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
120
88
final String saveErrorMessage ="errorMessage" ;
121
89
final String saveUsername ="username" ;
122
90
final String savePassword ="password" ;
91
+
123
92
@ Override
124
93
public void onCreate (Bundle savedInstanceState ) {
125
94
super .onCreate (savedInstanceState );
@@ -133,35 +102,41 @@ public void onCreate(Bundle savedInstanceState) {
133
102
getDelegate ().installViewFactory ();
134
103
getDelegate ().onCreate (savedInstanceState );
135
104
136
- setContentView (R .layout .activity_login );
105
+ binding = ActivityLoginBinding .inflate (getLayoutInflater ());
106
+ setContentView (binding .getRoot ());
107
+
108
+ binding .loginUsername .addTextChangedListener (textWatcher );
109
+ binding .loginPassword .addTextChangedListener (textWatcher );
110
+ binding .loginTwoFactor .addTextChangedListener (textWatcher );
137
111
138
- ButterKnife .bind (this );
112
+ binding .skipLogin .setOnClickListener (view -> skipLogin ());
113
+ binding .forgotPassword .setOnClickListener (view -> forgotPassword ());
114
+ binding .aboutPrivacyPolicy .setOnClickListener (view -> onPrivacyPolicyClicked ());
115
+ binding .signUpButton .setOnClickListener (view -> signUp ());
116
+ binding .loginButton .setOnClickListener (view -> performLogin ());
139
117
140
- usernameEdit .addTextChangedListener (textWatcher );
141
- passwordEdit .addTextChangedListener (textWatcher );
142
- twoFactorEdit .addTextChangedListener (textWatcher );
118
+ binding .loginPassword .setOnEditorActionListener (this ::onEditorAction );
119
+ binding .loginPassword .setOnFocusChangeListener (this ::onPasswordFocusChanged );
143
120
144
121
if (ConfigUtils .isBetaFlavour ()) {
145
- loginCredentials .setText (getString (R .string .login_credential ));
122
+ binding . loginCredentials .setText (getString (R .string .login_credential ));
146
123
} else {
147
- loginCredentials .setVisibility (View .GONE );
124
+ binding . loginCredentials .setVisibility (View .GONE );
148
125
}
149
126
}
150
127
/**
151
128
* Hides the keyboard if the user's focus is not on the password (hasFocus is false).
152
129
* @param view The keyboard
153
130
* @param hasFocus Set to true if the keyboard has focus
154
131
*/
155
- @ OnFocusChange (R .id .login_password )
156
132
void onPasswordFocusChanged (View view , boolean hasFocus ) {
157
133
if (!hasFocus ) {
158
134
ViewUtil .hideKeyboard (view );
159
135
}
160
136
}
161
137
162
- @ OnEditorAction (R .id .login_password )
163
- boolean onEditorAction (int actionId , KeyEvent keyEvent ) {
164
- if (loginButton .isEnabled ()) {
138
+ boolean onEditorAction (TextView textView , int actionId , KeyEvent keyEvent ) {
139
+ if (binding .loginButton .isEnabled ()) {
165
140
if (actionId == IME_ACTION_DONE ) {
166
141
performLogin ();
167
142
return true ;
@@ -174,8 +149,7 @@ boolean onEditorAction(int actionId, KeyEvent keyEvent) {
174
149
}
175
150
176
151
177
- @ OnClick (R .id .skip_login )
178
- void skipLogin () {
152
+ protected void skipLogin () {
179
153
new AlertDialog .Builder (this ).setTitle (R .string .skip_login_title )
180
154
.setMessage (R .string .skip_login_message )
181
155
.setCancelable (false )
@@ -187,18 +161,15 @@ void skipLogin() {
187
161
.show ();
188
162
}
189
163
190
- @ OnClick (R .id .forgot_password )
191
- void forgotPassword () {
164
+ protected void forgotPassword () {
192
165
Utils .handleWebUrl (this , Uri .parse (BuildConfig .FORGOT_PASSWORD_URL ));
193
166
}
194
167
195
- @ OnClick (R .id .about_privacy_policy )
196
- void onPrivacyPolicyClicked () {
168
+ protected void onPrivacyPolicyClicked () {
197
169
Utils .handleWebUrl (this , Uri .parse (BuildConfig .PRIVACY_POLICY_URL ));
198
170
}
199
171
200
- @ OnClick (R .id .sign_up_button )
201
- void signUp () {
172
+ protected void signUp () {
202
173
Intent intent = new Intent (this , SignupActivity .class );
203
174
startActivity (intent );
204
175
}
@@ -236,23 +207,23 @@ protected void onDestroy() {
236
207
} catch (Exception e ) {
237
208
e .printStackTrace ();
238
209
}
239
- usernameEdit .removeTextChangedListener (textWatcher );
240
- passwordEdit .removeTextChangedListener (textWatcher );
241
- twoFactorEdit .removeTextChangedListener (textWatcher );
210
+ binding . loginUsername .removeTextChangedListener (textWatcher );
211
+ binding . loginPassword .removeTextChangedListener (textWatcher );
212
+ binding . loginTwoFactor .removeTextChangedListener (textWatcher );
242
213
delegate .onDestroy ();
243
214
if (null !=loginClient ) {
244
215
loginClient .cancel ();
245
216
}
217
+ binding = null ;
246
218
super .onDestroy ();
247
219
}
248
220
249
- @ OnClick (R .id .login_button )
250
221
public void performLogin () {
251
222
Timber .d ("Login to start!" );
252
- final String username = usernameEdit .getText ().toString ();
253
- final String rawUsername = usernameEdit .getText ().toString ().trim ();
254
- final String password = passwordEdit .getText ().toString ();
255
- String twoFactorCode = twoFactorEdit .getText ().toString ();
223
+ final String username = binding . loginUsername .getText ().toString ();
224
+ final String rawUsername = binding . loginUsername .getText ().toString ().trim ();
225
+ final String password = binding . loginPassword .getText ().toString ();
226
+ String twoFactorCode = binding . loginTwoFactor .getText ().toString ();
256
227
257
228
showLoggingProgressBar ();
258
229
doLogin (username , password , twoFactorCode );
@@ -389,9 +360,9 @@ public MenuInflater getMenuInflater() {
389
360
390
361
public void askUserForTwoFactorAuth () {
391
362
progressDialog .dismiss ();
392
- twoFactorContainer .setVisibility (VISIBLE );
393
- twoFactorEdit .setVisibility (VISIBLE );
394
- twoFactorEdit .requestFocus ();
363
+ binding . twoFactorContainer .setVisibility (VISIBLE );
364
+ binding . loginTwoFactor .setVisibility (VISIBLE );
365
+ binding . loginTwoFactor .requestFocus ();
395
366
InputMethodManager imm = (InputMethodManager ) getSystemService (Context .INPUT_METHOD_SERVICE );
396
367
imm .toggleSoftInput (InputMethodManager .SHOW_FORCED , InputMethodManager .HIDE_IMPLICIT_ONLY );
397
368
showMessageAndCancelDialog (R .string .login_failed_2fa_needed );
@@ -422,15 +393,15 @@ public void startMainActivity() {
422
393
}
423
394
424
395
private void showMessage (@ StringRes int resId , @ ColorRes int colorResId ) {
425
- errorMessage .setText (getString (resId ));
426
- errorMessage .setTextColor (ContextCompat .getColor (this , colorResId ));
427
- errorMessageContainer .setVisibility (VISIBLE );
396
+ binding . errorMessage .setText (getString (resId ));
397
+ binding . errorMessage .setTextColor (ContextCompat .getColor (this , colorResId ));
398
+ binding . errorMessageContainer .setVisibility (VISIBLE );
428
399
}
429
400
430
401
private void showMessage (String message , @ ColorRes int colorResId ) {
431
- errorMessage .setText (message );
432
- errorMessage .setTextColor (ContextCompat .getColor (this , colorResId ));
433
- errorMessageContainer .setVisibility (VISIBLE );
402
+ binding . errorMessage .setText (message );
403
+ binding . errorMessage .setTextColor (ContextCompat .getColor (this , colorResId ));
404
+ binding . errorMessageContainer .setVisibility (VISIBLE );
434
405
}
435
406
436
407
private AppCompatDelegate getDelegate () {
@@ -451,9 +422,11 @@ public void onTextChanged(CharSequence charSequence, int start, int count, int a
451
422
452
423
@ Override
453
424
public void afterTextChanged (Editable editable ) {
454
- boolean enabled = usernameEdit .getText ().length () != 0 && passwordEdit .getText ().length () != 0
455
- && (BuildConfig .DEBUG || twoFactorEdit .getText ().length () != 0 || twoFactorEdit .getVisibility () != VISIBLE );
456
- loginButton .setEnabled (enabled );
425
+ boolean enabled = binding .loginUsername .getText ().length () != 0 &&
426
+ binding .loginPassword .getText ().length () != 0 &&
427
+ (BuildConfig .DEBUG || binding .loginTwoFactor .getText ().length () != 0 ||
428
+ binding .loginTwoFactor .getVisibility () != VISIBLE );
429
+ binding .loginButton .setEnabled (enabled );
457
430
}
458
431
}
459
432
@@ -471,22 +444,22 @@ protected void onSaveInstanceState(Bundle outState) {
471
444
} else {
472
445
outState .putBoolean (saveProgressDailog ,false );
473
446
}
474
- outState .putString (saveErrorMessage ,errorMessage .getText ().toString ()); //Save the errorMessage
447
+ outState .putString (saveErrorMessage ,binding . errorMessage .getText ().toString ()); //Save the errorMessage
475
448
outState .putString (saveUsername ,getUsername ()); // Save the username
476
449
outState .putString (savePassword ,getPassword ()); // Save the password
477
450
}
478
451
private String getUsername () {
479
- return usernameEdit .getText ().toString ();
452
+ return binding . loginUsername .getText ().toString ();
480
453
}
481
454
private String getPassword (){
482
- return passwordEdit .getText ().toString ();
455
+ return binding . loginPassword .getText ().toString ();
483
456
}
484
457
485
458
@ Override
486
459
protected void onRestoreInstanceState (final Bundle savedInstanceState ) {
487
460
super .onRestoreInstanceState (savedInstanceState );
488
- usernameEdit .setText (savedInstanceState .getString (saveUsername ));
489
- passwordEdit .setText (savedInstanceState .getString (savePassword ));
461
+ binding . loginUsername .setText (savedInstanceState .getString (saveUsername ));
462
+ binding . loginPassword .setText (savedInstanceState .getString (savePassword ));
490
463
if (savedInstanceState .getBoolean (saveProgressDailog )) {
491
464
performLogin ();
492
465
}
0 commit comments