Example #1
0
            private async void StartAuth()
            {
                if (IsAuthenticating)
                {
                    return;
                }
                IsAuthenticating = true;

                LoginActivity activity;

                try {
                    var log         = ServiceContainer.Resolve <Logger> ();
                    var authManager = ServiceContainer.Resolve <AuthManager> ();
                    var ctx         = Activity;

                    // Workaround for Android linker bug which forgets to register JNI types
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/GoogleAuthException", typeof(GoogleAuthException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/GooglePlayServicesAvailabilityException", typeof(GooglePlayServicesAvailabilityException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/UserRecoverableAuthException", typeof(UserRecoverableAuthException));
                    Java.Interop.TypeManager.RegisterType("com/google/android/gms/auth/UserRecoverableNotifiedException", typeof(UserRecoverableNotifiedException));

                    String token = null;
                    try {
                        token = await Task.Factory.StartNew(() => GoogleAuthUtil.GetToken(ctx, Email, GoogleOAuthScope));
                    } catch (GooglePlayServicesAvailabilityException exc) {
                        var dia = GooglePlayServicesUtil.GetErrorDialog(
                            exc.ConnectionStatusCode, ctx, GoogleAuthRequestCode);
                        dia.Show();
                    } catch (UserRecoverableAuthException exc) {
                        StartActivityForResult(exc.Intent, GoogleAuthRequestCode);
                    } catch (Java.IO.IOException exc) {
                        // Connectivity error.. nothing to do really?
                        log.Info(Tag, exc, "Failed to login with Google due to network issues.");
                    } catch (Exception exc) {
                        log.Error(Tag, exc, "Failed to get access token for '{0}'.", Email);
                    }

                    // Failed to get token
                    if (token == null)
                    {
                        return;
                    }

                    activity = Activity as LoginActivity;
                    if (activity != null && activity.CurrentMode == Mode.Signup)
                    {
                        // Signup with Google
                        var success = await authManager.SignupWithGoogle(token);

                        if (!success)
                        {
                            GoogleAuthUtil.InvalidateToken(ctx, token);

                            new SignupFailedDialogFragment().Show(FragmentManager, "invalid_credentials_dialog");
                        }
                    }
                    else
                    {
                        // Authenticate client
                        var success = await authManager.AuthenticateWithGoogle(token);

                        if (!success)
                        {
                            GoogleAuthUtil.InvalidateToken(ctx, token);

                            new NoAccountDialogFragment().Show(FragmentManager, "invalid_credentials_dialog");
                        }
                    }
                } finally {
                    IsAuthenticating = false;
                }

                // Clean up self:
                if (Activity != null)
                {
                    FragmentManager.BeginTransaction()
                    .Remove(this)
                    .Commit();
                }

                // Try to make the activity recheck auth status
                activity = Activity as LoginActivity;
                if (activity != null)
                {
                    activity.StartAuthActivity();
                }
            }