Move access to the WebIconDatabase from the UI thread.

the retainIconsOnStartup function was accessing the
WebIconDatabase on the UI thread which is a strict mode
violation. Move that database access into an AsyncTask.

Change-Id: I4f03680d00b7678a89e41f94201ca5309891a8d9
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index d30ffb2..acd76dd 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -363,23 +363,40 @@
     }
 
     // Open the icon database and retain all the icons for visited sites.
+    // This is done on a background thread so as not to stall startup.
     private void retainIconsOnStartup() {
-        final WebIconDatabase db = WebIconDatabase.getInstance();
-        db.open(mActivity.getDir("icons", 0).getPath());
-        Cursor c = null;
-        try {
-            c = Browser.getAllBookmarks(mActivity.getContentResolver());
-            if (c.moveToFirst()) {
-                int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
-                do {
-                    String url = c.getString(urlIndex);
-                    db.retainIconForPageUrl(url);
-                } while (c.moveToNext());
+        // WebIconDatabase needs to be retrieved on the UI thread so that if
+        // it has not been created successfully yet the Handler is started on the
+        // UI thread.
+        new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
+    }
+
+    private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
+        private WebIconDatabase mDb;
+
+        public RetainIconsOnStartupTask(WebIconDatabase db) {
+            mDb = db;
+        }
+
+        protected Void doInBackground(Void... unused) {
+            mDb.open(mActivity.getDir("icons", 0).getPath());
+            Cursor c = null;
+            try {
+                c = Browser.getAllBookmarks(mActivity.getContentResolver());
+                if (c.moveToFirst()) {
+                    int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
+                    do {
+                        String url = c.getString(urlIndex);
+                        mDb.retainIconForPageUrl(url);
+                    } while (c.moveToNext());
+                }
+            } catch (IllegalStateException e) {
+                Log.e(LOGTAG, "retainIconsOnStartup", e);
+            } finally {
+                if (c != null) c.close();
             }
-        } catch (IllegalStateException e) {
-            Log.e(LOGTAG, "retainIconsOnStartup", e);
-        } finally {
-            if (c!= null) c.close();
+
+            return null;
         }
     }