Bookmark widget favicon drawing cleanup

 Bug: 3289421
 Changes the favicon drawing for the bookmark widget to keep the favicon
 at its original size and draw a mostly transparent rounded white rectangle
 behind it.

Change-Id: I8267bbcc3d805d0266281f231e38393eec4d0bc2
diff --git a/res/layout/bookmarklistwidget_item.xml b/res/layout/bookmarklistwidget_item.xml
index 9d1f8ec..2f64054 100644
--- a/res/layout/bookmarklistwidget_item.xml
+++ b/res/layout/bookmarklistwidget_item.xml
@@ -24,11 +24,11 @@
     <ImageView
         android:id="@+id/thumb"
         android:src="@drawable/browser_thumbnail"
-        android:layout_height="32dp"
-        android:layout_width="32dp"
+        android:layout_height="@dimen/bookmark_widget_thumb_size"
+        android:layout_width="@dimen/bookmark_widget_thumb_size"
         android:layout_gravity="center_vertical"
         android:layout_marginLeft="16dp"
-        android:scaleType="fitCenter"/>
+        android:scaleType="centerInside"/>
     <TextView
         android:id="@+id/label"
         android:layout_width="0dip"
diff --git a/res/values/colors.xml b/res/values/colors.xml
index 31a8458..cb1b754 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -31,4 +31,5 @@
     <color name="bookmarkWidgetFolderBackground">#A0383847</color>
     <color name="qc_slice_normal">#C0A0A0A0</color>
     <color name="qc_slice_active">#C02090FF</color>
+    <color name="bookmarkWidgetFaviconBackground">#23ffffff</color>
 </resources>
diff --git a/res/values/dimensions.xml b/res/values/dimensions.xml
index a4ab90a..ec7f94c 100644
--- a/res/values/dimensions.xml
+++ b/res/values/dimensions.xml
@@ -31,4 +31,6 @@
     <dimen name="qc_radius">130dip</dimen>
     <dimen name="qc_radius_inc">100dip</dimen>
     <dimen name="qc_slop">15dip</dimen>
+    <dimen name="bookmark_widget_thumb_size">32dip</dimen>
+    <dimen name="bookmark_widget_favicon_size">26dip</dimen>
 </resources>
diff --git a/src/com/android/browser/BookmarkUtils.java b/src/com/android/browser/BookmarkUtils.java
index d2eec66..f261cb3 100644
--- a/src/com/android/browser/BookmarkUtils.java
+++ b/src/com/android/browser/BookmarkUtils.java
@@ -44,8 +44,9 @@
 
     enum BookmarkIconType {
         ICON_INSTALLABLE_WEB_APP, // Icon for an installable web app (launches WebAppRuntime).
-        ICON_HOME_SHORTCUT        // Icon for a shortcut on the home screen (launches Browser).
-    };
+        ICON_HOME_SHORTCUT,        // Icon for a shortcut on the home screen (launches Browser).
+        ICON_WIDGET,
+    }
 
     /**
      * Creates an icon to be associated with this bookmark. If available, the apple touch icon
@@ -56,6 +57,20 @@
         int iconDimension = context.getResources().getDimensionPixelSize(
                 android.R.dimen.app_icon_size);
 
+        return createIcon(context, touchIcon, favicon, type, iconDimension);
+    }
+
+    public static Bitmap createListWidgetIcon(Context context, Bitmap touchIcon,
+            Bitmap favicon) {
+        int iconDimension = context.getResources().getDimensionPixelSize(
+                R.dimen.bookmark_widget_favicon_size);
+
+        return createIcon(context, touchIcon, favicon,
+                BookmarkIconType.ICON_WIDGET, iconDimension);
+    }
+
+    private static Bitmap createIcon(Context context, Bitmap touchIcon,
+            Bitmap favicon, BookmarkIconType type, int iconDimension) {
         Bitmap bm = Bitmap.createBitmap(iconDimension, iconDimension, Bitmap.Config.ARGB_8888);
         Canvas canvas = new Canvas(bm);
         Rect iconBounds = new Rect(0, 0, bm.getWidth(), bm.getHeight());
@@ -77,7 +92,7 @@
             // If we have a favicon, overlay it in a nice rounded white box on top of the
             // background.
             if (favicon != null) {
-                drawFaviconToCanvas(context, favicon, canvas, iconBounds);
+                drawFaviconToCanvas(context, favicon, canvas, iconBounds, type);
             }
         }
         return bm;
@@ -139,28 +154,40 @@
         canvas.drawPath(path, paint);
     }
 
-    private static void drawFaviconToCanvas(Context context, Bitmap favicon, Canvas canvas,
-            Rect iconBounds) {
+    private static void drawFaviconToCanvas(Context context, Bitmap favicon,
+            Canvas canvas, Rect iconBounds, BookmarkIconType type) {
         // Make a Paint for the white background rectangle and for
         // filtering the favicon.
         Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
         p.setStyle(Paint.Style.FILL_AND_STROKE);
-        p.setColor(Color.WHITE);
+        if (type == BookmarkIconType.ICON_WIDGET) {
+            p.setColor(context.getResources()
+                    .getColor(R.color.bookmarkWidgetFaviconBackground));
+        } else {
+            p.setColor(Color.WHITE);
+        }
 
         // Create a rectangle that is slightly wider than the favicon
         int faviconDimension = context.getResources().getDimensionPixelSize(R.dimen.favicon_size);
-        int faviconPaddedRectDimension = context.getResources().getDimensionPixelSize(
-                R.dimen.favicon_padded_size);
+        int faviconPaddedRectDimension;
+        if (type == BookmarkIconType.ICON_WIDGET) {
+            faviconPaddedRectDimension = canvas.getWidth();
+        } else {
+            faviconPaddedRectDimension = context.getResources().getDimensionPixelSize(
+                    R.dimen.favicon_padded_size);
+        }
         float padding = (faviconPaddedRectDimension - faviconDimension) / 2;
         final float x = iconBounds.exactCenterX() - (faviconPaddedRectDimension / 2);
-        // Note: Subtract from the y position since the box is
-        // slightly higher than center. Use padding since it is already
-        // device independent.
-        final float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2) - padding;
+        float y = iconBounds.exactCenterY() - (faviconPaddedRectDimension / 2);
+        if (type != BookmarkIconType.ICON_WIDGET) {
+            // Note: Subtract from the y position since the box is
+            // slightly higher than center. Use padding since it is already
+            // device independent.
+            y -= padding;
+        }
         RectF r = new RectF(x, y, x + faviconPaddedRectDimension, y + faviconPaddedRectDimension);
-
         // Draw a white rounded rectangle behind the favicon
-        canvas.drawRoundRect(r, 2, 2, p);
+        canvas.drawRoundRect(r, 3, 3, p);
 
         // Draw the favicon in the same rectangle as the rounded
         // rectangle but inset by the padding
diff --git a/src/com/android/browser/widget/BookmarkListWidgetService.java b/src/com/android/browser/widget/BookmarkListWidgetService.java
index 30671fc..39751e6 100644
--- a/src/com/android/browser/widget/BookmarkListWidgetService.java
+++ b/src/com/android/browser/widget/BookmarkListWidgetService.java
@@ -16,6 +16,7 @@
 
 package com.android.browser.widget;
 
+import com.android.browser.BookmarkUtils;
 import com.android.browser.BrowserBookmarksPage;
 import com.android.browser.R;
 
@@ -362,16 +363,37 @@
                         RenderResult res = new RenderResult(id, title, url);
                         res.mIsFolder = c.getInt(BOOKMARK_INDEX_IS_FOLDER) != 0;
                         if (!res.mIsFolder) {
+                            // RemoteViews require a valid bitmap config
+                            Options options = new Options();
+                            options.inPreferredConfig = Config.ARGB_8888;
+                            Bitmap favIcon = null;
+                            Bitmap touchIcon = null;
                             byte[] blob = c.getBlob(BOOKMARK_INDEX_TOUCH_ICON);
-                            if (blob == null || blob.length == 0) {
-                                blob = c.getBlob(BOOKMARK_INDEX_FAVICON);
-                            }
-                            if (blob != null) {
-                                // RemoteViews require a valid bitmap config
-                                Options options = new Options();
-                                options.inPreferredConfig = Config.ARGB_8888;
-                                res.mBitmap = BitmapFactory.decodeByteArray(
+                            if (blob != null && blob.length > 0) {
+                                touchIcon = BitmapFactory.decodeByteArray(
                                         blob, 0, blob.length, options);
+                            } else {
+                                blob = c.getBlob(BOOKMARK_INDEX_FAVICON);
+                                if (blob != null && blob.length > 0) {
+                                    favIcon = BitmapFactory.decodeByteArray(
+                                            blob, 0, blob.length, options);
+                                }
+                            }
+
+                            if (favIcon == null) {
+                                favIcon = BitmapFactory.decodeResource(
+                                        mContext.getResources(),
+                                        R.drawable.app_web_browser_sm);
+                            }
+                            if (touchIcon != null || favIcon != null) {
+                                res.mBitmap = BookmarkUtils.createListWidgetIcon(
+                                        mContext, touchIcon, favIcon);
+                            }
+                            if (touchIcon != null) {
+                                touchIcon.recycle();
+                            }
+                            if (favIcon != null) {
+                                favIcon.recycle();
                             }
                         }
                         bookmarks.add(res);