Fix bug where apps weren't appearing on workspace after being installed

Bug: 8707110

Change-Id: Ib50e0c600a4a77450127f3947aa894346dc99a5a
diff --git a/src/com/android/launcher2/LauncherModel.java b/src/com/android/launcher2/LauncherModel.java
index 00770b2..99ebd96 100644
--- a/src/com/android/launcher2/LauncherModel.java
+++ b/src/com/android/launcher2/LauncherModel.java
@@ -82,6 +82,7 @@
     private DeferredHandler mHandler = new DeferredHandler();
     private LoaderTask mLoaderTask;
     private boolean mIsLoaderTaskRunning;
+    private volatile boolean mFlushingWorkerThread;
 
     // Specific runnable types that are run on the main thread deferred handler, this allows us to
     // clear all queued binding runnables when the Launcher activity is destroyed.
@@ -375,6 +376,35 @@
         runOnWorkerThread(r);
     }
 
+    public void flushWorkerThread() {
+        mFlushingWorkerThread = true;
+        Runnable waiter = new Runnable() {
+                public void run() {
+                    synchronized (this) {
+                        notifyAll();
+                        mFlushingWorkerThread = false;
+                    }
+                }
+            };
+
+        synchronized(waiter) {
+            runOnWorkerThread(waiter);
+            if (mLoaderTask != null) {
+                synchronized(mLoaderTask) {
+                    mLoaderTask.notify();
+                }
+            }
+            boolean success = false;
+            while (!success) {
+                try {
+                    waiter.wait();
+                    success = true;
+                } catch (InterruptedException e) {
+                }
+            }
+        }
+    }
+
     /**
      * Move an item in the DB to a new <container, screen, cellX, cellY>
      */
@@ -1004,9 +1034,11 @@
                         }
                     });
 
-                while (!mStopped && !mLoadAndBindStepFinished) {
+                while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
                     try {
-                        this.wait();
+                        // Just in case mFlushingWorkerThread changes but we aren't woken up,
+                        // wait no longer than 1sec at a time
+                        this.wait(1000);
                     } catch (InterruptedException ex) {
                         // Ignore
                     }