diff options
Diffstat (limited to 'src/com/google/gct/login/GoogleLogin.java')
-rw-r--r-- | src/com/google/gct/login/GoogleLogin.java | 76 |
1 files changed, 43 insertions, 33 deletions
diff --git a/src/com/google/gct/login/GoogleLogin.java b/src/com/google/gct/login/GoogleLogin.java index 7ddf670..4676152 100644 --- a/src/com/google/gct/login/GoogleLogin.java +++ b/src/com/google/gct/login/GoogleLogin.java @@ -70,9 +70,6 @@ public class GoogleLogin { public static final Logger LOG = Logger.getInstance(GoogleLogin.class); - /** - * Constructor - */ private GoogleLogin() { this.clientInfo = getClientInfo(); this.uiFacade = new AndroidUiFacade(); @@ -85,8 +82,9 @@ public class GoogleLogin { * Gets the {@link GoogleLogin} object. * @return the {@link GoogleLogin} object. */ + @NotNull public static GoogleLogin getInstance() { - if(instance == null) { + if (instance == null) { instance = new GoogleLogin(); instance.dataStore.initializeUsers(); } @@ -110,10 +108,10 @@ public class GoogleLogin { * either succeeds or fails. * @throws InvalidThreadTypeException */ - public static void promptToLogIn(final String message, @Nullable final IGoogleLoginCompletedCallback callback) + public static void promptToLogIn(@Nullable final String message, @Nullable final IGoogleLoginCompletedCallback callback) throws InvalidThreadTypeException { if (!instance.isLoggedIn()) { - if(ApplicationManager.getApplication().isDispatchThread()) { + if (ApplicationManager.getApplication().isDispatchThread()) { getInstance().logIn(message, callback); } else { throw new InvalidThreadTypeException("promptToLogin"); @@ -135,6 +133,7 @@ public class GoogleLogin { * @return An HttpRequestFactory object that has been signed with the active user's * authentication headers or null if there is no active user. */ + @Nullable public HttpRequestFactory createRequestFactory() { return createRequestFactory(null); } @@ -156,9 +155,10 @@ public class GoogleLogin { * @return An HttpRequestFactory object that has been signed with the active user's * authentication headers or null if there is no active user. */ - public HttpRequestFactory createRequestFactory(String message) { + @Nullable + public HttpRequestFactory createRequestFactory(@Nullable String message) { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { // TODO: prompt user to select an existing user or sign in return null; } @@ -173,9 +173,10 @@ public class GoogleLogin { * @throws IOException if something goes wrong while fetching the token. * */ + @Nullable public String fetchAccessToken() throws IOException { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { return null; } return activeUser.getGoogleLoginState().fetchAccessToken(); @@ -185,9 +186,10 @@ public class GoogleLogin { * Returns the OAuth2 Client ID for the active user. * @return the OAuth2 Client ID for the active user. */ + @Nullable public String fetchOAuth2ClientId() { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { return null; } return activeUser.getGoogleLoginState().fetchOAuth2ClientId(); @@ -197,9 +199,10 @@ public class GoogleLogin { * Returns the OAuth2 Client Secret for the active user. * @return the OAuth2 Client Secret for the active user. */ + @Nullable public String fetchOAuth2ClientSecret() { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { return null; } return activeUser.getGoogleLoginState().fetchOAuth2ClientSecret(); @@ -212,9 +215,10 @@ public class GoogleLogin { * * @return the refresh token, or {@code null} if the user cancels out of a request to log in */ + @Nullable public String fetchOAuth2RefreshToken() { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { return null; } return activeUser.getGoogleLoginState().fetchOAuth2RefreshToken(); @@ -229,9 +233,10 @@ public class GoogleLogin { * @throws IOException if something goes wrong while fetching the token. * */ + @Nullable public String fetchOAuth2Token() throws IOException { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { return null; } return activeUser.getGoogleLoginState().fetchOAuth2Token(); @@ -242,9 +247,10 @@ public class GoogleLogin { * returns credentials with the access token and refresh token set to null. * @return the OAuth credentials. */ + @Nullable public Credential getCredential() { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { return null; } return activeUser.getGoogleLoginState().getCredential(); @@ -254,9 +260,10 @@ public class GoogleLogin { * Returns the active user's email address, or null if there is no active user, * @return the active user's email address, or null if there is no active user, */ + @Nullable public String getEmail() { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { return null; } return activeUser.getGoogleLoginState().getEmail(); @@ -331,7 +338,7 @@ public class GoogleLogin { } }); - loggedIn = state.logInWithLocalServer(message); + loggedIn = state != null && state.logInWithLocalServer(message); } @Override @@ -383,7 +390,7 @@ public class GoogleLogin { */ public boolean logOut() { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { return false; } @@ -405,7 +412,7 @@ public class GoogleLogin { */ public boolean logOut(boolean showPrompt) { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { return false; } return activeUser.getGoogleLoginState().logOut(showPrompt); @@ -417,13 +424,13 @@ public class GoogleLogin { * the access and refresh token will be set to null. * @return a new {@link Credential}. */ + @Nullable public Credential makeCredential() { CredentialedUser activeUser = users.getActiveUser(); - if(activeUser == null) { + if (activeUser == null) { return null; - } else { - return activeUser.getGoogleLoginState().makeCredential(); } + return activeUser.getGoogleLoginState().makeCredential(); } /** @@ -502,7 +509,7 @@ public class GoogleLogin { uiFacade, new AndroidLoggerFacade()); - if(initializingUsers && !state.isLoggedIn()) { + if (initializingUsers && !state.isLoggedIn()) { // Logs user out if oauth scope for active user's credentials // does not match the current scope return null; @@ -515,7 +522,7 @@ public class GoogleLogin { * Returns the Client Info for Android Studio in a {@link com.google.gct.login.GoogleLogin.ClientInfo}. * @return the Client Info for Android Studio in a {@link com.google.gct.login.GoogleLogin.ClientInfo}. */ - private ClientInfo getClientInfo() { + private static ClientInfo getClientInfo() { String id = LoginContext.getId(); String info = LoginContext.getInfo(); if (id != null && id.trim().length() > 0 @@ -527,7 +534,7 @@ public class GoogleLogin { } // TODO: update code to specify parent - private void logErrorAndDisplayDialog(@NotNull final String title, @NotNull final Exception exception) { + private static void logErrorAndDisplayDialog(@NotNull final String title, @NotNull final Exception exception) { LOG.error(exception.getMessage(), exception); GoogleLoginUtils.showErrorDialog(exception.getMessage(), title); } @@ -627,13 +634,17 @@ public class GoogleLogin { @Override public boolean askYesOrNo(String title, String message) { String updatedMessage = message; - if(message.equals("Are you sure you want to sign out?")) { + if (message.equals("Are you sure you want to sign out?")) { + updatedMessage = "Are you sure you want to sign out"; CredentialedUser activeUser = getActiveUser(); - String name = activeUser.getName().isEmpty() ? "" : activeUser.getName() + " "; - updatedMessage = "Are you sure you want to sign out " + name - + "(" + activeUser.getEmail() + ")?"; + if (activeUser != null && !Strings.isNullOrEmpty(activeUser.getName())) { + updatedMessage += (" " + activeUser.getName()); + } + if (activeUser != null && !Strings.isNullOrEmpty(activeUser.getEmail())) { + updatedMessage += (" (" + activeUser.getEmail() + ")"); + } + updatedMessage += "?"; } - Icon icon = IconLoader.getIcon(GOOGLE_IMG); return (Messages.showYesNoDialog(updatedMessage, title, icon) == Messages.YES); } @@ -685,7 +696,7 @@ public class GoogleLogin { List<String> allUsers = GoogleLoginPrefs.getStoredUsers(); String removedUsers = ""; - for(String aUser : allUsers) { + for (String aUser : allUsers) { // Add a new user, so that loadOAuth called from the GoogleLoginState constructor // will be able to create a customized key to get that user's OAuth data // This will be overwritten with new GoogleLoginState object @@ -728,9 +739,8 @@ public class GoogleLogin { } // Log removed users - if(!removedUsers.isEmpty()) { - String message = "The following user(s) had expired authentication scopes: " + removedUsers - + "and have been logged out."; + if (!removedUsers.isEmpty()) { + String message = "The following user(s) had expired authentication scopes: " + removedUsers + "and have been logged out."; GoogleLoginUtils.showErrorDialog(message, "Google Login"); } } |