1
1
package fr .free .nrw .commons .auth ;
2
2
3
+ import static android .view .KeyEvent .KEYCODE_ENTER ;
4
+ import static android .view .View .VISIBLE ;
5
+ import static android .view .inputmethod .EditorInfo .IME_ACTION_DONE ;
6
+ import static fr .free .nrw .commons .di .NetworkingModule .NAMED_COMMONS_WIKI_SITE ;
7
+
3
8
import android .accounts .AccountAuthenticatorActivity ;
4
9
import android .app .ProgressDialog ;
5
10
import android .content .Context ;
16
21
import android .widget .Button ;
17
22
import android .widget .EditText ;
18
23
import android .widget .TextView ;
19
-
20
24
import androidx .annotation .ColorRes ;
21
25
import androidx .annotation .NonNull ;
26
+ import androidx .annotation .Nullable ;
22
27
import androidx .annotation .StringRes ;
23
28
import androidx .appcompat .app .AlertDialog ;
24
29
import androidx .appcompat .app .AppCompatDelegate ;
25
30
import androidx .core .app .NavUtils ;
26
31
import androidx .core .content .ContextCompat ;
27
-
28
- import com .google .android .material .textfield .TextInputLayout ;
29
-
30
- import org .wikipedia .AppAdapter ;
31
- import org .wikipedia .dataclient .WikiSite ;
32
- import org .wikipedia .login .LoginClient ;
33
- import org .wikipedia .login .LoginResult ;
34
-
35
- import javax .inject .Inject ;
36
- import javax .inject .Named ;
37
-
38
32
import butterknife .BindView ;
39
33
import butterknife .ButterKnife ;
40
34
import butterknife .OnClick ;
41
35
import butterknife .OnEditorAction ;
42
36
import butterknife .OnFocusChange ;
37
+ import com .google .android .material .textfield .TextInputLayout ;
43
38
import fr .free .nrw .commons .BuildConfig ;
44
39
import fr .free .nrw .commons .R ;
45
40
import fr .free .nrw .commons .Utils ;
52
47
import fr .free .nrw .commons .theme .NavigationBaseActivity ;
53
48
import fr .free .nrw .commons .utils .ConfigUtils ;
54
49
import fr .free .nrw .commons .utils .ViewUtil ;
55
- import io .reactivex .Completable ;
56
- import io .reactivex .android .schedulers .AndroidSchedulers ;
57
50
import io .reactivex .disposables .CompositeDisposable ;
58
- import io .reactivex .functions .Action ;
59
- import io .reactivex .schedulers .Schedulers ;
51
+ import javax .inject .Inject ;
52
+ import javax .inject .Named ;
53
+ import org .wikipedia .AppAdapter ;
54
+ import org .wikipedia .dataclient .ServiceFactory ;
55
+ import org .wikipedia .dataclient .WikiSite ;
56
+ import org .wikipedia .dataclient .mwapi .MwQueryResponse ;
57
+ import org .wikipedia .login .LoginClient ;
58
+ import org .wikipedia .login .LoginClient .LoginCallback ;
59
+ import org .wikipedia .login .LoginResult ;
60
+ import retrofit2 .Call ;
61
+ import retrofit2 .Callback ;
62
+ import retrofit2 .Response ;
60
63
import timber .log .Timber ;
61
64
62
- import static android .view .KeyEvent .KEYCODE_ENTER ;
63
- import static android .view .View .VISIBLE ;
64
- import static android .view .inputmethod .EditorInfo .IME_ACTION_DONE ;
65
- import static fr .free .nrw .commons .di .NetworkingModule .NAMED_COMMONS_WIKI_SITE ;
66
-
67
65
public class LoginActivity extends AccountAuthenticatorActivity {
68
66
69
67
@ Inject
@@ -111,6 +109,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
111
109
private AppCompatDelegate delegate ;
112
110
private LoginTextWatcher textWatcher = new LoginTextWatcher ();
113
111
private CompositeDisposable compositeDisposable = new CompositeDisposable ();
112
+ private Call <MwQueryResponse > loginToken ;
114
113
115
114
@ Override
116
115
public void onCreate (Bundle savedInstanceState ) {
@@ -238,6 +237,9 @@ protected void onDestroy() {
238
237
passwordEdit .removeTextChangedListener (textWatcher );
239
238
twoFactorEdit .removeTextChangedListener (textWatcher );
240
239
delegate .onDestroy ();
240
+ if (null !=loginClient ) {
241
+ loginClient .cancel ();
242
+ }
241
243
super .onDestroy ();
242
244
}
243
245
@@ -255,34 +257,62 @@ public void performLogin() {
255
257
256
258
private void doLogin (String username , String password , String twoFactorCode ) {
257
259
progressDialog .show ();
260
+ loginToken = ServiceFactory .get (commonsWikiSite ).getLoginToken ();
261
+ loginToken .enqueue (
262
+ new Callback <MwQueryResponse >() {
263
+ @ Override
264
+ public void onResponse (Call <MwQueryResponse > call ,
265
+ Response <MwQueryResponse > response ) {
266
+ loginClient .login (commonsWikiSite , username , password , null , twoFactorCode ,
267
+ response .body ().query ().loginToken (), new LoginCallback () {
268
+ @ Override
269
+ public void success (@ NonNull LoginResult result ) {
270
+ Timber .d ("Login Success" );
271
+ onLoginSuccess (result );
272
+ }
273
+
274
+ @ Override
275
+ public void twoFactorPrompt (@ NonNull Throwable caught ,
276
+ @ Nullable String token ) {
277
+ Timber .d ("Requesting 2FA prompt" );
278
+ hideProgress ();
279
+ askUserForTwoFactorAuth ();
280
+ }
281
+
282
+ @ Override
283
+ public void passwordResetPrompt (@ Nullable String token ) {
284
+ Timber .d ("Showing password reset prompt" );
285
+ hideProgress ();
286
+ showPasswordResetPrompt ();
287
+ }
288
+
289
+ @ Override
290
+ public void error (@ NonNull Throwable caught ) {
291
+ Timber .e (caught );
292
+ hideProgress ();
293
+ showMessageAndCancelDialog (caught .getLocalizedMessage ());
294
+ }
295
+ });
296
+ }
297
+
298
+ @ Override
299
+ public void onFailure (Call <MwQueryResponse > call , Throwable t ) {
300
+ Timber .e (t );
301
+ showMessageAndCancelDialog (t .getLocalizedMessage ());
302
+ }
303
+ });
304
+
305
+ }
306
+
307
+ private void hideProgress () {
308
+ progressDialog .dismiss ();
309
+ }
258
310
259
- Action action = () -> {
260
- try {
261
- loginClient .loginBlocking (commonsWikiSite , username , password , twoFactorCode );
262
- } catch (Throwable throwable ) {
263
- throwable .printStackTrace ();
264
- }
265
- };
266
-
267
- compositeDisposable .add (Completable .fromAction (action )
268
- .subscribeOn (Schedulers .io ())
269
- .observeOn (AndroidSchedulers .mainThread ())
270
- .subscribe (() -> onLoginSuccess (username , password ),
271
- error -> {
272
- if (error instanceof LoginClient .LoginFailedException ) {
273
- LoginClient .LoginFailedException exception = (LoginClient .LoginFailedException ) error ;
274
- if (exception .getMessage ().equals ("2FA" )) {
275
- askUserForTwoFactorAuth ();
276
- }
277
- }
278
- if (!progressDialog .isShowing ()) {
279
- return ;
280
- }
281
- progressDialog .dismiss ();
282
- showMessageAndCancelDialog (R .string .error_occurred );
283
- }));
311
+ private void showPasswordResetPrompt () {
312
+ showMessageAndCancelDialog (getString (R .string .you_must_reset_your_passsword ));
284
313
}
285
314
315
+
286
316
/**
287
317
* This function is called when user skips the login.
288
318
* It redirects the user to Explore Activity.
@@ -302,13 +332,12 @@ private void showLoggingProgressBar() {
302
332
progressDialog .show ();
303
333
}
304
334
305
- private void onLoginSuccess (String username , String password ) {
335
+ private void onLoginSuccess (LoginResult loginResult ) {
306
336
if (!progressDialog .isShowing ()) {
307
337
// no longer attached to activity!
308
338
return ;
309
339
}
310
340
sessionManager .setUserLoggedIn (true );
311
- LoginResult loginResult = new LoginResult (commonsWikiSite , "PASS" , username , password , "" );
312
341
AppAdapter .get ().updateAccount (loginResult );
313
342
progressDialog .dismiss ();
314
343
showSuccessAndDismissDialog ();
0 commit comments