Fixing some issues with AppsCustomize

- Tweaking the widget page to make it a little tighter
- Altering the way we generate widget previews to make them more consistent
- Making the Apps page two line titles

Change-Id: I7bce420466c1d41fb87b423f259472482803a406
diff --git a/res/layout/apps_customize_widget.xml b/res/layout/apps_customize_widget.xml
index 57a9962..7cc4177 100644
--- a/res/layout/apps_customize_widget.xml
+++ b/res/layout/apps_customize_widget.xml
@@ -45,12 +45,8 @@
             android:ellipsize="marquee"
             android:fadingEdge="horizontal"
 
-            android:textColor="#FFBBBBBB"
-            android:textSize="14sp"
-            android:shadowColor="#FF000000"
-            android:shadowDx="0.0"
-            android:shadowDy="1.0"
-            android:shadowRadius="1.0" />
+            android:textColor="#FFFFFFFF"
+            android:textSize="13sp" />
 
         <!-- The original dimensions of the widget (can't be the same text as above due to different
              style. -->
@@ -58,17 +54,13 @@
             android:id="@+id/widget_dims"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_gravity="bottom"
+            android:layout_gravity="center"
             android:layout_marginLeft="5dp"
             android:layout_weight="0"
             android:gravity="left"
 
-            android:textColor="#FF666666"
-            android:textSize="12sp"
-            android:shadowColor="#99000000"
-            android:shadowDx="0.0"
-            android:shadowDy="1.0"
-            android:shadowRadius="1.0" />
+            android:textColor="#FF555555"
+            android:textSize="12sp" />
     </LinearLayout>
 
     <!-- The icon of the widget. -->
@@ -77,6 +69,6 @@
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"
-        android:adjustViewBounds="true"
-        android:scaleType="fitStart" />
+        android:padding="8dp"
+        android:scaleType="matrix" />
 </com.android.launcher2.PagedViewWidget>
diff --git a/res/values-port/dimens.xml b/res/values-port/dimens.xml
index 5aff6d9..83f2d60 100644
--- a/res/values-port/dimens.xml
+++ b/res/values-port/dimens.xml
@@ -24,6 +24,7 @@
     <dimen name="workspace_bottom_padding">0dp</dimen>
     <dimen name="workspace_page_spacing">-1dp</dimen>
 
+<!-- AppsCustomize -->
     <integer name="apps_customize_cellCountX">4</integer>
     <integer name="apps_customize_cellCountY">5</integer>
     <dimen name="apps_customize_pageLayoutWidthGap">-1dp</dimen>
diff --git a/res/values-sw340dp-port/dimens.xml b/res/values-sw340dp-port/dimens.xml
index 690facc..dd485e3 100644
--- a/res/values-sw340dp-port/dimens.xml
+++ b/res/values-sw340dp-port/dimens.xml
@@ -49,4 +49,12 @@
     <dimen name="button_bar_width_left_padding">8dp</dimen>
     <dimen name="button_bar_width_right_padding">8dp</dimen>
     <dimen name="button_bar_height_plus_padding">80dp</dimen>
+
+<!-- AppsCustomize -->
+    <dimen name="apps_customize_pageLayoutPaddingTop">16dp</dimen>
+    <dimen name="apps_customize_pageLayoutPaddingBottom">20dp</dimen>
+    <dimen name="apps_customize_pageLayoutPaddingLeft">8dp</dimen>
+    <dimen name="apps_customize_pageLayoutPaddingRight">8dp</dimen>
+    <dimen name="apps_customize_cell_height">92dp</dimen>
+    <dimen name="apps_customize_widget_cell_height_gap">16dp</dimen>
 </resources>
diff --git a/res/values-sw340dp-port/styles.xml b/res/values-sw340dp-port/styles.xml
index c006b85..2b1376d 100644
--- a/res/values-sw340dp-port/styles.xml
+++ b/res/values-sw340dp-port/styles.xml
@@ -32,4 +32,16 @@
         <item name="android:paddingTop">@dimen/toolbar_button_vertical_padding</item>
         <item name="android:paddingBottom">@dimen/toolbar_button_vertical_padding</item>
     </style>
+
+<!-- AppsCustomize -->
+    <style name="WorkspaceIcon.Portrait.AppsCustomize">
+        <item name="android:singleLine">false</item>
+        <item name="android:maxLines">2</item>
+        <item name="android:background">@null</item>
+        <item name="android:paddingTop">4dp</item>
+        <item name="android:paddingBottom">0dp</item>
+        <item name="android:paddingLeft">0dp</item>
+        <item name="android:paddingRight">0dp</item>
+        <item name="android:drawablePadding">6dp</item>
+    </style>
 </resources>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 7973ca8..faad606 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -55,11 +55,11 @@
     <dimen name="apps_customize_tab_bar_height">56dp</dimen>
     <dimen name="app_icon_size">48dp</dimen>
     <!-- The width can be 72dp because we don't have L/R padding -->
-    <dimen name="apps_customize_cell_width">72dp</dimen>
-    <dimen name="apps_customize_cell_height">80dp</dimen>
+    <dimen name="apps_customize_cell_width">74dp</dimen>
+    <dimen name="apps_customize_cell_height">82dp</dimen>
     <dimen name="apps_customize_max_gap">18dp</dimen>
     <dimen name="apps_customize_widget_cell_width_gap">10dp</dimen>
-    <dimen name="apps_customize_widget_cell_height_gap">10dp</dimen>
+    <dimen name="apps_customize_widget_cell_height_gap">8dp</dimen>
     <dimen name="title_texture_width">120px</dimen>
 
     <!-- height of the bottom row of controls -->
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 05a83b2..8368388 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -95,12 +95,14 @@
     </style>
 
     <style name="WorkspaceIcon.Portrait.AppsCustomize">
+        <item name="android:singleLine">false</item>
+        <item name="android:maxLines">2</item>
         <item name="android:background">@null</item>
-        <item name="android:paddingTop">4dp</item>
+        <item name="android:paddingTop">0dp</item>
         <item name="android:paddingBottom">0dp</item>
         <item name="android:paddingLeft">0dp</item>
         <item name="android:paddingRight">0dp</item>
-        <item name="android:drawablePadding">8dp</item>
+        <item name="android:drawablePadding">2dp</item>
     </style>
     <style name="WorkspaceIcon.Landscape.AppsCustomize">
         <item name="android:background">@null</item>
diff --git a/src/com/android/launcher2/AppsCustomizePagedView.java b/src/com/android/launcher2/AppsCustomizePagedView.java
index ca4d614..32b1787 100644
--- a/src/com/android/launcher2/AppsCustomizePagedView.java
+++ b/src/com/android/launcher2/AppsCustomizePagedView.java
@@ -31,10 +31,11 @@
 import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
+import android.graphics.Bitmap.Config;
 import android.graphics.Canvas;
 import android.graphics.PorterDuff;
 import android.graphics.Rect;
-import android.graphics.Bitmap.Config;
+import android.graphics.RectF;
 import android.graphics.drawable.Drawable;
 import android.os.AsyncTask;
 import android.os.Process;
@@ -46,7 +47,6 @@
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.animation.AccelerateInterpolator;
-import android.view.animation.DecelerateInterpolator;
 import android.widget.GridLayout;
 import android.widget.ImageView;
 import android.widget.Toast;
@@ -277,7 +277,7 @@
     @Override
     protected void init() {
         super.init();
-        mCenterPagesVertically = false;
+        mCenterPagesVertically = true;
 
         Context context = getContext();
         Resources r = context.getResources();
@@ -531,8 +531,11 @@
         // Compose the drag image
         Bitmap b;
         Drawable preview = image.getDrawable();
-        int w = preview.getIntrinsicWidth();
-        int h = preview.getIntrinsicHeight();
+        RectF mTmpScaleRect = new RectF(0f,0f,1f,1f);
+        image.getImageMatrix().mapRect(mTmpScaleRect);
+        float scale = mTmpScaleRect.right;
+        int w = (int) (preview.getIntrinsicWidth() * scale);
+        int h = (int) (preview.getIntrinsicHeight() * scale);
         if (createItemInfo instanceof PendingAddWidgetInfo) {
             PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
             int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
@@ -540,7 +543,7 @@
             createItemInfo.spanY = spanXY[1];
 
             b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
-            renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1, mDragViewMultiplyColor);
+            renderDrawableToBitmap(preview, b, 0, 0, w, h, scale, mDragViewMultiplyColor);
         } else {
             // Workaround for the fact that we don't keep the original ResolveInfo associated with
             // the shortcut around.  To get the icon, we just render the preview image (which has
@@ -891,15 +894,18 @@
         layout.setMinimumWidth(getPageContentWidth());
         layout.measure(widthSpec, heightSpec);
     }
-    private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
-            float scaleX, float scaleY) {
-        renderDrawableToBitmap(d, bitmap, x, y, w, h, scaleX, scaleY, 0xFFFFFFFF);
+    private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
+        renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
     }
     private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
-            float scaleX, float scaleY, int multiplyColor) {
+            float scale) {
+        renderDrawableToBitmap(d, bitmap, x, y, w, h, scale, 0xFFFFFFFF);
+    }
+    private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
+            float scale, int multiplyColor) {
         if (bitmap != null) {
             Canvas c = new Canvas(bitmap);
-            c.scale(scaleX, scaleY);
+            c.scale(scale, scale);
             Rect oldBounds = d.copyBounds();
             d.setBounds(x, y, x + w, y + h);
             d.draw(c);
@@ -911,26 +917,20 @@
         }
     }
     private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
+        // Render the background
+        int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
+        int bitmapSize = mAppIconSize + 2 * offset;
+        Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
+        renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapSize, bitmapSize);
+
         // Render the icon
-        Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888);
         Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
-        renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f);
+        renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
         return preview;
     }
     private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
             int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
 
-        // Calculate the size of the drawable
-        cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
-        cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
-        int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
-        int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
-
-        // Scale down the bitmap to fit the space
-        float widgetPreviewScale = (float) cellWidth / expectedWidth;
-        expectedWidth = (int) (widgetPreviewScale * expectedWidth);
-        expectedHeight = (int) (widgetPreviewScale * expectedHeight);
-
         // Load the preview image if possible
         String packageName = info.provider.getPackageName();
         Drawable drawable = null;
@@ -941,49 +941,59 @@
                 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
                         + " for provider: " + info.provider);
             } else {
-                // Scale down the preview to the dimensions we want
+                // Scale down the preview to something that is closer to the cellWidth/Height
                 int imageWidth = drawable.getIntrinsicWidth();
                 int imageHeight = drawable.getIntrinsicHeight();
-                float aspect = (float) imageWidth / imageHeight;
-                int newWidth = imageWidth;
-                int newHeight = imageHeight;
-                if (aspect > 1f) {
-                    newWidth = expectedWidth;
-                    newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
+                int bitmapWidth = imageWidth;
+                int bitmapHeight = imageHeight;
+                if (imageWidth > imageHeight) {
+                    bitmapWidth = cellWidth;
+                    bitmapHeight = (int) (imageHeight * ((float) bitmapWidth / imageWidth));
                 } else {
-                    newHeight = expectedHeight;
-                    newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
+                    bitmapHeight = cellHeight;
+                    bitmapWidth = (int) (imageWidth * ((float) bitmapHeight / imageHeight));
                 }
 
-                preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
-                renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
+                preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
+                renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
             }
         }
 
         // Generate a preview image if we couldn't load one
         if (drawable == null) {
             Resources resources = mLauncher.getResources();
-            int bitmapWidth;
-            int bitmapHeight;
+            // TODO: This actually uses the apps customize cell layout params, where as we make want
+            // the Workspace params for more accuracy.
+            int targetWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
+            int targetHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
+            int bitmapWidth = targetWidth;
+            int bitmapHeight = targetHeight;
+            int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
+            float iconScale = 1f;
 
-            // Specify the dimensions of the bitmap (since we are using a default preview bg with 
-            // the full icon, we only imply the aspect ratio of the widget)
+            // Determine the size of the bitmap we want to draw
             if (cellHSpan == cellVSpan) {
-                bitmapWidth = bitmapHeight = cellWidth;
-                expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension;
-            } else if (cellHSpan >= cellVSpan) {
-                bitmapWidth = expectedWidth = cellWidth;
-                bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension;
+                // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
+                if (cellHSpan <= 1) {
+                    bitmapWidth = bitmapHeight = mAppIconSize + 2 * offset;
+                } else {
+                    bitmapWidth = bitmapHeight = mAppIconSize + 4 * offset;
+                }
             } else {
-                // Note that in vertical widgets, we might not have enough space due to the text
-                // label, so be conservative and use the width as a height bound
-                bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension;
-                bitmapHeight = expectedHeight = cellWidth;
+                // Otherwise, ensure that we are properly sized within the cellWidth/Height
+                if (targetWidth > targetHeight) {
+                    bitmapWidth = Math.min(targetWidth, cellWidth);
+                    bitmapHeight = (int) (targetHeight * ((float) bitmapWidth / targetWidth));
+                    iconScale = Math.min((float) bitmapHeight / (mAppIconSize + 2 * offset), 1f);
+                } else {
+                    bitmapHeight = Math.min(targetHeight, cellHeight);
+                    bitmapWidth = (int) (targetWidth * ((float) bitmapHeight / targetHeight));
+                    iconScale = Math.min((float) bitmapWidth / (mAppIconSize + 2 * offset), 1f);
+                }
             }
-
             preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
-            renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
-                    expectedHeight, 1f, 1f);
+            renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
+                    bitmapWidth);
 
             // Draw the icon in the top left corner
             try {
@@ -991,9 +1001,9 @@
                 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
                 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
 
-                int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
-                renderDrawableToBitmap(icon, preview, offset, offset,
-                        mAppIconSize, mAppIconSize, 1f, 1f);
+                renderDrawableToBitmap(icon, preview, (int) (offset * iconScale),
+                        (int) (offset * iconScale), (int) (mAppIconSize * iconScale),
+                        (int) (mAppIconSize * iconScale));
             } catch (Resources.NotFoundException e) {}
         }
         return preview;
@@ -1007,7 +1017,8 @@
             PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
                     mWidgetCountY);
             setupPage(layout);
-            addView(layout);
+            addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
+                    LayoutParams.MATCH_PARENT));
         }
     }
     public void syncWidgetPageItems(int page, boolean immediate) {
@@ -1126,7 +1137,12 @@
         for (int i = 0; i < count; ++i) {
             PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
             if (widget != null) {
-                widget.applyPreview(new FastBitmapDrawable(data.generatedImages.get(i)), i);
+                Bitmap preview = data.generatedImages.get(i);
+                boolean scale =
+                    (preview.getWidth() >= data.cellWidth ||
+                     preview.getHeight() >= data.cellHeight);
+
+                widget.applyPreview(new FastBitmapDrawable(preview), i, scale);
             }
         }
         layout.createHardwareLayer();
diff --git a/src/com/android/launcher2/PagedViewCellLayout.java b/src/com/android/launcher2/PagedViewCellLayout.java
index 5e87b46..2ef7e29 100644
--- a/src/com/android/launcher2/PagedViewCellLayout.java
+++ b/src/com/android/launcher2/PagedViewCellLayout.java
@@ -367,7 +367,7 @@
     }
 
     /**
-     * Estimates the width that the number of vSpan cells will take up.
+     * Estimates the width that the number of hSpan cells will take up.
      */
     public int estimateCellWidth(int hSpan) {
         // TODO: we need to take widthGap into effect
diff --git a/src/com/android/launcher2/PagedViewWidget.java b/src/com/android/launcher2/PagedViewWidget.java
index adf94ab..4d5fd7b 100644
--- a/src/com/android/launcher2/PagedViewWidget.java
+++ b/src/com/android/launcher2/PagedViewWidget.java
@@ -22,23 +22,13 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.res.Resources;
-import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 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;
 import android.util.AttributeSet;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
-import android.view.View;
 import android.widget.Checkable;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
@@ -145,10 +135,11 @@
         }
     }
 
-    void applyPreview(FastBitmapDrawable preview, int index) {
+    void applyPreview(FastBitmapDrawable preview, int index, boolean scale) {
         final ImageView image = (ImageView) findViewById(R.id.widget_preview);
         if (preview != null) {
             image.setImageDrawable(preview);
+            image.setScaleType(scale ? ImageView.ScaleType.FIT_START : ImageView.ScaleType.MATRIX);
             image.setAlpha(0f);
             image.animate()
                  .alpha(1f)