When the launcher is paused and we reload stuff in the background, we need to re-re-load it in onResume.

Otherwise we can load widgets and other resources from the wrong Configuration.
This doesn't completely fix the bug, but it makes it much less likely.  We tell
the launcher once at the beginning of starting a reload because of SD cards coming
back, and once when we bind.

cherry pick of I99ee6af38bef91e261832bad4dec978a5d4a8b3d

Bug: 3126698
Change-Id: I917bdb3982e3eea4924c6e9a8f3c037fd493f415
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 5d45d27..444357e 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -217,6 +217,7 @@
     private boolean mPaused = true;
     private boolean mRestoring;
     private boolean mWaitingForResult;
+    private boolean mOnResumeNeedsLoad;
 
     private Bundle mSavedInstanceState;
 
@@ -680,13 +681,12 @@
     @Override
     protected void onResume() {
         super.onResume();
-
         mPaused = false;
-
-        if (mRestoring) {
+        if (mRestoring || mOnResumeNeedsLoad) {
             mWorkspaceLoading = true;
             mModel.startLoader(this, true);
             mRestoring = false;
+            mOnResumeNeedsLoad = false;
         }
         // When we resume Launcher, a different Activity might be responsible for the app
         // market intent, so refresh the icon
@@ -703,6 +703,7 @@
         if (mNextView != null) {
             dismissPreview(mNextView);
         }
+        mPaused = true;
         mDragController.cancelDrag();
     }
 
@@ -2870,6 +2871,30 @@
     }
 
     /**
+     * If the activity is currently paused, signal that we need to re-run the loader
+     * in onResume.
+     *
+     * This needs to be called from incoming places where resources might have been loaded
+     * while we are paused.  That is becaues the Configuration might be wrong
+     * when we're not running, and if it comes back to what it was when we
+     * were paused, we are not restarted.
+     *
+     * Implementation of the method from LauncherModel.Callbacks.
+     *
+     * @return true if we are currently paused.  The caller might be able to
+     * skip some work in that case since we will come back again.
+     */
+    public boolean setLoadOnResume() {
+        if (mPaused) {
+            Log.i(TAG, "setLoadOnResume");
+            mOnResumeNeedsLoad = true;
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    /**
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public int getCurrentWorkspaceScreen() {
@@ -2917,6 +2942,8 @@
      */
     public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
 
+        setLoadOnResume();
+
         final Workspace workspace = mWorkspace;
 
         for (int i=start; i<end; i++) {
@@ -2954,6 +2981,7 @@
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public void bindFolders(HashMap<Long, FolderInfo> folders) {
+        setLoadOnResume();
         sFolders.clear();
         sFolders.putAll(folders);
     }
@@ -2964,6 +2992,8 @@
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public void bindAppWidget(LauncherAppWidgetInfo item) {
+        setLoadOnResume();
+
         final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
         if (DEBUG_WIDGETS) {
             Log.d(TAG, "bindAppWidget: " + item);
@@ -3000,6 +3030,8 @@
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public void finishBindingItems() {
+        setLoadOnResume();
+
         if (mSavedState != null) {
             if (!mWorkspace.hasFocus()) {
                 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
@@ -3050,6 +3082,7 @@
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
+        setLoadOnResume();
         removeDialog(DIALOG_CREATE_SHORTCUT);
         mAllAppsGrid.addApps(apps);
         if (mCustomizePagedView != null) {
@@ -3065,6 +3098,7 @@
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
+        setLoadOnResume();
         removeDialog(DIALOG_CREATE_SHORTCUT);
         mWorkspace.updateShortcuts(apps);
         mAllAppsGrid.updateApps(apps);