Fix issue where Home was not loading the workspace items.

This change also adds new logs to help track down the issue if it
happens again.
diff --git a/src/com/android/launcher/Launcher.java b/src/com/android/launcher/Launcher.java
index 3fd141d..710578f 100644
--- a/src/com/android/launcher/Launcher.java
+++ b/src/com/android/launcher/Launcher.java
@@ -1367,19 +1367,26 @@
         sModel.loadUserItems(false, this, false, false);
     }
 
-    void onDesktopItemsLoaded() {
-        if (mDestroyed) return;
-        bindDesktopItems();
+    void onDesktopItemsLoaded(ArrayList<ItemInfo> shortcuts,
+            ArrayList<LauncherAppWidgetInfo> appWidgets) {
+        if (mDestroyed) {
+            if (LauncherModel.DEBUG_LOADERS) {
+                d(LauncherModel.LOG_TAG, "  ------> destroyed, ignoring desktop items");
+            }
+            return;
+        }
+        bindDesktopItems(shortcuts, appWidgets);
     }
 
     /**
      * Refreshes the shortcuts shown on the workspace.
      */
-    private void bindDesktopItems() {
-        final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems();
-        final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets();
+    private void bindDesktopItems(ArrayList<ItemInfo> shortcuts,
+            ArrayList<LauncherAppWidgetInfo> appWidgets) {
+
         final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter();
         if (shortcuts == null || appWidgets == null || drawerAdapter == null) {
+            if (LauncherModel.DEBUG_LOADERS) d(LauncherModel.LOG_TAG, "  ------> a source is null");            
             return;
         }
 
@@ -1529,7 +1536,10 @@
             final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
             item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
 
-            if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo));
+            if (LOGD) {
+                d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
+                       appWidgetId, appWidgetInfo));
+            }
 
             item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
             item.hostView.setTag(item);
@@ -2171,9 +2181,15 @@
                     mAppWidgets.addLast(appWidgetInfo);
                 }
             }
+
+            if (LauncherModel.DEBUG_LOADERS) {
+                d(Launcher.LOG_TAG, "------> binding " + shortcuts.size() + " items");
+                d(Launcher.LOG_TAG, "------> binding " + appWidgets.size() + " widgets");
+            }
         }
 
         public void startBindingItems() {
+            if (LauncherModel.DEBUG_LOADERS) d(Launcher.LOG_TAG, "------> start binding items");
             obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
         }
 
diff --git a/src/com/android/launcher/LauncherModel.java b/src/com/android/launcher/LauncherModel.java
index 15cd942..d69bd13 100644
--- a/src/com/android/launcher/LauncherModel.java
+++ b/src/com/android/launcher/LauncherModel.java
@@ -76,12 +76,16 @@
             new HashMap<ComponentName, ApplicationInfo>(INITIAL_ICON_CACHE_CAPACITY);
 
     synchronized void abortLoaders() {
+        if (DEBUG_LOADERS) d(LOG_TAG, "aborting loaders");
+
         if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) {
+            if (DEBUG_LOADERS) d(LOG_TAG, "  --> aborting applications loader");
             mApplicationsLoader.stop();
             mApplicationsLoaded = false;
         }
 
         if (mDesktopItemsLoader != null && mDesktopItemsLoader.isRunning()) {
+            if (DEBUG_LOADERS) d(LOG_TAG, "  --> aborting workspace loader");
             mDesktopItemsLoader.stop();
             mDesktopItemsLoaded = false;
         }
@@ -474,6 +478,7 @@
     }
 
     private static final AtomicInteger sAppsLoaderCount = new AtomicInteger(1);
+    private static final AtomicInteger sWorkspaceLoaderCount = new AtomicInteger(1);
 
     private class ApplicationsLoader implements Runnable {
         private final WeakReference<Launcher> mLauncher;
@@ -612,7 +617,7 @@
             if (DEBUG_LOADERS) d(LOG_TAG, "  --> items loaded, return");
             if (loadApplications) startApplicationsLoader(launcher, true);
             // We have already loaded our data from the DB
-            launcher.onDesktopItemsLoaded();
+            launcher.onDesktopItemsLoaded(mDesktopItems, mDesktopAppWidgets);
             return;
         }
 
@@ -720,6 +725,7 @@
         private final boolean mLocaleChanged;
         private final boolean mLoadApplications;
         private final boolean mIsLaunching;
+        private final int mId;        
 
         DesktopItemsLoader(Launcher launcher, boolean localeChanged, boolean loadApplications,
                 boolean isLaunching) {
@@ -727,6 +733,7 @@
             mIsLaunching = isLaunching;
             mLauncher = new WeakReference<Launcher>(launcher);
             mLocaleChanged = localeChanged;
+            mId = sWorkspaceLoaderCount.getAndIncrement();
         }
 
         void stop() {
@@ -738,6 +745,8 @@
         }
 
         public void run() {
+            if (DEBUG_LOADERS) d(LOG_TAG, "  ----> running workspace loader (" + mId + ")");
+
             mRunning = true;
 
             android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
@@ -943,16 +952,39 @@
             }
 
             if (!mStopped) {
-                launcher.runOnUiThread(new Runnable() {
-                    public void run() {
-                        launcher.onDesktopItemsLoaded();
-                    }
-                });
-                if (mLoadApplications) startApplicationsLoader(launcher, mIsLaunching);
-            }
+                if (DEBUG_LOADERS)  {
+                    d(LOG_TAG, "  --> done loading workspace");
+                    d(LOG_TAG, "  ----> worskpace items=" + desktopItems.size());                
+                    d(LOG_TAG, "  ----> worskpace widgets=" + desktopAppWidgets.size());
+                }
 
-            if (!mStopped) {
+                // Create a copy of the lists in case the workspace loader is restarted
+                // and the list are cleared before the UI can go through them
+                final ArrayList<ItemInfo> uiDesktopItems =
+                        new ArrayList<ItemInfo>(desktopItems);
+                final ArrayList<LauncherAppWidgetInfo> uiDesktopWidgets =
+                        new ArrayList<LauncherAppWidgetInfo>(desktopAppWidgets);
+
+                if (!mStopped) {
+                    d(LOG_TAG, "  ----> items cloned, ready to refresh UI");                
+                    launcher.runOnUiThread(new Runnable() {
+                        public void run() {
+                            if (DEBUG_LOADERS) d(LOG_TAG, "  ----> onDesktopItemsLoaded()");
+                            launcher.onDesktopItemsLoaded(uiDesktopItems, uiDesktopWidgets);
+                        }
+                    });
+                }
+
+                if (mLoadApplications) {
+                    if (DEBUG_LOADERS) {
+                        d(LOG_TAG, "  ----> loading applications from workspace loader");
+                    }
+                    startApplicationsLoader(launcher, mIsLaunching);
+                }
+
                 mDesktopItemsLoaded = true;
+            } else {
+                if (DEBUG_LOADERS) d(LOG_TAG, "  ----> worskpace loader was stopped");
             }
             mRunning = false;
         }