Merge "Update PreviewLayoutRule API to prepare for new folder animation." into ub-launcher3-master
diff --git a/src/com/android/launcher3/folder/ClippedFolderIconLayoutRule.java b/src/com/android/launcher3/folder/ClippedFolderIconLayoutRule.java
index 6ee02f9..840fcf5 100644
--- a/src/com/android/launcher3/folder/ClippedFolderIconLayoutRule.java
+++ b/src/com/android/launcher3/folder/ClippedFolderIconLayoutRule.java
@@ -1,16 +1,17 @@
 package com.android.launcher3.folder;
 
-import android.graphics.Path;
-import android.graphics.Point;
+import android.view.View;
 
-import com.android.launcher3.DeviceProfile;
-import com.android.launcher3.LauncherAppState;
-import com.android.launcher3.Utilities;
+import com.android.launcher3.config.FeatureFlags;
+
+import java.util.ArrayList;
+import java.util.List;
 
 public class ClippedFolderIconLayoutRule implements FolderIcon.PreviewLayoutRule {
 
     static final int MAX_NUM_ITEMS_IN_PREVIEW = 4;
     private static final int MIN_NUM_ITEMS_IN_PREVIEW = 2;
+    private static final int MAX_NUM_ITEMS_PER_ROW = 2;
 
     final float MIN_SCALE = 0.48f;
     final float MAX_SCALE = 0.58f;
@@ -38,7 +39,7 @@
     public FolderIcon.PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
             int curNumItems, FolderIcon.PreviewItemDrawingParams params) {
 
-        float totalScale = scaleForNumItems(curNumItems);
+        float totalScale = scaleForItem(index, curNumItems);
         float transX;
         float transY;
         float overlayAlpha = 0;
@@ -94,7 +95,7 @@
                 MIN_NUM_ITEMS_IN_PREVIEW) / (MAX_NUM_ITEMS_IN_PREVIEW - MIN_NUM_ITEMS_IN_PREVIEW));
         double theta = theta0 + index * (2 * Math.PI / curNumItems) * direction;
 
-        float halfIconSize = (mIconSize * scaleForNumItems(curNumItems)) / 2;
+        float halfIconSize = (mIconSize * scaleForItem(index, curNumItems)) / 2;
 
         // Map the location along the circle, and offset the coordinates to represent the center
         // of the icon, and to be based from the top / left of the preview area. The y component
@@ -104,7 +105,9 @@
 
     }
 
-    private float scaleForNumItems(int numItems) {
+    @Override
+    public float scaleForItem(int index, int numItems) {
+        // Scale is determined by the number of items in the preview.
         float scale = 1f;
         if (numItems <= 2) {
             scale = MAX_SCALE;
@@ -118,7 +121,7 @@
     }
 
     @Override
-    public int numItems() {
+    public int maxNumItems() {
         return MAX_NUM_ITEMS_IN_PREVIEW;
     }
 
@@ -127,4 +130,23 @@
         return true;
     }
 
+    @Override
+    public List<View> getItemsToDisplay(Folder folder) {
+        List<View> items = new ArrayList<>(folder.getItemsInReadingOrder());
+        int numItems = items.size();
+        if (FeatureFlags.LAUNCHER3_NEW_FOLDER_ANIMATION && numItems > MAX_NUM_ITEMS_IN_PREVIEW) {
+            // We match the icons in the preview with the layout of the opened folder (b/27944225),
+            // but we still need to figure out how we want to handle updating the preview when the
+            // upper left quadrant changes.
+            int appsPerRow = folder.mContent.getPageAt(0).getCountX();
+            int appsToDelete = appsPerRow - MAX_NUM_ITEMS_PER_ROW;
+
+            // We only display the upper left quadrant.
+            while (appsToDelete > 0) {
+                items.remove(MAX_NUM_ITEMS_PER_ROW);
+                appsToDelete--;
+            }
+        }
+        return items.subList(0, Math.min(numItems, MAX_NUM_ITEMS_IN_PREVIEW));
+    }
 }
diff --git a/src/com/android/launcher3/folder/FolderIcon.java b/src/com/android/launcher3/folder/FolderIcon.java
index 74dc48c..171d40d 100644
--- a/src/com/android/launcher3/folder/FolderIcon.java
+++ b/src/com/android/launcher3/folder/FolderIcon.java
@@ -73,6 +73,7 @@
 import com.android.launcher3.util.Thunk;
 
 import java.util.ArrayList;
+import java.util.List;
 
 /**
  * An icon that can appear on in the workspace representing an {@link Folder}.
@@ -327,7 +328,7 @@
             to.offset(center[0] - animateView.getMeasuredWidth() / 2,
                       center[1] - animateView.getMeasuredHeight() / 2);
 
-            float finalAlpha = index < mPreviewLayoutRule.numItems() ? 0.5f : 0f;
+            float finalAlpha = index < mPreviewLayoutRule.maxNumItems() ? 0.5f : 0f;
 
             float finalScale = scale * scaleRelativeToDragLayer;
             dragLayer.animateView(animateView, from, to, finalAlpha,
@@ -425,8 +426,8 @@
     }
 
     private float getLocalCenterForIndex(int index, int curNumItems, int[] center) {
-        mTmpParams = computePreviewItemDrawingParams(Math.min(mPreviewLayoutRule.numItems(), index),
-                curNumItems, mTmpParams);
+        mTmpParams = computePreviewItemDrawingParams(
+                Math.min(mPreviewLayoutRule.maxNumItems(), index), curNumItems, mTmpParams);
 
         mTmpParams.transX += mBackground.basePreviewOffsetX;
         mTmpParams.transY += mBackground.basePreviewOffsetY;
@@ -890,8 +891,8 @@
     }
 
     private void updateItemDrawingParams(boolean animate) {
-        ArrayList<View> items = mFolder.getItemsInReadingOrder();
-        int nItemsInPreview = Math.min(items.size(), mPreviewLayoutRule.numItems());
+        List<View> items = mPreviewLayoutRule.getItemsToDisplay(mFolder);
+        int nItemsInPreview = items.size();
 
         int prevNumItems = mDrawingParams.size();
 
@@ -1062,10 +1063,10 @@
     public interface PreviewLayoutRule {
         PreviewItemDrawingParams computePreviewItemDrawingParams(int index, int curNumItems,
             PreviewItemDrawingParams params);
-
         void init(int availableSpace, int intrinsicIconSize, boolean rtl);
-
-        int numItems();
+        float scaleForItem(int index, int totalNumItems);
+        int maxNumItems();
         boolean clipToBackground();
+        List<View> getItemsToDisplay(Folder folder);
     }
 }
diff --git a/src/com/android/launcher3/folder/StackFolderIconLayoutRule.java b/src/com/android/launcher3/folder/StackFolderIconLayoutRule.java
index 7fb02e3..297203a 100644
--- a/src/com/android/launcher3/folder/StackFolderIconLayoutRule.java
+++ b/src/com/android/launcher3/folder/StackFolderIconLayoutRule.java
@@ -16,10 +16,12 @@
 
 package com.android.launcher3.folder;
 
-import android.graphics.Path;
+import android.view.View;
 
 import com.android.launcher3.folder.FolderIcon.PreviewItemDrawingParams;
 
+import java.util.List;
+
 public class StackFolderIconLayoutRule implements FolderIcon.PreviewLayoutRule {
 
     static final int MAX_NUM_ITEMS_IN_PREVIEW = 3;
@@ -54,10 +56,10 @@
     @Override
     public PreviewItemDrawingParams computePreviewItemDrawingParams(int index, int curNumItems,
             PreviewItemDrawingParams params) {
+        float scale = scaleForItem(index, curNumItems);
 
         index = MAX_NUM_ITEMS_IN_PREVIEW - index - 1;
         float r = (index * 1.0f) / (MAX_NUM_ITEMS_IN_PREVIEW - 1);
-        float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
 
         float offset = (1 - r) * mMaxPerspectiveShift;
         float scaledSize = scale * mBaselineIconSize;
@@ -80,12 +82,26 @@
     }
 
     @Override
-    public int numItems() {
+    public int maxNumItems() {
         return MAX_NUM_ITEMS_IN_PREVIEW;
     }
 
     @Override
+    public float scaleForItem(int index, int numItems) {
+        // Scale is determined by the position of the icon in the preview.
+        index = MAX_NUM_ITEMS_IN_PREVIEW - index - 1;
+        float r = (index * 1.0f) / (MAX_NUM_ITEMS_IN_PREVIEW - 1);
+        return (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
+    }
+
+    @Override
     public boolean clipToBackground() {
         return false;
     }
+
+    @Override
+    public List<View> getItemsToDisplay(Folder folder) {
+        List<View> items = folder.getItemsInReadingOrder();
+        return items.subList(0, Math.min(items.size(), MAX_NUM_ITEMS_IN_PREVIEW));
+    }
 }
diff --git a/src_config/com/android/launcher3/config/FeatureFlags.java b/src_config/com/android/launcher3/config/FeatureFlags.java
index ffb86e4..5c29366 100644
--- a/src_config/com/android/launcher3/config/FeatureFlags.java
+++ b/src_config/com/android/launcher3/config/FeatureFlags.java
@@ -28,6 +28,7 @@
     public static boolean LAUNCHER3_USE_SYSTEM_DRAG_DRIVER = true;
     public static boolean LAUNCHER3_DISABLE_PINCH_TO_OVERVIEW = false;
     public static boolean LAUNCHER3_ALL_APPS_PULL_UP = true;
+    public static boolean LAUNCHER3_NEW_FOLDER_ANIMATION = false;
 
     // Feature flag to enable moving the QSB on the 0th screen of the workspace.
     public static final boolean QSB_ON_FIRST_SCREEN = true;