Skip auto sign-in if the user is already logged in.

Also delay dismissing the dialog to avoid flashing it briefly.

Change-Id: I86c23ccebc6ccaad1feac56bea4ddcfefe1607f4
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index 0fce014..018744d 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -26,6 +26,7 @@
 import android.graphics.BitmapFactory;
 import android.graphics.PixelFormat;
 import android.os.Bundle;
+import android.os.Handler;
 import android.util.Log;
 import android.view.ActionMode;
 import android.view.ContextMenu;
@@ -115,7 +116,8 @@
         }
 
         String account = settings.getAutoLoginAccount(this);
-        if (settings.isAutoLoginEnabled() && account != null) {
+        if (settings.isAutoLoginEnabled() && account != null &&
+                !GoogleAccountLogin.isLoggedIn()) {
             GoogleAccountLogin login =
                     new GoogleAccountLogin(this, account);
             final ProgressDialog dialog = ProgressDialog.show(this,
@@ -125,9 +127,17 @@
                     true /* cancelable */,
                     login);
             final Bundle b = icicle;
-            final Runnable start = new Runnable() {
+            final Handler handler = new Handler();
+            final Runnable dismiss = new Runnable() {
                 @Override public void run() {
                     dialog.dismiss();
+                }
+            };
+            final Runnable start = new Runnable() {
+                @Override public void run() {
+                    // Post a delayed dismiss message to avoid a flash of the
+                    // progress dialog.
+                    handler.postDelayed(dismiss, 1000);
                     mController.start(b, getIntent());
                 }
             };
diff --git a/src/com/android/browser/GoogleAccountLogin.java b/src/com/android/browser/GoogleAccountLogin.java
index 8ff0972..04b3957 100644
--- a/src/com/android/browser/GoogleAccountLogin.java
+++ b/src/com/android/browser/GoogleAccountLogin.java
@@ -35,10 +35,11 @@
 import android.net.http.AndroidHttpClient;
 import android.net.Uri;
 import android.os.Bundle;
+import android.webkit.CookieManager;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 
-import java.util.ArrayList;
+import java.util.StringTokenizer;
 
 public class GoogleAccountLogin extends Thread implements
         AccountManagerCallback<Bundle>, OnCancelListener {
@@ -182,6 +183,22 @@
         return AccountManager.get(ctx).getAccountsByType(GOOGLE);
     }
 
+    // Checks for the presence of the SID cookie on google.com.
+    public static boolean isLoggedIn() {
+        String cookies = CookieManager.getInstance().getCookie(
+                "http://www.google.com");
+        if (cookies != null) {
+            StringTokenizer tokenizer = new StringTokenizer(cookies, ";");
+            while (tokenizer.hasMoreTokens()) {
+                String cookie = tokenizer.nextToken().trim();
+                if (cookie.startsWith("SID=")) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     // Used to indicate that the Browser should continue loading the main page.
     // This can happen on success, error, or timeout.
     private synchronized void done() {