Fixing folder icon in landscape, issue 5135333

Change-Id: Idf46b12a947bdc0e56993b8827021855d5349bc1
diff --git a/res/values-land/dimens.xml b/res/values-land/dimens.xml
index 4f6bc41..55a25ec 100644
--- a/res/values-land/dimens.xml
+++ b/res/values-land/dimens.xml
@@ -61,4 +61,8 @@
     <dimen name="apps_customize_widget_cell_height_gap">5dp</dimen>
     <integer name="apps_customize_widget_cell_count_x">3</integer>
     <integer name="apps_customize_widget_cell_count_y">2</integer>
+
+<!-- Folders -->
+    <!-- The size of the image which sits behind the preview of the folder contents -->
+    <dimen name="folder_preview_size">58dp</dimen>
 </resources>
diff --git a/src/com/android/launcher2/FolderIcon.java b/src/com/android/launcher2/FolderIcon.java
index f1a1508..cde5d94 100644
--- a/src/com/android/launcher2/FolderIcon.java
+++ b/src/com/android/launcher2/FolderIcon.java
@@ -27,6 +27,7 @@
 import android.graphics.PorterDuff;
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
+import android.os.Parcelable;
 import android.util.AttributeSet;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -50,6 +51,7 @@
     private Launcher mLauncher;
     Folder mFolder;
     FolderInfo mInfo;
+    private static boolean sStaticValuesDirty = true;
 
     // The number of icons to display in the
     private static final int NUM_ITEMS_IN_PREVIEW = 3;
@@ -132,18 +134,26 @@
         folder.setFolderIcon(icon);
         folder.bind(folderInfo);
         icon.mFolder = folder;
-        icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
-
-        folderInfo.addListener(icon);
-
         Resources res = launcher.getResources();
-        if (sSharedFolderLeaveBehind == null) {
+
+        // We need to reload the static values when configuration changes in case they are
+        // different in another configuration
+        if (sStaticValuesDirty) {
             sSharedFolderLeaveBehind = res.getDrawable(R.drawable.portal_ring_rest);
         }
 
+        icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
+        folderInfo.addListener(icon);
+
         return icon;
     }
 
+    @Override
+    protected Parcelable onSaveInstanceState() {
+        sStaticValuesDirty = true;
+        return super.onSaveInstanceState();
+    }
+
     public static class FolderRingAnimator {
         public int mCellX;
         public int mCellY;
@@ -167,15 +177,14 @@
             mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
             mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
 
-            if (sPreviewSize < 0 || sPreviewPadding < 0) {
+            // We need to reload the static values when configuration changes in case they are
+            // different in another configuration
+            if (sStaticValuesDirty) {
                 sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size);
                 sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
-            }
-            if (sSharedOuterRingDrawable == null) {
                 sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
-            }
-            if (sSharedInnerRingDrawable == null) {
                 sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
+                sStaticValuesDirty = false;
             }
         }
 
@@ -554,6 +563,18 @@
         va.start();
     }
 
+    public void setTextVisible(boolean visible) {
+        if (visible) {
+            mFolderName.setVisibility(VISIBLE);
+        } else {
+            mFolderName.setVisibility(INVISIBLE);
+        }
+    }
+
+    public boolean getTextVisible() {
+        return mFolderName.getVisibility() == VISIBLE;
+    }
+
     public void onItemsChanged() {
         invalidate();
         requestLayout();
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index b6a1666..1c13f14 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -465,6 +465,11 @@
             layout = mLauncher.getHotseat().getLayout();
             child.setOnKeyListener(null);
 
+            // Hide folder title in the hotseat
+            if (child instanceof FolderIcon) {
+                ((FolderIcon) child).setTextVisible(false);
+            }
+
             if (screen < 0) {
                 screen = mLauncher.getHotseat().getOrderInHotseat(x, y);
             } else {
@@ -474,6 +479,11 @@
                 y = mLauncher.getHotseat().getCellYFromOrder(screen);
             }
         } else {
+            // Show folder title if not in the hotseat
+            if (child instanceof FolderIcon) {
+                ((FolderIcon) child).setTextVisible(true);
+            }
+
             layout = (CellLayout) getChildAt(screen);
             child.setOnKeyListener(new BubbleTextViewKeyEventListener());
         }
@@ -1900,6 +1910,8 @@
         final Rect clipRect = mTempRect;
         v.getDrawingRect(clipRect);
 
+        boolean textVisible = false;
+
         destCanvas.save();
         if (v instanceof TextView && pruneToDrawable) {
             Drawable d = ((TextView) v).getCompoundDrawables()[1];
@@ -1908,7 +1920,12 @@
             d.draw(destCanvas);
         } else {
             if (v instanceof FolderIcon) {
-                clipRect.bottom = getResources().getDimensionPixelSize(R.dimen.folder_preview_size);
+                // For FolderIcons the text can bleed into the icon area, and so we need to
+                // hide the text completely (which can't be achieved by clipping).
+                if (((FolderIcon) v).getTextVisible()) {
+                    ((FolderIcon) v).setTextVisible(false);
+                    textVisible = true;
+                }
             } else if (v instanceof BubbleTextView) {
                 final BubbleTextView tv = (BubbleTextView) v;
                 clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V +
@@ -1921,6 +1938,11 @@
             destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
             destCanvas.clipRect(clipRect, Op.REPLACE);
             v.draw(destCanvas);
+
+            // Restore text visibility of FolderIcon if necessary
+            if (textVisible) {
+                ((FolderIcon) v).setTextVisible(true);
+            }
         }
         destCanvas.restore();
     }