Merge changes I78550965,I6e0fe0ba,Ia6db6cf3

* changes:
  Cache the factory reset URL.
  Fix StrictMode violation in InstantSearchEngine
  Query Private Browsing state for NFC on UI thread.
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index a4f0f04..2023ee6 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -103,7 +103,7 @@
     private WebStorageSizeManager mWebStorageSizeManager;
     private AutofillHandler mAutofillHandler;
     private WeakHashMap<WebSettings, String> mCustomUserAgents;
-    private boolean mInitialized = false;
+    private static boolean sInitialized = false;
     // Looper shared between some lightweight background operations
     // Specifically, this is created on the thread that initializes browser settings
     // and is then reused by CrashRecoveryHandler
@@ -116,6 +116,8 @@
     // Cached settings
     private SearchEngine mSearchEngine;
 
+    private static String sFactoryResetUrl;
+
     public static void initialize(final Context context) {
         sInstance = new BrowserSettings(context);
     }
@@ -199,21 +201,28 @@
                 }
                 mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
             }
+
+            sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base);
+            if (sFactoryResetUrl.indexOf("{CID}") != -1) {
+                sFactoryResetUrl = sFactoryResetUrl.replace("{CID}",
+                    BrowserProvider.getClientId(mContext.getContentResolver()));
+            }
+
             Looper.prepare();
             mBackgroundLooper = Looper.myLooper();
-            synchronized (BrowserSettings.this) {
-                mInitialized = true;
-                BrowserSettings.this.notifyAll();
+            synchronized (BrowserSettings.class) {
+                sInitialized = true;
+                BrowserSettings.class.notifyAll();
             }
             Looper.loop();
         }
     };
 
-    void requireInitialization() {
-        synchronized (BrowserSettings.this) {
-            while (!mInitialized) {
+    private static void requireInitialization() {
+        synchronized (BrowserSettings.class) {
+            while (!sInitialized) {
                 try {
-                    BrowserSettings.this.wait();
+                    BrowserSettings.class.wait();
                 } catch (InterruptedException e) {
                 }
             }
@@ -334,12 +343,8 @@
     }
 
     public static String getFactoryResetHomeUrl(Context context) {
-        String url = context.getResources().getString(R.string.homepage_base);
-        if (url.indexOf("{CID}") != -1) {
-            url = url.replace("{CID}",
-                    BrowserProvider.getClientId(context.getContentResolver()));
-        }
-        return url;
+        requireInitialization();
+        return sFactoryResetUrl;
     }
 
     public LayoutAlgorithm getLayoutAlgorithm() {
diff --git a/src/com/android/browser/InstantSearchEngine.java b/src/com/android/browser/InstantSearchEngine.java
index c913494..4dd83fa 100644
--- a/src/com/android/browser/InstantSearchEngine.java
+++ b/src/com/android/browser/InstantSearchEngine.java
@@ -199,7 +199,7 @@
             return false;
         }
 
-        final String currentUrl = current.getUrl();
+        final String currentUrl = mController.getCurrentTab().getUrl();
 
         if (currentUrl != null) {
             Uri uri = Uri.parse(currentUrl);
diff --git a/src/com/android/browser/NfcHandler.java b/src/com/android/browser/NfcHandler.java
index bc19950..a8e11d3 100644
--- a/src/com/android/browser/NfcHandler.java
+++ b/src/com/android/browser/NfcHandler.java
@@ -21,6 +21,10 @@
 import android.nfc.NdefRecord;
 import android.nfc.NfcAdapter;
 import android.os.AsyncTask;
+import android.os.Handler;
+import android.os.Message;
+
+import java.util.concurrent.CountDownLatch;
 
 /** This class implements sharing the URL of the currently
   * shown browser page over NFC. Sharing is only active
@@ -31,42 +35,26 @@
     private NfcAdapter mNfcAdapter;
     private Activity mActivity;
     private Controller mController;
+    private Handler mHandler;
+    private Tab mCurrentTab;
+    private boolean mIsPrivate;
+    private CountDownLatch mPrivateBrowsingSignal;
 
-    /** We need an async task to check whether the tab is private
-      * on the UI thread.
-      */
-    private class CreateMessageTask extends AsyncTask<Void, Void, NdefMessage> {
-        private boolean mIsPrivate = false;
-        private Tab mCurrentTab;
-
-        @Override
-        protected void onPreExecute() {
-            mCurrentTab = mController.getCurrentTab();
-            if ((mCurrentTab != null) && (mCurrentTab.getWebView() != null)) {
-                mIsPrivate = mCurrentTab.getWebView().isPrivateBrowsingEnabled();
-            }
-        }
-
-        @Override
-        protected NdefMessage doInBackground(Void... params) {
-            if ((mCurrentTab == null) || mIsPrivate) {
-                return null;
-            }
-            String currentUrl = mCurrentTab.getUrl();
-            if (currentUrl != null) {
-                NdefRecord record = NdefRecord.createUri(currentUrl);
-                NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
-                return msg;
-            } else {
-                return null;
-            }
-        }
-    }
+    private static final int GET_PRIVATE_BROWSING_STATE_MSG = 100;
 
     public NfcHandler(Activity browser, Controller controller) {
         mActivity = browser;
         mController = controller;
         mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity);
+        mHandler = new Handler() {
+            @Override
+            public void handleMessage(Message msg) {
+                if (msg.what == GET_PRIVATE_BROWSING_STATE_MSG) {
+                    mIsPrivate = mCurrentTab.getWebView().isPrivateBrowsingEnabled();
+                    mPrivateBrowsingSignal.countDown();
+                }
+            }
+        };
     }
 
     void onPause() {
@@ -83,11 +71,29 @@
 
     @Override
     public NdefMessage createMessage() {
-        CreateMessageTask task = new CreateMessageTask();
-        task.execute();
-        try {
-            return task.get();
-        } catch (Exception e) {
+        mCurrentTab = mController.getCurrentTab();
+        if ((mCurrentTab != null) && (mCurrentTab.getWebView() != null)) {
+            // We can only read the WebView state on the UI thread, so post
+            // a message and wait.
+            mPrivateBrowsingSignal = new CountDownLatch(1);
+            mHandler.sendMessage(mHandler.obtainMessage(GET_PRIVATE_BROWSING_STATE_MSG));
+            try {
+                mPrivateBrowsingSignal.await();
+            } catch (InterruptedException e) {
+                return null;
+            }
+        }
+
+        if ((mCurrentTab == null) || mIsPrivate) {
+            return null;
+        }
+
+        String currentUrl = mCurrentTab.getUrl();
+        if (currentUrl != null) {
+            NdefRecord record = NdefRecord.createUri(currentUrl);
+            NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
+            return msg;
+        } else {
             return null;
         }
     }