Layout and spacing changes for AllApps/Customization drawer

- Also fixing issue where preview images in the customization drawer were not being scaled down

Change-Id: I83d1abf019d591954204b87a437eac2ff916dd54
diff --git a/src/com/android/launcher2/AllAppsPagedView.java b/src/com/android/launcher2/AllAppsPagedView.java
index 149f9fb..351384f 100644
--- a/src/com/android/launcher2/AllAppsPagedView.java
+++ b/src/com/android/launcher2/AllAppsPagedView.java
@@ -94,6 +94,12 @@
     }
 
     @Override
+    protected void init() {
+        super.init();
+        mCenterPagesVertically = false;
+    }
+
+    @Override
     public void setLauncher(Launcher launcher) {
         mLauncher = launcher;
         mLauncher.setAllAppsPagedView(this);
diff --git a/src/com/android/launcher2/CustomizePagedView.java b/src/com/android/launcher2/CustomizePagedView.java
index a475a50..156bc20 100644
--- a/src/com/android/launcher2/CustomizePagedView.java
+++ b/src/com/android/launcher2/CustomizePagedView.java
@@ -33,6 +33,7 @@
 import android.graphics.Bitmap;
 import android.graphics.Bitmap.Config;
 import android.graphics.Canvas;
+import android.graphics.Color;
 import android.graphics.Rect;
 import android.graphics.Region.Op;
 import android.graphics.drawable.Drawable;
@@ -130,6 +131,12 @@
         setupWorkspaceLayout();
     }
 
+    @Override
+    protected void init() {
+        super.init();
+        mCenterPagesVertically = false;
+    }
+
     public void setLauncher(Launcher launcher) {
         Context context = getContext();
         mLauncher = launcher;
@@ -440,6 +447,17 @@
     }
 
     /**
+     * Helper function to draw a drawable to the specified canvas with the specified bounds.
+     */
+    private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int l, int t, int r, int b) {
+        if (bitmap != null) mCanvas.setBitmap(bitmap);
+        mCanvas.save();
+        d.setBounds(l, t, r, b);
+        d.draw(mCanvas);
+        mCanvas.restore();
+    }
+
+    /**
      * This method will extract the preview image specified by the widget developer (if it exists),
      * otherwise, it will try to generate a default image preview with the widget's package icon.
      * @return the drawable will be used and sized in the ImageView to represent the widget
@@ -453,28 +471,21 @@
             if (drawable == null) {
                 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
                         + " for provider: " + info.provider);
-            } else {
-                return drawable;
             }
         }
 
         // If we don't have a preview image, create a default one
+        final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
+        final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
         if (drawable == null) {
             Resources resources = mLauncher.getResources();
 
             // Create a new bitmap to hold the widget preview
-            final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
-            final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
             int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
             int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
-            Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
-            mCanvas.setBitmap(bitmap);
-            // For some reason, we must re-set the clip rect here, otherwise it will be wrong
-            mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
-
-            Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
-            background.setBounds(0, 0, width, height);
-            background.draw(mCanvas);
+            final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
+            final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
+            renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
 
             // Draw the icon vertically centered, flush left
             try {
@@ -490,13 +501,34 @@
 
                 final int iconSize = minDim / 2;
                 final int offset = iconSize / 4;
-                icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
-                icon.draw(mCanvas);
+                final int offsetIconSize = offset + iconSize;
+                renderDrawableToBitmap(icon, null, offset, offset, offsetIconSize, offsetIconSize);
             } catch (Resources.NotFoundException e) {
                 // if we can't find the icon, then just don't draw it
             }
 
             drawable = new FastBitmapDrawable(bitmap);
+        } else {
+            // Scale down the preview if necessary
+            final float aspect = (float) info.minWidth / info.minHeight;
+            final int boundedWidth = (int) Math.max(minDim, Math.min(maxDim, info.minWidth));
+            final int boundedHeight = (int) Math.max(minDim, Math.min(maxDim, info.minHeight));
+            final int scaledWidth = (int) (boundedWidth * sScaleFactor);
+            final int scaledHeight = (int) (boundedHeight * sScaleFactor);
+            int width;
+            int height;
+            if (scaledWidth > scaledHeight) {
+                width = scaledWidth;
+                height = (int) (width / aspect);
+            } else {
+                height = scaledHeight;
+                width = (int) (height * aspect);
+            }
+
+            final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
+            renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
+
+            drawable = new FastBitmapDrawable(bitmap);
         }
         drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
         return drawable;
@@ -607,7 +639,7 @@
 
             PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
                     R.layout.customize_paged_view_item, layout, false);
-            icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
+            icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache, true);
             switch (mCustomizationType) {
             case WallpaperCustomization:
                 icon.setOnClickListener(this);
@@ -661,7 +693,7 @@
             final ApplicationInfo info = mApps.get(i);
             PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
                     R.layout.all_apps_paged_view_application, layout, false);
-            icon.applyFromApplicationInfo(info, mPageViewIconCache, false);
+            icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
             icon.setOnClickListener(this);
             icon.setOnLongClickListener(this);
 
@@ -763,5 +795,4 @@
     public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
         return false;
     }
-
 }
diff --git a/src/com/android/launcher2/HolographicOutlineHelper.java b/src/com/android/launcher2/HolographicOutlineHelper.java
index bca3f71..658490a 100644
--- a/src/com/android/launcher2/HolographicOutlineHelper.java
+++ b/src/com/android/launcher2/HolographicOutlineHelper.java
@@ -183,4 +183,4 @@
         thickOuterBlur.recycle();
         thickInnerBlur.recycle();
     }
-}
\ No newline at end of file
+}
diff --git a/src/com/android/launcher2/PagedView.java b/src/com/android/launcher2/PagedView.java
index c125696..e9829fb 100644
--- a/src/com/android/launcher2/PagedView.java
+++ b/src/com/android/launcher2/PagedView.java
@@ -102,6 +102,7 @@
     protected int mPageLayoutHeightGap;
     protected int mCellCountX;
     protected int mCellCountY;
+    protected boolean mCenterPagesVertically;
 
     protected static final int INVALID_POINTER = -1;
 
@@ -207,6 +208,7 @@
         mPageViewIconCache = new PagedViewIconCache();
         mScroller = new Scroller(getContext());
         mCurrentPage = 0;
+        mCenterPagesVertically = true;
 
         final ViewConfiguration configuration = ViewConfiguration.get(getContext());
         mTouchSlop = configuration.getScaledTouchSlop();
@@ -394,7 +396,8 @@
             final View child = getChildAt(i);
             if (child.getVisibility() != View.GONE) {
                 final int childWidth = child.getMeasuredWidth();
-                final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2;
+                final int childHeight = (mCenterPagesVertically ?
+                        (getMeasuredHeight() - child.getMeasuredHeight()) / 2 : 0);
                 child.layout(childLeft, childHeight,
                         childLeft + childWidth, childHeight + child.getMeasuredHeight());
                 childLeft += childWidth + mPageSpacing;
diff --git a/src/com/android/launcher2/PagedViewIcon.java b/src/com/android/launcher2/PagedViewIcon.java
index c59ef80..6c6c4dc 100644
--- a/src/com/android/launcher2/PagedViewIcon.java
+++ b/src/com/android/launcher2/PagedViewIcon.java
@@ -156,12 +156,16 @@
     }
 
     public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
-            PagedViewIconCache cache) {
+            PagedViewIconCache cache, boolean scaleUp) {
         mIconCache = cache;
         mIconCacheKey = info;
         mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
 
         mIcon = Utilities.createIconBitmap(info.loadIcon(packageManager), mContext);
+        if (scaleUp) {
+            mIcon = Bitmap.createScaledBitmap(mIcon, mScaledIconSize,
+                    mScaledIconSize, true);
+        }
         setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
         setText(info.loadLabel(packageManager));
         setTag(info);
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index c4ca9e1..263c3a6 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -778,8 +778,7 @@
             // We shrink and disappear to nothing in the case of all apps
             // (which is when we shrink to the bottom)
             newY = screenHeight - newY - scaledPageHeight;
-            finalAlpha = 0.0f;
-            extraShrinkFactor = 0.1f;
+            finalAlpha = 0.25f;
         } else if (shrinkPosition == ShrinkPosition.SHRINK_TO_MIDDLE) {
             newY = screenHeight / 2 - scaledPageHeight / 2;
             finalAlpha = 1.0f;
@@ -910,6 +909,9 @@
     // never dragged over
     public void onDragStopped() {
         updateWhichPagesAcceptDrops(mShrunkenState);
+        if (mShrunkenState == ShrinkPosition.SHRINK_TO_BOTTOM_VISIBLE) {
+            shrink(ShrinkPosition.SHRINK_TO_BOTTOM_HIDDEN, true);
+        }
     }
 
     // We call this when we trigger an unshrink by clicking on the CellLayout cl