Fix widget tray crash on screen rotation introduced by
ag/694693

b/21402209

Change-Id: Idae97fca971d90f5fbba658411491147cb023c8d
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index 5002538..e293c2a 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -139,6 +139,8 @@
 
     // < only access in worker thread >
     AllAppsList mBgAllAppsList;
+    // Entire list of widgets.
+    WidgetsModel mBgWidgetsModel;
 
     // The lock that must be acquired before referencing any static bg data structures.  Unlike
     // other locks, this one can generally be held long-term because we never expect any of these
@@ -2779,12 +2781,14 @@
             @SuppressWarnings("unchecked")
             final ArrayList<AppInfo> list
                     = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
+            final WidgetsModel widgetList = mBgWidgetsModel.clone();
             Runnable r = new Runnable() {
                 public void run() {
                     final long t = SystemClock.uptimeMillis();
                     final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
                     if (callbacks != null) {
                         callbacks.bindAllApplications(list);
+                        callbacks.bindAllPackages(widgetList);
                     }
                     if (DEBUG_LOADERS) {
                         Log.d(TAG, "bound all " + list.size() + " apps from cache in "
@@ -2798,8 +2802,6 @@
             } else {
                 mHandler.post(r);
             }
-            loadAndBindWidgetsAndShortcuts(mApp.getContext(), tryGetCallbacks(oldCallbacks),
-                    false /* refresh */);
         }
 
         private void loadAllApps() {
@@ -3342,6 +3344,7 @@
 
     public void loadAndBindWidgetsAndShortcuts(final Context context, final Callbacks callbacks,
             final boolean refresh) {
+
         runOnWorkerThread(new Runnable(){
             @Override
             public void run() {
@@ -3355,6 +3358,7 @@
                         }
                     }
                 });
+                mBgWidgetsModel = model;
                 // update the Widget entries inside DB on the worker thread.
                 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
                         model.getRawList());
@@ -3363,9 +3367,9 @@
     }
 
     /**
-     *  Returns a list of ResolveInfos/AppWidgetInfos.
+     * Returns a list of ResolveInfos/AppWidgetInfos.
      *
-     *  @see #loadAndBindWidgetsAndShortcuts
+     * @see #loadAndBindWidgetsAndShortcuts
      */
     private WidgetsModel createWidgetsModel(Context context, boolean refresh) {
         PackageManager packageManager = context.getPackageManager();