Remove pre-login on every tab.

Only attempt pre-login once unless the Browser's data is cleared.  Remove
preferences for pre-login and code to detect cookies.

Bug: 3367381
Change-Id: I321a7c09be27ff6f3f570d0b9963ee2984b01c4a
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index 75e0cfb..357d1e9 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -92,9 +92,6 @@
     private String databasePath; // default value set in loadFromDb()
     private String geolocationDatabasePath; // default value set in loadFromDb()
     private WebStorageSizeManager webStorageSizeManager;
-    // Autologin settings
-    private boolean autoLoginEnabled;
-    private String autoLoginAccount;
 
     private String jsFlags = "";
 
@@ -168,8 +165,6 @@
 
     public final static String PREF_QUICK_CONTROLS = "enable_quick_controls";
     public final static String PREF_MOST_VISITED_HOMEPAGE = "use_most_visited_homepage";
-    public final static String PREF_AUTOLOGIN = "enable_autologin";
-    public final static String PREF_AUTOLOGIN_ACCOUNT = "autologin_account";
     public final static String PREF_PLUGIN_STATE = "plugin_state";
     public final static String PREF_USE_INSTANT = "use_instant_search";
 
@@ -537,11 +532,6 @@
         geolocationEnabled = p.getBoolean("enable_geolocation", geolocationEnabled);
         workersEnabled = p.getBoolean("enable_workers", workersEnabled);
 
-        // Autologin account settings.  The account preference may be null until
-        // the user explicitly changes the account in the settings.
-        autoLoginEnabled = p.getBoolean(PREF_AUTOLOGIN, autoLoginEnabled);
-        autoLoginAccount = p.getString(PREF_AUTOLOGIN_ACCOUNT, autoLoginAccount);
-
         update();
     }
 
@@ -632,32 +622,6 @@
         update();
     }
 
-    public boolean isAutoLoginEnabled() {
-        return autoLoginEnabled;
-    }
-
-    public String getAutoLoginAccount(Context context) {
-        // Each time we attempt to get the account, we need to verify that the
-        // account is still valid.
-        return GoogleAccountLogin.validateAccount(context, autoLoginAccount);
-    }
-
-    public void setAutoLoginAccount(Context context, String name) {
-        Editor ed = PreferenceManager.
-                getDefaultSharedPreferences(context).edit();
-        ed.putString(PREF_AUTOLOGIN_ACCOUNT, name);
-        ed.apply();
-        autoLoginAccount = name;
-    }
-
-    public void setAutoLoginEnabled(Context context, boolean enable) {
-        Editor ed = PreferenceManager.
-                getDefaultSharedPreferences(context).edit();
-        ed.putBoolean(PREF_AUTOLOGIN, enable);
-        ed.apply();
-        autoLoginEnabled = enable;
-    }
-
     public void setAutoFillProfile(Context ctx, AutoFillProfile profile, Message msg) {
         if (profile != null) {
             setActiveAutoFillProfileId(ctx, profile.getUniqueId());
@@ -854,9 +818,6 @@
         domStorageEnabled = true;
         geolocationEnabled = true;
         workersEnabled = true;  // only affects V8. JSC does not have a similar setting
-        // Autologin default is true.  The account will be populated when
-        // reading from the DB as that is when a context is available.
-        autoLoginEnabled = true;
     }
 
     private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index b04c956..82aea47 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -279,7 +279,7 @@
             CookieManager.getInstance().removeSessionCookie();
         }
 
-        GoogleAccountLogin.startLoginIfNeeded(mActivity, mSettings,
+        GoogleAccountLogin.startLoginIfNeeded(mActivity,
                 new Runnable() {
                     @Override public void run() {
                         start(icicle, intent, currentTab, restoreIncognitoTabs);
@@ -2229,17 +2229,9 @@
             // animation behavior.
             addTab(tab);
             setActiveTab(tab);
-
-            // Callback to load the url data.
-            final Runnable load = new Runnable() {
-                @Override public void run() {
-                    if (!urlData.isEmpty()) {
-                        loadUrlDataIn(tab, urlData);
-                    }
-                }
-            };
-
-            GoogleAccountLogin.startLoginIfNeeded(mActivity, mSettings, load);
+            if (!urlData.isEmpty()) {
+                loadUrlDataIn(tab, urlData);
+            }
             return tab;
         } else {
             // Get rid of the subwindow if it exists
diff --git a/src/com/android/browser/GoogleAccountLogin.java b/src/com/android/browser/GoogleAccountLogin.java
index f4ccfd1..0bde010 100644
--- a/src/com/android/browser/GoogleAccountLogin.java
+++ b/src/com/android/browser/GoogleAccountLogin.java
@@ -40,7 +40,6 @@
 import android.os.Handler;
 import android.preference.PreferenceManager;
 import android.util.Log;
-import android.webkit.CookieManager;
 import android.webkit.CookieSyncManager;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
@@ -80,10 +79,10 @@
     private int mState;  // {NONE(0), SID(1), LSID(2)}
     private boolean mTokensInvalidated;
 
-    private GoogleAccountLogin(Activity activity, String name,
+    private GoogleAccountLogin(Activity activity, Account account,
             Runnable runnable) {
         mActivity = activity;
-        mAccount = new Account(name, GOOGLE);
+        mAccount = account;
         mWebView = new WebView(mActivity);
         mRunnable = runnable;
 
@@ -233,28 +232,22 @@
     // Start the login process if auto-login is enabled and the user is not
     // already logged in.
     public static void startLoginIfNeeded(Activity activity,
-            BrowserSettings settings, Runnable runnable) {
-        // Auto login not enabled?
-        if (!settings.isAutoLoginEnabled()) {
-            runnable.run();
-            return;
-        }
-
-        // No account found?
-        String account = settings.getAutoLoginAccount(activity);
-        if (account == null) {
-            runnable.run();
-            return;
-        }
-
+            Runnable runnable) {
         // Already logged in?
         if (isLoggedIn(activity)) {
             runnable.run();
             return;
         }
 
+        // No account found?
+        Account[] accounts = getAccounts(activity);
+        if (accounts == null || accounts.length == 0) {
+            runnable.run();
+            return;
+        }
+
         GoogleAccountLogin login =
-                new GoogleAccountLogin(activity, account, runnable);
+                new GoogleAccountLogin(activity, accounts[0], runnable);
         login.startLogin();
     }
 
@@ -271,31 +264,12 @@
                 mAccount, "SID", null, mActivity, this, null);
     }
 
-    // Returns the account name passed in if the account exists, otherwise
-    // returns the default account.
-    public static String validateAccount(Context ctx, String name) {
-        Account[] accounts = getAccounts(ctx);
-        if (accounts.length == 0) {
-            return null;
-        }
-        if (name != null) {
-            // Make sure the account still exists.
-            for (Account a : accounts) {
-                if (a.name.equals(name)) {
-                    return name;
-                }
-            }
-        }
-        // Return the first entry.
-        return accounts[0].name;
-    }
-
-    public static Account[] getAccounts(Context ctx) {
+    private static Account[] getAccounts(Context ctx) {
         return AccountManager.get(ctx).getAccountsByType(GOOGLE);
     }
 
-    // Checks for the presence of the SID cookie on google.com.
-    public static boolean isLoggedIn(Context ctx) {
+    // Checks if we already did pre-login.
+    private static boolean isLoggedIn(Context ctx) {
         // See if we last logged in less than a week ago.
         long lastLogin = PreferenceManager.
                 getDefaultSharedPreferences(ctx).
@@ -303,31 +277,7 @@
         if (lastLogin == -1) {
             return false;
         }
-        long diff = System.currentTimeMillis() - lastLogin;
-        if (diff > WEEK_IN_MILLIS) {
-            Log.d(LOGTAG, "Forcing login after " + diff + "ms");
-            return false;
-        }
-
-        // This will potentially block the UI thread but we have to have the
-        // most updated cookies.
-        // FIXME: Figure out how to avoid waiting to clear session cookies.
-        CookieManager.getInstance().waitForCookieOperationsToComplete();
-
-        // Use /a/ to grab hosted cookies as well as the base set of google.com
-        // cookies.
-        String cookies = CookieManager.getInstance().getCookie(
-                "http://www.google.com/a/");
-        if (cookies != null) {
-            StringTokenizer tokenizer = new StringTokenizer(cookies, ";");
-            while (tokenizer.hasMoreTokens()) {
-                String cookie = tokenizer.nextToken().trim();
-                if (cookie.startsWith("SID=") || cookie.startsWith("ASIDAP=")) {
-                    return true;
-                }
-            }
-        }
-        return false;
+        return true;
     }
 
     // Used to indicate that the Browser should continue loading the main page.
diff --git a/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java b/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java
index 2b2ee3e..2266608 100644
--- a/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java
+++ b/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java
@@ -17,14 +17,11 @@
 package com.android.browser.preferences;
 
 import com.android.browser.BrowserSettings;
-import com.android.browser.GoogleAccountLogin;
 import com.android.browser.R;
 
-import android.accounts.Account;
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
-import android.preference.ListPreference;
 import android.preference.Preference;
 import android.preference.PreferenceFragment;
 
@@ -44,53 +41,11 @@
 
         Preference e = findPreference(BrowserSettings.PREF_CLEAR_HISTORY);
         e.setOnPreferenceChangeListener(this);
-        setupAutoLoginPreference();
     }
 
     @Override
     public void onResume() {
         super.onResume();
-        setupAutoLoginPreference();
-    }
-
-    void setupAutoLoginPreference() {
-        ListPreference autologinPref = (ListPreference) findPreference(
-                BrowserSettings.PREF_AUTOLOGIN_ACCOUNT);
-        autologinPref.setOnPreferenceChangeListener(this);
-        updateAutoLoginSummary(autologinPref);
-        Account[] accounts = GoogleAccountLogin.getAccounts(getActivity());
-        // +1 for disable
-        CharSequence[] names = new CharSequence[accounts.length + 1];
-        CharSequence[] values = new CharSequence[names.length];
-        int i = 0;
-        int defaultAccount = 0;
-        for (Account a : accounts) {
-            values[i] = names[i] = a.name;
-            i++;
-        }
-        names[i] = getResources().getString(R.string.pref_autologin_disable);
-        values[i] = "";
-        autologinPref.setEntries(names);
-        autologinPref.setEntryValues(values);
-        BrowserSettings bs = BrowserSettings.getInstance();
-        if (bs.isAutoLoginEnabled()) {
-            autologinPref.setValue(bs.getAutoLoginAccount(getActivity()));
-        } else {
-            autologinPref.setValue("");
-        }
-    }
-
-    private void updateAutoLoginSummary(Preference pref) {
-        if (!mSettings.isAutoLoginEnabled()) {
-            pref.setSummary(R.string.pref_autologin_disable);
-        } else {
-            String account = mSettings.getAutoLoginAccount(getActivity());
-            if (account == null) {
-                pref.setSummary(R.string.pref_autologin_no_account);
-            } else {
-                pref.setSummary(getString(R.string.pref_autologin_summary, account));
-            }
-        }
     }
 
     @Override
@@ -102,17 +57,6 @@
             getActivity().setResult(Activity.RESULT_OK, (new Intent()).putExtra(Intent.EXTRA_TEXT,
                     pref.getKey()));
             return true;
-        } else if (pref.getKey().equals(BrowserSettings.PREF_AUTOLOGIN_ACCOUNT)) {
-            String account = (String) objValue;
-            if (account.length() == 0) {
-                // Disable
-                mSettings.setAutoLoginEnabled(getActivity(), false);
-            } else {
-                mSettings.setAutoLoginEnabled(getActivity(), true);
-            }
-            mSettings.setAutoLoginAccount(getActivity(), account);
-            updateAutoLoginSummary(pref);
-            return true;
         }
 
         return false;