5
5
import android .content .Intent ;
6
6
import android .content .SharedPreferences ;
7
7
import android .os .Bundle ;
8
+ import android .support .annotation .ColorRes ;
9
+ import android .support .annotation .NonNull ;
10
+ import android .support .annotation .StringRes ;
8
11
import android .support .v4 .app .NavUtils ;
12
+ import android .support .v4 .content .ContextCompat ;
13
+ import android .support .v7 .app .AppCompatDelegate ;
9
14
import android .text .Editable ;
10
15
import android .text .TextWatcher ;
16
+ import android .view .MenuInflater ;
11
17
import android .view .MenuItem ;
12
18
import android .view .View ;
19
+ import android .view .ViewGroup ;
13
20
import android .widget .Button ;
14
21
import android .widget .EditText ;
15
22
import android .widget .TextView ;
16
- import android .widget .Toast ;
17
23
24
+ import butterknife .BindView ;
25
+ import butterknife .ButterKnife ;
18
26
import fr .free .nrw .commons .BuildConfig ;
19
27
import fr .free .nrw .commons .CommonsApplication ;
28
+ import fr .free .nrw .commons .PageTitle ;
20
29
import fr .free .nrw .commons .R ;
30
+ import fr .free .nrw .commons .Utils ;
21
31
import fr .free .nrw .commons .WelcomeActivity ;
22
-
23
- import fr .free .nrw .commons .PageTitle ;
24
32
import fr .free .nrw .commons .contributions .ContributionsActivity ;
25
33
import timber .log .Timber ;
26
34
27
35
import static android .view .KeyEvent .KEYCODE_ENTER ;
28
36
import static android .view .inputmethod .EditorInfo .IME_ACTION_DONE ;
29
37
30
-
31
38
public class LoginActivity extends AccountAuthenticatorActivity {
32
39
33
40
public static final String PARAM_USERNAME = "fr.free.nrw.commons.login.username" ;
34
41
35
- private SharedPreferences prefs = null ;
42
+ @ BindView (R .id .loginButton ) Button loginButton ;
43
+ @ BindView (R .id .signupButton ) Button signupButton ;
44
+ @ BindView (R .id .loginUsername ) EditText usernameEdit ;
45
+ @ BindView (R .id .loginPassword ) EditText passwordEdit ;
46
+ @ BindView (R .id .loginTwoFactor ) EditText twoFactorEdit ;
47
+ @ BindView (R .id .error_message_container ) ViewGroup errorMessageContainer ;
48
+ @ BindView (R .id .error_message ) TextView errorMessage ;
36
49
37
- private Button loginButton ;
38
- private EditText usernameEdit ;
39
- private EditText passwordEdit ;
40
- private EditText twoFactorEdit ;
50
+ private CommonsApplication app ;
41
51
ProgressDialog progressDialog ;
52
+ private AppCompatDelegate delegate ;
53
+ private SharedPreferences prefs = null ;
42
54
private LoginTextWatcher textWatcher = new LoginTextWatcher ();
43
55
44
- private CommonsApplication app ;
45
-
46
56
@ Override
47
57
public void onCreate (Bundle savedInstanceState ) {
58
+ setTheme (Utils .isDarkTheme (this ) ? R .style .DarkAppTheme : R .style .LightAppTheme );
59
+ getDelegate ().installViewFactory ();
60
+ getDelegate ().onCreate (savedInstanceState );
48
61
super .onCreate (savedInstanceState );
49
62
50
63
app = CommonsApplication .getInstance ();
51
64
52
65
setContentView (R .layout .activity_login );
53
66
54
- loginButton = (Button ) findViewById (R .id .loginButton );
55
- Button signupButton = (Button ) findViewById (R .id .signupButton );
56
- usernameEdit = (EditText ) findViewById (R .id .loginUsername );
57
- passwordEdit = (EditText ) findViewById (R .id .loginPassword );
58
- twoFactorEdit = (EditText ) findViewById (R .id .loginTwoFactor );
67
+ ButterKnife .bind (this );
59
68
60
69
prefs = getSharedPreferences ("fr.free.nrw.commons" , MODE_PRIVATE );
61
70
@@ -64,45 +73,17 @@ public void onCreate(Bundle savedInstanceState) {
64
73
twoFactorEdit .addTextChangedListener (textWatcher );
65
74
passwordEdit .setOnEditorActionListener (newLoginInputActionListener ());
66
75
67
- loginButton .setOnClickListener (this ::performLogin );
68
- signupButton .setOnClickListener (this ::signUp );
69
- }
70
-
71
- private class LoginTextWatcher implements TextWatcher {
72
- @ Override
73
- public void beforeTextChanged (CharSequence charSequence , int start , int count , int after ) {
74
- }
75
-
76
- @ Override
77
- public void onTextChanged (CharSequence charSequence , int start , int count , int after ) {
78
- }
79
-
80
- @ Override
81
- public void afterTextChanged (Editable editable ) {
82
- if (usernameEdit .getText ().length () != 0 && passwordEdit .getText ().length () != 0 &&
83
- (BuildConfig .DEBUG || twoFactorEdit .getText ().length () != 0 || twoFactorEdit .getVisibility () != View .VISIBLE )) {
84
- loginButton .setEnabled (true );
85
- } else {
86
- loginButton .setEnabled (false );
87
- }
88
- }
76
+ loginButton .setOnClickListener (view -> performLogin ());
77
+ signupButton .setOnClickListener (view -> signUp ());
89
78
}
90
79
91
- private TextView .OnEditorActionListener newLoginInputActionListener () {
92
- return (textView , actionId , keyEvent ) -> {
93
- if (loginButton .isEnabled ()) {
94
- if (actionId == IME_ACTION_DONE ) {
95
- performLogin (textView );
96
- return true ;
97
- } else if ((keyEvent != null ) && keyEvent .getKeyCode () == KEYCODE_ENTER ) {
98
- performLogin (textView );
99
- return true ;
100
- }
101
- }
102
- return false ;
103
- };
80
+ @ Override
81
+ protected void onPostCreate (Bundle savedInstanceState ) {
82
+ super .onPostCreate (savedInstanceState );
83
+ getDelegate ().onPostCreate (savedInstanceState );
104
84
}
105
85
86
+ @ Override
106
87
protected void onResume () {
107
88
super .onResume ();
108
89
if (prefs .getBoolean ("firstrun" , true )) {
@@ -127,31 +108,31 @@ protected void onDestroy() {
127
108
usernameEdit .removeTextChangedListener (textWatcher );
128
109
passwordEdit .removeTextChangedListener (textWatcher );
129
110
twoFactorEdit .removeTextChangedListener (textWatcher );
111
+ delegate .onDestroy ();
130
112
super .onDestroy ();
131
113
}
132
114
133
- private void performLogin ( View view ) {
134
- Timber . d ( "Login to start!" );
135
- LoginTask task = getLoginTask ();
136
- task . execute ();
115
+ @ Override
116
+ protected void onStart () {
117
+ super . onStart ();
118
+ delegate . onStart ();
137
119
}
138
120
139
- private LoginTask getLoginTask () {
140
- return new LoginTask (
141
- this ,
142
- canonicializeUsername (usernameEdit .getText ().toString ()),
143
- passwordEdit .getText ().toString (),
144
- twoFactorEdit .getText ().toString ()
145
- );
121
+ @ Override
122
+ protected void onStop () {
123
+ super .onStop ();
124
+ delegate .onStop ();
146
125
}
147
126
148
- /**
149
- * Because Mediawiki is upercase-first-char-then-case-sensitive :)
150
- * @param username String
151
- * @return String canonicial username
152
- */
153
- private String canonicializeUsername (String username ) {
154
- return new PageTitle (username ).getText ();
127
+ @ Override
128
+ protected void onPostResume () {
129
+ super .onPostResume ();
130
+ getDelegate ().onPostResume ();
131
+ }
132
+
133
+ @ Override
134
+ public void setContentView (View view , ViewGroup .LayoutParams params ) {
135
+ getDelegate ().setContentView (view , params );
155
136
}
156
137
157
138
@ Override
@@ -164,36 +145,28 @@ public boolean onOptionsItemSelected(MenuItem item) {
164
145
return super .onOptionsItemSelected (item );
165
146
}
166
147
167
- /**
168
- * Called when Sign Up button is clicked.
169
- * @param view View
170
- */
171
- public void signUp (View view ) {
172
- Intent intent = new Intent (this , SignupActivity .class );
173
- startActivity (intent );
148
+ @ Override
149
+ @ NonNull
150
+ public MenuInflater getMenuInflater () {
151
+ return getDelegate ().getMenuInflater ();
174
152
}
175
153
176
154
public void askUserForTwoFactorAuth () {
177
155
if (BuildConfig .DEBUG ) {
178
156
twoFactorEdit .setVisibility (View .VISIBLE );
179
- showUserToastAndCancelDialog (R .string .login_failed_2fa_needed );
157
+ showMessageAndCancelDialog (R .string .login_failed_2fa_needed );
180
158
} else {
181
- showUserToastAndCancelDialog (R .string .login_failed_2fa_not_supported );
159
+ showMessageAndCancelDialog (R .string .login_failed_2fa_not_supported );
182
160
}
183
161
}
184
162
185
- public void showUserToastAndCancelDialog ( int resId ) {
186
- showUserToast (resId );
163
+ public void showMessageAndCancelDialog ( @ StringRes int resId ) {
164
+ showMessage (resId , R . color . secondaryDarkColor );
187
165
progressDialog .cancel ();
188
166
}
189
167
190
- private void showUserToast (int resId) {
191
- Toast .makeText (this , resId , Toast .LENGTH_LONG ).show ();
192
- }
193
-
194
- public void showSuccessToastAndDismissDialog () {
195
- Toast successToast = Toast .makeText (this , R .string .login_success , Toast .LENGTH_SHORT );
196
- successToast .show ();
168
+ public void showSuccessAndDismissDialog () {
169
+ showMessage (R .string .login_success , R .color .primaryDarkColor );
197
170
progressDialog .dismiss ();
198
171
}
199
172
@@ -207,4 +180,77 @@ public void startMainActivity() {
207
180
finish ();
208
181
}
209
182
183
+ private void performLogin () {
184
+ Timber .d ("Login to start!" );
185
+ LoginTask task = getLoginTask ();
186
+ task .execute ();
187
+ }
188
+
189
+ private void signUp () {
190
+ Intent intent = new Intent (this , SignupActivity .class );
191
+ startActivity (intent );
192
+ }
193
+
194
+ private TextView .OnEditorActionListener newLoginInputActionListener () {
195
+ return (textView , actionId , keyEvent ) -> {
196
+ if (loginButton .isEnabled ()) {
197
+ if (actionId == IME_ACTION_DONE ) {
198
+ performLogin ();
199
+ return true ;
200
+ } else if ((keyEvent != null ) && keyEvent .getKeyCode () == KEYCODE_ENTER ) {
201
+ performLogin ();
202
+ return true ;
203
+ }
204
+ }
205
+ return false ;
206
+ };
207
+ }
208
+
209
+ private LoginTask getLoginTask () {
210
+ return new LoginTask (
211
+ this ,
212
+ canonicializeUsername (usernameEdit .getText ().toString ()),
213
+ passwordEdit .getText ().toString (),
214
+ twoFactorEdit .getText ().toString ()
215
+ );
216
+ }
217
+
218
+ /**
219
+ * Because Mediawiki is upercase-first-char-then-case-sensitive :)
220
+ * @param username String
221
+ * @return String canonicial username
222
+ */
223
+ private String canonicializeUsername (String username ) {
224
+ return new PageTitle (username ).getText ();
225
+ }
226
+
227
+ private void showMessage (@ StringRes int resId , @ ColorRes int colorResId ) {
228
+ errorMessage .setText (getString (resId ));
229
+ errorMessage .setTextColor (ContextCompat .getColor (this , colorResId ));
230
+ errorMessageContainer .setVisibility (View .VISIBLE );
231
+ }
232
+
233
+ private AppCompatDelegate getDelegate () {
234
+ if (delegate == null ) {
235
+ delegate = AppCompatDelegate .create (this , null );
236
+ }
237
+ return delegate ;
238
+ }
239
+
240
+ private class LoginTextWatcher implements TextWatcher {
241
+ @ Override
242
+ public void beforeTextChanged (CharSequence charSequence , int start , int count , int after ) {
243
+ }
244
+
245
+ @ Override
246
+ public void onTextChanged (CharSequence charSequence , int start , int count , int after ) {
247
+ }
248
+
249
+ @ Override
250
+ public void afterTextChanged (Editable editable ) {
251
+ boolean enabled = usernameEdit .getText ().length () != 0 && passwordEdit .getText ().length () != 0 &&
252
+ (BuildConfig .DEBUG || twoFactorEdit .getText ().length () != 0 || twoFactorEdit .getVisibility () != View .VISIBLE );
253
+ loginButton .setEnabled (enabled );
254
+ }
255
+ }
210
256
}
0 commit comments