Refactoring and fixing bugs in Customize tray.

Change-Id: Ia37d3033d64036fd09f5f33443eb9f575e311c7c
diff --git a/src/com/android/launcher2/CustomizePagedView.java b/src/com/android/launcher2/CustomizePagedView.java
index 2b54e3f..e80eda3 100644
--- a/src/com/android/launcher2/CustomizePagedView.java
+++ b/src/com/android/launcher2/CustomizePagedView.java
@@ -40,6 +40,7 @@
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Rect;
+import android.graphics.RectF;
 import android.graphics.Bitmap.Config;
 import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
@@ -90,6 +91,10 @@
     // The mapping between the pages and the widgets that will be laid out on them
     private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
 
+    // This is used if we want to set a min width on pages so that things inside them left align to
+    // a fixed size
+    private int mMinPageWidth;
+
     // The max dimensions for the ImageView we use for displaying a widget
     private int mMaxWidgetWidth;
 
@@ -165,6 +170,7 @@
         final Resources r = context.getResources();
         setDragSlopeThreshold(
                 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
+        mMinPageWidth = r.getDimensionPixelSize(R.dimen.customization_drawer_content_min_width);
 
         setVisibility(View.GONE);
         setSoundEffectsEnabled(false);
@@ -544,12 +550,12 @@
         }
     }
 
-    private Bitmap drawableToBitmap(Drawable d) {
+    private Bitmap drawableToBitmap(Drawable d, float scaleX, float scaleY) {
         final Rect bounds = d.getBounds();
         final int w = bounds.width();
         final int h = bounds.height();
         Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
-        renderDrawableToBitmap(d, b, 0, 0, w, h);
+        renderDrawableToBitmap(d, b, 0, 0, w, h, scaleX, scaleY);
         return b;
     }
 
@@ -575,7 +581,13 @@
                 // Get the widget preview as the drag representation
                 final LinearLayout l = (LinearLayout) v;
                 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
-                mDragBitmap = drawableToBitmap(i.getDrawable());
+
+                // Calculate how much to scale the drag preview
+                RectF tmpScaleRect = new RectF(0,0,1,1);
+                i.getImageMatrix().mapRect(tmpScaleRect);
+
+                mDragBitmap = drawableToBitmap(i.getDrawable(), tmpScaleRect.right,
+                        tmpScaleRect.bottom);
                 i.getLocationOnScreen(mDragViewOrigin);
                 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
 
@@ -596,7 +608,7 @@
                 // get icon (top compound drawable, index is 1)
                 final TextView tv = (TextView) v;
                 final Drawable icon = tv.getCompoundDrawables()[1];
-                mDragBitmap = drawableToBitmap(icon);
+                mDragBitmap = drawableToBitmap(icon, 1.0f, 1.0f);
 
                 Object dragInfo = v.getTag();
                 if (mCustomizationType == CustomizationType.ApplicationCustomization) {
@@ -679,9 +691,11 @@
     /**
      * Helper function to draw a drawable to the specified canvas with the specified bounds.
      */
-    private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
+    private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
+            float scaleX, float scaleY) {
         if (bitmap != null) mCanvas.setBitmap(bitmap);
         mCanvas.save();
+        mCanvas.scale(scaleX, scaleY);
         final Rect oldBounds = d.copyBounds();
         d.setBounds(x, y, x + w, y + h);
         d.draw(mCanvas);
@@ -768,7 +782,7 @@
             background = resources.getDrawable(R.drawable.default_widget_preview);
         }
 
-        renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
+        renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
 
         // If we don't have a custom icon, we use the app icon on the default background
         if (!foundCustomDrawable) {
@@ -780,7 +794,7 @@
 
                 final int iconSize = minDim / 2;
                 final int offset = iconSize / 4;
-                renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
+                renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
             } catch (Resources.NotFoundException e) {
                 // if we can't find the icon, then just don't draw it
             }
@@ -820,7 +834,7 @@
             int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
             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);
+            renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
 
             // Draw the icon flush left
             try {
@@ -834,7 +848,7 @@
 
                 final int iconSize = minDim / 2;
                 final int offset = iconSize / 4;
-                renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
+                renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
             } catch (Resources.NotFoundException e) {
                 // if we can't find the icon, then just don't draw it
             }
@@ -860,7 +874,7 @@
             }
 
             final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
-            renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
+            renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height, 1.0f, 1.0f);
 
             newDrawable = new FastBitmapDrawable(bitmap);
         }
@@ -917,6 +931,7 @@
 
             PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
                     R.layout.customize_paged_view_widget, layout, false);
+
             l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans,
                     mPageViewIconCache, (numPages > 1));
             l.setTag(createItemInfo);
@@ -1072,10 +1087,12 @@
 
     @Override
     public void syncPages() {
+        boolean enforceMinimumPagedWidths = false;
         boolean centerPagedViewCellLayouts = false;
         switch (mCustomizationType) {
         case WidgetCustomization:
             syncWidgetPages();
+            enforceMinimumPagedWidths = true;
             break;
         case ShortcutCustomization:
             syncListPages(mShortcutList);
@@ -1083,6 +1100,7 @@
             break;
         case WallpaperCustomization:
             syncWallpaperPages();
+            enforceMinimumPagedWidths = true;
             break;
         case ApplicationCustomization:
             syncAppPages();
@@ -1108,7 +1126,12 @@
             }
         }
 
-        // bound the current page
+        // Set a min page width for PagedView layout if we have more than a single page
+        if (enforceMinimumPagedWidths) {
+            setMinimumWidthOverride((childCount > 1) ? mMinPageWidth : 0);
+        }
+
+        // Bound the current page index
         requestLayout();
         post(new Runnable() {
             @Override
diff --git a/src/com/android/launcher2/HolographicOutlineHelper.java b/src/com/android/launcher2/HolographicOutlineHelper.java
index 1efc123..80548fb 100644
--- a/src/com/android/launcher2/HolographicOutlineHelper.java
+++ b/src/com/android/launcher2/HolographicOutlineHelper.java
@@ -32,6 +32,7 @@
     private final Paint mAlphaClipPaint = new Paint();
 
     public static final int MAX_OUTER_BLUR_RADIUS;
+    public static final int MIN_OUTER_BLUR_RADIUS;
 
     private static final BlurMaskFilter sExtraThickOuterBlurMaskFilter;
     private static final BlurMaskFilter sThickOuterBlurMaskFilter;
@@ -48,6 +49,7 @@
     static {
         final float scale = LauncherApplication.getScreenDensity();
 
+        MIN_OUTER_BLUR_RADIUS = (int) (scale * 1.0f);
         MAX_OUTER_BLUR_RADIUS = (int) (scale * 12.0f);
 
         sExtraThickOuterBlurMaskFilter = new BlurMaskFilter(scale * 12.0f, BlurMaskFilter.Blur.OUTER);
diff --git a/src/com/android/launcher2/PagedView.java b/src/com/android/launcher2/PagedView.java
index 66383cb..c2fcd9f 100644
--- a/src/com/android/launcher2/PagedView.java
+++ b/src/com/android/launcher2/PagedView.java
@@ -100,6 +100,7 @@
     protected int mTouchSlop;
     private int mPagingTouchSlop;
     private int mMaximumVelocity;
+    private int mMinimumWidth;
     protected int mPageSpacing;
     protected int mPageLayoutPaddingTop;
     protected int mPageLayoutPaddingBottom;
@@ -175,13 +176,13 @@
                 R.styleable.PagedView, defStyle, 0);
         mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0);
         mPageLayoutPaddingTop = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingTop, 10);
+                R.styleable.PagedView_pageLayoutPaddingTop, 0);
         mPageLayoutPaddingBottom = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingBottom, 10);
+                R.styleable.PagedView_pageLayoutPaddingBottom, 0);
         mPageLayoutPaddingLeft = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingLeft, 10);
+                R.styleable.PagedView_pageLayoutPaddingLeft, 0);
         mPageLayoutPaddingRight = a.getDimensionPixelSize(
-                R.styleable.PagedView_pageLayoutPaddingRight, 10);
+                R.styleable.PagedView_pageLayoutPaddingRight, 0);
         mPageLayoutWidthGap = a.getDimensionPixelSize(
                 R.styleable.PagedView_pageLayoutWidthGap, -1);
         mPageLayoutHeightGap = a.getDimensionPixelSize(
@@ -1138,8 +1139,16 @@
         return -1;
     }
 
+    protected void setMinimumWidthOverride(int minimumWidth) {
+        mMinimumWidth = minimumWidth;
+    }
+
+    protected int getChildWidth(int index) {
+        return Math.max(mMinimumWidth, getChildAt(index).getMeasuredWidth());
+    }
+
     protected int getRelativeChildOffset(int index) {
-        return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2;
+        return (getMeasuredWidth() - getChildWidth(index)) / 2;
     }
 
     protected int getChildOffset(int index) {
@@ -1154,7 +1163,7 @@
     }
 
     protected int getScaledMeasuredWidth(View child) {
-        return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f);
+        return (int) (Math.max(mMinimumWidth, child.getMeasuredWidth()) * mLayoutScale + 0.5f);
     }
 
     int getPageNearestToCenterOfScreen() {
diff --git a/src/com/android/launcher2/PagedViewWidget.java b/src/com/android/launcher2/PagedViewWidget.java
index 07b215d..72f928b 100644
--- a/src/com/android/launcher2/PagedViewWidget.java
+++ b/src/com/android/launcher2/PagedViewWidget.java
@@ -27,7 +27,11 @@
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
+import android.graphics.PorterDuff;
 import android.graphics.PorterDuff.Mode;
+import android.graphics.PorterDuffXfermode;
+import android.graphics.Rect;
+import android.graphics.RectF;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.Message;
@@ -52,6 +56,10 @@
     private Bitmap mHolographicOutline;
     private final Canvas mHolographicOutlineCanvas = new Canvas();
     private FastBitmapDrawable mPreview;
+    private ImageView mPreviewImageView;
+    private final RectF mTmpScaleRect = new RectF();
+    private final Rect mEraseStrokeRect = new Rect();
+    private final Paint mEraseStrokeRectPaint = new Paint();
 
     private PagedViewIconCache.Key mIconCacheKey;
     private PagedViewIconCache mIconCache;
@@ -81,8 +89,11 @@
         public void handleMessage(Message msg) {
             final PagedViewWidget widget = (PagedViewWidget) msg.obj;
             final int prevAlpha = widget.mPreview.getAlpha();
-            final Bitmap outline = Bitmap.createBitmap(widget.getWidth(), widget.getHeight(),
-                    Bitmap.Config.ARGB_8888);
+            final int width = Math.max(widget.mPreview.getIntrinsicWidth(),
+                    widget.getMeasuredWidth());
+            final int height = Math.max(widget.mPreview.getIntrinsicHeight(),
+                    widget.getMeasuredHeight());
+            final Bitmap outline = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
 
             widget.mHolographicOutlineCanvas.setBitmap(outline);
             widget.mHolographicOutlineCanvas.save();
@@ -94,6 +105,12 @@
             widget.mHolographicOutlineCanvas.drawColor(Color.argb(156, 0, 0, 0), Mode.SRC_OVER);
             widget.mHolographicOutlineCanvas.restore();
 
+            // To account for the fact that some previews run up straight to the edge (we subtract
+            // the edge from the holographic preview (before we apply the holograph)
+            widget.mEraseStrokeRect.set(0, 0, width, height);
+            widget.mHolographicOutlineCanvas.drawRect(widget.mEraseStrokeRect,
+                    widget.mEraseStrokeRectPaint);
+
             sHolographicOutlineHelper.applyThickExpensiveOutlineWithBlur(outline,
                     widget.mHolographicOutlineCanvas, widget.mHoloBlurColor,
                     widget.mHoloOutlineColor);
@@ -123,6 +140,11 @@
                 defStyle, 0);
         mHoloBlurColor = a.getColor(R.styleable.PagedViewWidget_blurColor, 0);
         mHoloOutlineColor = a.getColor(R.styleable.PagedViewWidget_outlineColor, 0);
+        mEraseStrokeRectPaint.setStyle(Paint.Style.STROKE);
+        mEraseStrokeRectPaint.setStrokeWidth(HolographicOutlineHelper.MIN_OUTER_BLUR_RADIUS);
+        mEraseStrokeRectPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
+        mEraseStrokeRectPaint.setFilterBitmap(true);
+        mEraseStrokeRectPaint.setAntiAlias(true);
         a.recycle();
 
         if (sHolographicOutlineHelper == null) {
@@ -158,6 +180,7 @@
         final ImageView image = (ImageView) findViewById(R.id.widget_preview);
         image.setMaxWidth(maxWidth);
         image.setImageDrawable(preview);
+        mPreviewImageView = image;
         final TextView name = (TextView) findViewById(R.id.widget_name);
         name.setText(info.label);
         name.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
@@ -179,6 +202,7 @@
         ImageView image = (ImageView) findViewById(R.id.wallpaper_preview);
         image.setMaxWidth(maxWidth);
         image.setImageDrawable(preview);
+        mPreviewImageView = image;
         TextView name = (TextView) findViewById(R.id.wallpaper_name);
         name.setText(info.loadLabel(packageManager));
         name.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
@@ -211,8 +235,15 @@
 
         // draw any blended overlays
         if (mHolographicOutline != null && mHolographicAlpha > 0) {
+            // Calculate how much to scale the holographic preview
+            mTmpScaleRect.set(0,0,1,1);
+            mPreviewImageView.getImageMatrix().mapRect(mTmpScaleRect);
+
             mPaint.setAlpha(mHolographicAlpha);
+            canvas.save();
+            canvas.scale(mTmpScaleRect.right, mTmpScaleRect.bottom);
             canvas.drawBitmap(mHolographicOutline, 0, 0, mPaint);
+            canvas.restore();
         }
     }