Let the CredentialUserRoster notify login listeners of changes as opposed to the common Login API. This way, users would be notified of login events only when the changes are reflected in GoogleLogin#users.

Change-Id: Ib762842c10a61ad626a2a898fd0ed9e88c0a91cd
diff --git a/login/src/com/google/gct/login/CredentialedUserRoster.java b/login/src/com/google/gct/login/CredentialedUserRoster.java
index d383ca2..152382e 100644
--- a/login/src/com/google/gct/login/CredentialedUserRoster.java
+++ b/login/src/com/google/gct/login/CredentialedUserRoster.java
@@ -15,9 +15,11 @@
  */
 package com.google.gct.login;
 
+import com.google.common.collect.Lists;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -29,6 +31,11 @@
 public class CredentialedUserRoster {
   private final Map<String, CredentialedUser> allUsers = new HashMap<String, CredentialedUser>();
   private CredentialedUser activeUser;
+  private Collection<GoogleLoginListener> listeners;
+
+  public CredentialedUserRoster() {
+    listeners = Lists.newLinkedList();
+  }
 
   /**
    * Returns a copy of the map of the current logged in users.
@@ -82,6 +89,7 @@
       activeUser = allUsers.get(userEmail);
       activeUser.setActive(true);
       GoogleLoginPrefs.saveActiveUser(userEmail);
+      notifyLoginStatusChange();
     }
   }
 
@@ -94,6 +102,7 @@
         activeUser.setActive(false);
         activeUser = null;
         GoogleLoginPrefs.removeActiveUser();
+        notifyLoginStatusChange();
       }
     }
   }
@@ -151,8 +160,29 @@
       }
 
       allUsers.remove(userEmail);
+      notifyLoginStatusChange();
       return true;
     }
   }
 
+  /**
+   * Register a specified {@link GoogleLoginListener} to be notified of changes to the
+   * logged-in state.
+   *
+   * @param listener the specified {@code GoogleLoginListener}
+   */
+  void addLoginListener(GoogleLoginListener listener) {
+    synchronized(listeners) {
+      listeners.add(listener);
+    }
+  }
+
+  private void notifyLoginStatusChange() {
+    synchronized(listeners) {
+      for (GoogleLoginListener listener : listeners) {
+        listener.statusChanged();
+      }
+    }
+  }
+
 }
diff --git a/login/src/com/google/gct/login/GoogleLogin.java b/login/src/com/google/gct/login/GoogleLogin.java
index 8db1356..cf1ddf1 100644
--- a/login/src/com/google/gct/login/GoogleLogin.java
+++ b/login/src/com/google/gct/login/GoogleLogin.java
@@ -71,6 +71,7 @@
     this.uiFacade = new AndroidUiFacade();
     this.users = new CredentialedUserRoster();
     this.dataStore =  new AndroidPreferencesOAuthDataStore();
+    addLoginListenersFromExtensionPoints();
   }
 
   /**
@@ -419,12 +420,11 @@
   /**
    * Gets all the implementations of  {@link GoogleLoginListener} and registers them to
    * <code>state</code>.
-   * @param state the {@link GoogleLoginState} for which we want to register listeners to.
    */
-  private static void addLoginListenersFromExtensionPoints(GoogleLoginState state) {
+  private void addLoginListenersFromExtensionPoints() {
     GoogleLoginListener[] loginListeners = Extensions.getExtensions(GoogleLoginListener.EP_NAME);
     for(GoogleLoginListener listener : loginListeners) {
-      state.addLoginListener(listener);
+      users.addLoginListener(listener);
     }
   }
 
@@ -441,8 +441,6 @@
         new AndroidPreferencesOAuthDataStore(),
         uiFacade,
         new AndroidLoggerFacade());
-
-    addLoginListenersFromExtensionPoints(state);
     return state;
   }
 
diff --git a/login/src/com/google/gct/login/GoogleLoginListener.java b/login/src/com/google/gct/login/GoogleLoginListener.java
index 8964fe5..560f5f0 100644
--- a/login/src/com/google/gct/login/GoogleLoginListener.java
+++ b/login/src/com/google/gct/login/GoogleLoginListener.java
@@ -15,13 +15,17 @@
  */
 package com.google.gct.login;
 
-import com.google.gdt.eclipse.login.common.LoginListener;
 import com.intellij.openapi.extensions.ExtensionPointName;
 
 /**
  * Listener for changes in the login status.
  */
-public interface GoogleLoginListener extends LoginListener {
+public interface GoogleLoginListener {
   public static ExtensionPointName<GoogleLoginListener> EP_NAME =
     new ExtensionPointName<GoogleLoginListener>("com.google.gct.login.googleLoginListener");
+
+  /**
+   * Called when the login or active status of the user changes.
+   */
+  void statusChanged();
 }