27
27
import static android .view .KeyEvent .ACTION_DOWN ;
28
28
import static android .view .KeyEvent .KEYCODE_ENTER ;
29
29
import static android .view .inputmethod .EditorInfo .IME_ACTION_DONE ;
30
- import static com .github .mobile .accounts .AccountConstants .ACCOUNT_TYPE ;
31
- import static com .github .mobile .accounts .AccountConstants .PROVIDER_AUTHORITY ;
30
+ import static com .github .mobile .accounts .AccountConstants .*;
31
+ import static com .github .mobile .RequestCodes .OTP_CODE_ENTER ;
32
+ import static com .github .mobile .accounts .TwoFactorAuthActivity .PARAM_EXCEPTION ;
33
+ import static com .github .mobile .accounts .TwoFactorAuthClient .TWO_FACTOR_AUTH_TYPE_SMS ;
34
+
32
35
import android .accounts .Account ;
33
36
import android .accounts .AccountManager ;
34
37
import android .app .AlertDialog ;
59
62
import com .actionbarsherlock .view .Menu ;
60
63
import com .actionbarsherlock .view .MenuItem ;
61
64
import com .github .kevinsawicki .wishlist .ViewFinder ;
62
- import com .github .mobile .DefaultClient ;
63
65
import com .github .mobile .R .id ;
64
66
import com .github .mobile .R .layout ;
65
67
import com .github .mobile .R .menu ;
77
79
78
80
import org .eclipse .egit .github .core .User ;
79
81
import org .eclipse .egit .github .core .client .GitHubClient ;
82
+ import org .eclipse .egit .github .core .service .OAuthService ;
80
83
import org .eclipse .egit .github .core .service .UserService ;
81
84
82
85
import roboguice .util .RoboAsyncTask ;
@@ -105,7 +108,7 @@ public class LoginActivity extends RoboSherlockAccountAuthenticatorActivity {
105
108
*/
106
109
private static final long SYNC_PERIOD = 8L * 60L * 60L ;
107
110
108
- private static void configureSyncFor (Account account ) {
111
+ public static void configureSyncFor (Account account ) {
109
112
Log .d (TAG , "Configuring account sync" );
110
113
111
114
ContentResolver .setIsSyncable (account , PROVIDER_AUTHORITY , 1 );
@@ -114,7 +117,7 @@ private static void configureSyncFor(Account account) {
114
117
new Bundle (), SYNC_PERIOD );
115
118
}
116
119
117
- private static class AccountLoader extends
120
+ public static class AccountLoader extends
118
121
AuthenticatedUserTask <List <User >> {
119
122
120
123
@ Inject
@@ -198,6 +201,7 @@ public void afterTextChanged(Editable gitDirEditText) {
198
201
199
202
passwordText .setOnKeyListener (new OnKeyListener () {
200
203
204
+ @ Override
201
205
public boolean onKey (View v , int keyCode , KeyEvent event ) {
202
206
if (event != null && ACTION_DOWN == event .getAction ()
203
207
&& keyCode == KEYCODE_ENTER && loginEnabled ()) {
@@ -210,6 +214,7 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
210
214
211
215
passwordText .setOnEditorActionListener (new OnEditorActionListener () {
212
216
217
+ @ Override
213
218
public boolean onEditorAction (TextView v , int actionId ,
214
219
KeyEvent event ) {
215
220
if (actionId == IME_ACTION_DONE && loginEnabled ()) {
@@ -305,9 +310,19 @@ public void onCancel(DialogInterface dialog) {
305
310
306
311
@ Override
307
312
public User call () throws Exception {
308
- GitHubClient client = new DefaultClient ();
313
+ GitHubClient client = new TwoFactorAuthClient ();
309
314
client .setCredentials (username , password );
310
- User user = new UserService (client ).getUser ();
315
+
316
+ User user ;
317
+ try {
318
+ user = new UserService (client ).getUser ();
319
+ } catch (TwoFactorAuthException e ) {
320
+ if (e .twoFactorAuthType == TWO_FACTOR_AUTH_TYPE_SMS )
321
+ sendSmsOtpCode (new OAuthService (client ));
322
+ openTwoFactorAuthActivity ();
323
+
324
+ return null ;
325
+ }
311
326
312
327
Account account = new Account (user .getLogin (), ACCOUNT_TYPE );
313
328
if (requestNewAccount ) {
@@ -330,24 +345,37 @@ protected void onException(Exception e) throws RuntimeException {
330
345
dialog .dismiss ();
331
346
332
347
Log .d (TAG , "Exception requesting authenticated user" , e );
333
-
334
- if (AccountUtils .isUnauthorized (e ))
335
- onAuthenticationResult (false );
336
- else
337
- ToastUtils .show (LoginActivity .this , e ,
338
- string .connection_failed );
348
+ handleLoginException (e );
339
349
}
340
350
341
351
@ Override
342
352
public void onSuccess (User user ) {
343
353
dialog .dismiss ();
344
354
345
- onAuthenticationResult (true );
355
+ if (user != null )
356
+ onAuthenticationResult (true );
346
357
}
347
358
};
348
359
authenticationTask .execute ();
349
360
}
350
361
362
+ @ Override
363
+ protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
364
+ super .onActivityResult (requestCode , resultCode , data );
365
+
366
+ if (requestCode == OTP_CODE_ENTER ) {
367
+ switch (resultCode ) {
368
+ case RESULT_OK :
369
+ onAuthenticationResult (true );
370
+ break ;
371
+ case RESULT_CANCELED :
372
+ Exception e = (Exception ) data .getExtras ().getSerializable (PARAM_EXCEPTION );
373
+ handleLoginException (e );
374
+ break ;
375
+ }
376
+ }
377
+ }
378
+
351
379
/**
352
380
* Called when response is received from the server for confirm credentials
353
381
* request. See onAuthenticationResult(). Sets the
@@ -406,6 +434,7 @@ public void onAuthenticationResult(boolean result) {
406
434
}
407
435
}
408
436
437
+ @ Override
409
438
public boolean onOptionsItemSelected (MenuItem item ) {
410
439
switch (item .getItemId ()) {
411
440
case id .m_login :
@@ -432,4 +461,23 @@ private List<String> getEmailAddresses() {
432
461
addresses .add (account .name );
433
462
return addresses ;
434
463
}
435
- }
464
+
465
+ private void sendSmsOtpCode (final OAuthService service ) throws IOException {
466
+ try {
467
+ AccountAuthenticator .createAuthorization (service );
468
+ } catch (TwoFactorAuthException ignored ) {
469
+ }
470
+ }
471
+
472
+ private void openTwoFactorAuthActivity () {
473
+ Intent intent = TwoFactorAuthActivity .createIntent (this , username , password );
474
+ startActivityForResult (intent , OTP_CODE_ENTER );
475
+ }
476
+
477
+ private void handleLoginException (final Exception e ) {
478
+ if (AccountUtils .isUnauthorized (e ))
479
+ onAuthenticationResult (false );
480
+ else
481
+ ToastUtils .show (LoginActivity .this , e , string .connection_failed );
482
+ }
483
+ }
0 commit comments