Fix crash when switching between Apps/Widgets tabs

Bug: 8138894
diff --git a/src/com/android/launcher2/AppsCustomizePagedView.java b/src/com/android/launcher2/AppsCustomizePagedView.java
index 121afb0..ed834b9 100644
--- a/src/com/android/launcher2/AppsCustomizePagedView.java
+++ b/src/com/android/launcher2/AppsCustomizePagedView.java
@@ -90,7 +90,7 @@
         if (generatedImages != null) {
             if (cancelled) {
                 for (int i = 0; i < generatedImages.size(); i++) {
-                    widgetPreviewLoader.releaseBitmap(items.get(i), generatedImages.get(i));
+                    widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
                 }
             }
             generatedImages.clear();
diff --git a/src/com/android/launcher2/AppsCustomizeTabHost.java b/src/com/android/launcher2/AppsCustomizeTabHost.java
index 5eb8483..27ceaba 100644
--- a/src/com/android/launcher2/AppsCustomizeTabHost.java
+++ b/src/com/android/launcher2/AppsCustomizeTabHost.java
@@ -264,15 +264,19 @@
                 // Animate the transition
                 ObjectAnimator outAnim = LauncherAnimUtils.ofFloat(mAnimationBuffer, "alpha", 0f);
                 outAnim.addListener(new AnimatorListenerAdapter() {
+                    private void clearAnimationBuffer() {
+                        mAnimationBuffer.setVisibility(View.GONE);
+                        PagedViewWidget.setRecyclePreviewsWhenDetachedFromWindow(false);
+                        mAnimationBuffer.removeAllViews();
+                        PagedViewWidget.setRecyclePreviewsWhenDetachedFromWindow(true);
+                    }
                     @Override
                     public void onAnimationEnd(Animator animation) {
-                        mAnimationBuffer.setVisibility(View.GONE);
-                        mAnimationBuffer.removeAllViews();
+                        clearAnimationBuffer();
                     }
                     @Override
                     public void onAnimationCancel(Animator animation) {
-                        mAnimationBuffer.setVisibility(View.GONE);
-                        mAnimationBuffer.removeAllViews();
+                        clearAnimationBuffer();
                     }
                 });
                 ObjectAnimator inAnim = LauncherAnimUtils.ofFloat(mAppsCustomizePane, "alpha", 1f);
diff --git a/src/com/android/launcher2/PagedViewWidget.java b/src/com/android/launcher2/PagedViewWidget.java
index aece398..bb5827a 100644
--- a/src/com/android/launcher2/PagedViewWidget.java
+++ b/src/com/android/launcher2/PagedViewWidget.java
@@ -38,6 +38,7 @@
     static final String TAG = "PagedViewWidgetLayout";
 
     private static boolean sDeletePreviewsWhenDetachedFromWindow = true;
+    private static boolean sRecyclePreviewsWhenDetachedFromWindow = true;
 
     private String mDimensionsFormatString;
     CheckForShortPress mPendingCheckForShortPress = null;
@@ -82,6 +83,10 @@
         sDeletePreviewsWhenDetachedFromWindow = value;
     }
 
+    public static void setRecyclePreviewsWhenDetachedFromWindow(boolean value) {
+        sRecyclePreviewsWhenDetachedFromWindow = value;
+    }
+
     @Override
     protected void onDetachedFromWindow() {
         super.onDetachedFromWindow();
@@ -90,8 +95,9 @@
             final ImageView image = (ImageView) findViewById(R.id.widget_preview);
             if (image != null) {
                 FastBitmapDrawable preview = (FastBitmapDrawable) image.getDrawable();
-                if (mInfo != null && preview != null && preview.getBitmap() != null) {
-                    mWidgetPreviewLoader.releaseBitmap(mInfo, preview.getBitmap());
+                if (sRecyclePreviewsWhenDetachedFromWindow &&
+                        mInfo != null && preview != null && preview.getBitmap() != null) {
+                    mWidgetPreviewLoader.recycleBitmap(mInfo, preview.getBitmap());
                 }
                 image.setImageDrawable(null);
             }
diff --git a/src/com/android/launcher2/WidgetPreviewLoader.java b/src/com/android/launcher2/WidgetPreviewLoader.java
index a9bc098..617879b 100644
--- a/src/com/android/launcher2/WidgetPreviewLoader.java
+++ b/src/com/android/launcher2/WidgetPreviewLoader.java
@@ -234,15 +234,14 @@
         }
     }
 
-    public void releaseBitmap(Object o, Bitmap bitmapToFree) {
-        // enable this code when doDecode doesn't force Bitmaps to become immutable
+    public void recycleBitmap(Object o, Bitmap bitmapToRecycle) {
         String name = getObjectName(o);
         synchronized(mLoadedPreviews) {
             synchronized(mUnusedBitmaps) {
                 Bitmap b = mLoadedPreviews.get(name).get();
-                if (b == bitmapToFree) {
+                if (b == bitmapToRecycle) {
                     mLoadedPreviews.remove(name);
-                    if (bitmapToFree.isMutable()) {
+                    if (bitmapToRecycle.isMutable()) {
                         mUnusedBitmaps.add(new SoftReference<Bitmap>(b));
                     }
                 } else {