Adding outlines to side pages for widgets and wallpaper pages in the customization tray.

Change-Id: Ib2ec5e630c1c861003d0e0b5672ed69ef25c0b47
diff --git a/res/layout-xlarge-land/customization_drawer.xml b/res/layout-xlarge-land/customization_drawer.xml
index 304aaf7..3fff2b0 100644
--- a/res/layout-xlarge-land/customization_drawer.xml
+++ b/res/layout-xlarge-land/customization_drawer.xml
@@ -19,7 +19,7 @@
 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    launcher:wallpaperCellSpanX="4"
+    launcher:wallpaperCellSpanX="3"
     launcher:widgetCellCountX="16"
     launcher:cellCountX="8"
     launcher:cellCountY="3"
diff --git a/res/layout-xlarge/customize_paged_view_wallpaper.xml b/res/layout-xlarge/customize_paged_view_wallpaper.xml
index 6cf248a..e3be86d 100644
--- a/res/layout-xlarge/customize_paged_view_wallpaper.xml
+++ b/res/layout-xlarge/customize_paged_view_wallpaper.xml
@@ -13,8 +13,9 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<LinearLayout
+<com.android.launcher2.PagedViewWidget
     xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
 
     android:layout_width="wrap_content"
     android:layout_height="365dp"
@@ -22,7 +23,10 @@
     android:paddingRight="12.5dp"
     android:paddingBottom="50dp"
     android:gravity="top"
-    android:orientation="vertical">
+    android:orientation="vertical"
+
+    launcher:blurColor="#FF6B8CF0"
+    launcher:outlineColor="#FF8CD2FF">
 
     <!-- The preview image for the wallpaper. -->
     <ImageView
@@ -57,4 +61,4 @@
 
         android:maxLines="2"
         android:fadingEdge="horizontal" />
-</LinearLayout>
+</com.android.launcher2.PagedViewWidget>
diff --git a/res/layout-xlarge/customize_paged_view_widget.xml b/res/layout-xlarge/customize_paged_view_widget.xml
index deeeb3d..3b95ebc 100644
--- a/res/layout-xlarge/customize_paged_view_widget.xml
+++ b/res/layout-xlarge/customize_paged_view_widget.xml
@@ -13,20 +13,21 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<LinearLayout
+<com.android.launcher2.PagedViewWidget
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
 
     android:layout_width="wrap_content"
     android:layout_height="365dp"
     android:paddingLeft="12.5dp"
+    android:paddingTop="12.5dp"
     android:paddingRight="12.5dp"
     android:paddingBottom="50dp"
     android:gravity="top"
     android:orientation="vertical"
 
-    launcher:checkedBlurColor="#FFDAFF71"
-    launcher:checkedOutlineColor="#FFCFFF9C">
+    launcher:blurColor="#FF6B8CF0"
+    launcher:outlineColor="#FF8CD2FF">
 
     <!-- The icon of the widget. -->
     <ImageView
@@ -76,4 +77,4 @@
         android:shadowDx="0.0"
         android:shadowDy="1.0"
         android:shadowRadius="1.0" />
-</LinearLayout>
+</com.android.launcher2.PagedViewWidget>
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index 975548e..853d49e 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -74,6 +74,15 @@
         <attr name="checkedOutlineColor" format="color" />
     </declare-styleable>
 
+    <!-- PagedViewWidget specific attributes. These attributes are used to
+         customize a PagedViewWidget view in XML files. -->
+    <declare-styleable name="PagedViewWidget">
+        <!-- The blur color of the holographic outline -->
+        <attr name="blurColor" />
+        <!-- The outline color of the holographic outline -->
+        <attr name="outlineColor" />
+    </declare-styleable>
+
     <!-- PagedView specific attributes. These attributes are used to customize
          a PagedView view in XML files. -->
     <declare-styleable name="PagedView">
diff --git a/src/com/android/launcher2/CustomizePagedView.java b/src/com/android/launcher2/CustomizePagedView.java
index b48d4ab..50ec64b 100644
--- a/src/com/android/launcher2/CustomizePagedView.java
+++ b/src/com/android/launcher2/CustomizePagedView.java
@@ -569,7 +569,7 @@
      * This method will extract the preview image specified by the wallpaper source provider (if it
      * exists) otherwise, it will try to generate a default image preview.
      */
-    private Drawable getWallpaperPreview(ResolveInfo info) {
+    private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
         // To be implemented later: resolving the up-to-date wallpaper thumbnail
 
         final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
@@ -597,7 +597,7 @@
             // if we can't find the icon, then just don't draw it
         }
 
-        Drawable drawable = new FastBitmapDrawable(bitmap);
+        FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
         drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
         return drawable;
     }
@@ -607,10 +607,11 @@
      * otherwise, it will try to generate a default image preview with the widget's package icon.
      * @return the drawable that will be used and sized in the ImageView to represent the widget
      */
-    private Drawable getWidgetPreview(AppWidgetProviderInfo info) {
+    private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
         final PackageManager packageManager = mPackageManager;
         String packageName = info.provider.getPackageName();
         Drawable drawable = null;
+        FastBitmapDrawable newDrawable = null;
         if (info.previewImage != 0) {
             drawable = packageManager.getDrawable(packageName, info.previewImage, null);
             if (drawable == null) {
@@ -649,7 +650,7 @@
                 // if we can't find the icon, then just don't draw it
             }
 
-            drawable = new FastBitmapDrawable(bitmap);
+            newDrawable = new FastBitmapDrawable(bitmap);
         } else {
             // Scale down the preview if necessary
             final float imageWidth = drawable.getIntrinsicWidth();
@@ -672,10 +673,11 @@
             final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
             renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
 
-            drawable = new FastBitmapDrawable(bitmap);
+            newDrawable = new FastBitmapDrawable(bitmap);
         }
-        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
-        return drawable;
+        newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
+                newDrawable.getIntrinsicHeight());
+        return newDrawable;
     }
 
     private void setupPage(PagedViewCellLayout layout) {
@@ -720,28 +722,18 @@
         for (int i = 0; i < count; ++i) {
             final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
             final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
+            final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
+                    info.minHeight, null);
+            final FastBitmapDrawable icon = getWidgetPreview(info);
 
-            LinearLayout l = (LinearLayout) mInflater.inflate(
+            PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
                     R.layout.customize_paged_view_widget, layout, false);
+            l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
             l.setTag(createItemInfo);
             l.setOnClickListener(this);
             l.setOnTouchListener(this);
             l.setOnLongClickListener(this);
 
-            final Drawable icon = getWidgetPreview(info);
-
-            int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
-            final int hSpan = spans[0];
-            final int vSpan = spans[1];
-
-            ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
-            image.setMaxWidth(mMaxWidgetWidth);
-            image.setImageDrawable(icon);
-            TextView name = (TextView) l.findViewById(R.id.widget_name);
-            name.setText(info.label);
-            TextView dims = (TextView) l.findViewById(R.id.widget_dims);
-            dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
-
             layout.addView(l);
         }
     }
@@ -775,20 +767,14 @@
         final int endIndex = Math.min(count, startIndex + numItemsPerPage);
         for (int i = startIndex; i < endIndex; ++i) {
             final ResolveInfo info = mWallpaperList.get(i);
+            final FastBitmapDrawable icon = getWallpaperPreview(info);
 
-            LinearLayout l = (LinearLayout) mInflater.inflate(
+            PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
                     R.layout.customize_paged_view_wallpaper, layout, false);
+            l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
             l.setTag(info);
             l.setOnClickListener(this);
 
-            final Drawable icon = getWallpaperPreview(info);
-
-            ImageView image = (ImageView) l.findViewById(R.id.wallpaper_preview);
-            image.setMaxWidth(mMaxWidgetWidth);
-            image.setImageDrawable(icon);
-            TextView name = (TextView) l.findViewById(R.id.wallpaper_name);
-            name.setText(info.loadLabel(mPackageManager));
-
             layout.addView(l);
         }
     }
diff --git a/src/com/android/launcher2/FastBitmapDrawable.java b/src/com/android/launcher2/FastBitmapDrawable.java
index 1aa8b35..a453294 100644
--- a/src/com/android/launcher2/FastBitmapDrawable.java
+++ b/src/com/android/launcher2/FastBitmapDrawable.java
@@ -26,11 +26,13 @@
 
 class FastBitmapDrawable extends Drawable {
     private Bitmap mBitmap;
+    private int mAlpha;
     private int mWidth;
     private int mHeight;
     private final Paint mPaint = new Paint();
 
     FastBitmapDrawable(Bitmap b) {
+	mAlpha = 255;
         mBitmap = b;
         if (b != null) {
             mWidth = mBitmap.getWidth();
@@ -53,9 +55,14 @@
 
     @Override
     public void setAlpha(int alpha) {
+        mAlpha = alpha;
         mPaint.setAlpha(alpha);
     }
 
+    public int getAlpha() {
+        return mAlpha;
+    }
+
     @Override
     public void setColorFilter(ColorFilter cf) {
     }
diff --git a/src/com/android/launcher2/PagedView.java b/src/com/android/launcher2/PagedView.java
index d24eef4..31cfb5b 100644
--- a/src/com/android/launcher2/PagedView.java
+++ b/src/com/android/launcher2/PagedView.java
@@ -566,9 +566,15 @@
             rightScreen = Math.min(getChildCount() - 1, rightScreen);
 
             final long drawingTime = getDrawingTime();
+            // Clip to the bounds
+            canvas.save();
+            canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft,
+                    mScrollY + mBottom - mTop);
+
             for (int i = leftScreen; i <= rightScreen; i++) {
                 drawChild(canvas, getChildAt(i), drawingTime);
             }
+            canvas.restore();
         }
     }
 
diff --git a/src/com/android/launcher2/PagedViewIcon.java b/src/com/android/launcher2/PagedViewIcon.java
index 9e48351..0e72598 100644
--- a/src/com/android/launcher2/PagedViewIcon.java
+++ b/src/com/android/launcher2/PagedViewIcon.java
@@ -52,7 +52,7 @@
     private Object mIconCacheKey;
     private PagedViewIconCache mIconCache;
 
-    private int mAlpha = -1;
+    private int mAlpha;
     private int mHolographicAlpha;
 
     private boolean mIsChecked;
@@ -104,12 +104,13 @@
 
     public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
+
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0);
+	mAlpha = 255;
         mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0);
         mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0);
         mCheckedBlurColor = a.getColor(R.styleable.PagedViewIcon_checkedBlurColor, 0);
         mCheckedOutlineColor = a.getColor(R.styleable.PagedViewIcon_checkedOutlineColor, 0);
-
         a.recycle();
 
         if (sHolographicOutlineHelper == null) {
diff --git a/src/com/android/launcher2/PagedViewWidget.java b/src/com/android/launcher2/PagedViewWidget.java
new file mode 100644
index 0000000..40e507b
--- /dev/null
+++ b/src/com/android/launcher2/PagedViewWidget.java
@@ -0,0 +1,218 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.launcher2;
+
+import android.appwidget.AppWidgetProviderInfo;
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.content.res.TypedArray;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.drawable.Drawable;
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.os.Message;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.widget.ImageView;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import com.android.launcher.R;
+import com.android.launcher2.PagedView.PagedViewIconCache;
+
+/**
+ * The linear layout used strictly for the widget/wallpaper tab of the customization tray
+ */
+public class PagedViewWidget extends LinearLayout {
+    static final String TAG = "PagedViewWidgetLayout";
+
+    private final Paint mPaint = new Paint();
+    private static HolographicOutlineHelper sHolographicOutlineHelper;
+    private Bitmap mHolographicOutline;
+    private final Canvas mHolographicOutlineCanvas = new Canvas();
+    private FastBitmapDrawable mPreview;
+
+    private int mAlpha = 255;
+    private int mHolographicAlpha;
+
+    // Highlight colors
+    private int mHoloBlurColor;
+    private int mHoloOutlineColor;
+
+    private static final HandlerThread sWorkerThread = new HandlerThread("pagedviewwidget-helper");
+    static {
+        sWorkerThread.start();
+    }
+
+    private static final int MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE = 1;
+
+    private static final Handler sWorker = new Handler(sWorkerThread.getLooper()) {
+        private DeferredHandler mHandler = new DeferredHandler();
+        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);
+
+            widget.mHolographicOutlineCanvas.setBitmap(outline);
+            widget.mHolographicOutlineCanvas.save();
+            widget.mHolographicOutlineCanvas.translate(widget.mPaddingLeft, widget.mPaddingTop);
+            widget.mPreview.setAlpha(255);
+            widget.mPreview.draw(widget.mHolographicOutlineCanvas);
+            widget.mPreview.setAlpha(prevAlpha);
+            widget.mHolographicOutlineCanvas.restore();
+
+            sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(outline,
+                    widget.mHolographicOutlineCanvas, widget.mHoloBlurColor,
+                    widget.mHoloOutlineColor);
+
+            mHandler.post(new Runnable() {
+                public void run() {
+                    widget.mHolographicOutline = outline;
+                    widget.invalidate();
+                }
+            });
+        }
+    };
+
+    public PagedViewWidget(Context context) {
+        this(context, null);
+    }
+
+    public PagedViewWidget(Context context, AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public PagedViewWidget(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+
+        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewWidget,
+                defStyle, 0);
+        mHoloBlurColor = a.getColor(R.styleable.PagedViewWidget_blurColor, 0);
+        mHoloOutlineColor = a.getColor(R.styleable.PagedViewWidget_outlineColor, 0);
+        a.recycle();
+
+        if (sHolographicOutlineHelper == null) {
+            sHolographicOutlineHelper = new HolographicOutlineHelper();
+        }
+
+        setFocusable(true);
+        setWillNotDraw(false);
+        setClipToPadding(false);
+    }
+
+    private void queueHolographicOutlineCreation() {
+        // Generate the outline in the background
+        if (mHolographicOutline == null) {
+            Message m = sWorker.obtainMessage(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE);
+            m.obj = this;
+            sWorker.sendMessage(m);
+        }
+    }
+
+    public void applyFromAppWidgetProviderInfo(AppWidgetProviderInfo info,
+            FastBitmapDrawable preview, int maxWidth, int[] cellSpan) {
+        final ImageView image = (ImageView) findViewById(R.id.widget_preview);
+        image.setMaxWidth(maxWidth);
+        image.setImageDrawable(preview);
+        final TextView name = (TextView) findViewById(R.id.widget_name);
+        name.setText(info.label);
+        final TextView dims = (TextView) findViewById(R.id.widget_dims);
+        dims.setText(mContext.getString(R.string.widget_dims_format, cellSpan[0], cellSpan[1]));
+        mPreview = preview;
+    }
+
+    public void applyFromWallpaperInfo(ResolveInfo info, PackageManager packageManager,
+            FastBitmapDrawable preview, int maxWidth) {
+        ImageView image = (ImageView) findViewById(R.id.wallpaper_preview);
+        image.setMaxWidth(maxWidth);
+        image.setImageDrawable(preview);
+        TextView name = (TextView) findViewById(R.id.wallpaper_name);
+        name.setText(info.loadLabel(packageManager));
+        mPreview = preview;
+    }
+
+    @Override
+    public boolean onTouchEvent(MotionEvent event) {
+        // We eat up the touch events here, since the PagedView (which uses the same swiping
+        // touch code as Workspace previously) uses onInterceptTouchEvent() to determine when
+        // the user is scrolling between pages.  This means that if the pages themselves don't
+        // handle touch events, it gets forwarded up to PagedView itself, and it's own
+        // onTouchEvent() handling will prevent further intercept touch events from being called
+        // (it's the same view in that case).  This is not ideal, but to prevent more changes,
+        // we just always mark the touch event as handled.
+        return super.onTouchEvent(event) || true;
+    }
+
+    @Override
+    protected void onDraw(Canvas canvas) {
+        if (mAlpha > 0) {
+            super.onDraw(canvas);
+        }
+
+        // draw any blended overlays
+        if (mHolographicOutline != null && mHolographicAlpha > 0) {
+            mPaint.setAlpha(mHolographicAlpha);
+            canvas.drawBitmap(mHolographicOutline, 0, 0, mPaint);
+        }
+    }
+
+    @Override
+    protected boolean onSetAlpha(int alpha) {
+        return true;
+    }
+
+    @Override
+    public void setAlpha(float alpha) {
+        final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
+        final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
+        int newViewAlpha = (int) (viewAlpha * 255);
+        int newHolographicAlpha = (int) (holographicAlpha * 255);
+        if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
+            mAlpha = newViewAlpha;
+            mHolographicAlpha = newHolographicAlpha;
+            setChildrenAlpha(viewAlpha);
+            super.setAlpha(viewAlpha);
+        }
+    }
+
+    private void setChildrenAlpha(float alpha) {
+        final int childCount = getChildCount();
+        for (int i = 0; i < childCount; i++) {
+            getChildAt(i).setAlpha(alpha);
+        }
+    }
+
+    @Override
+    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+        if (w > 0 && h > 0) {
+            queueHolographicOutlineCreation();
+        }
+
+        super.onSizeChanged(w, h, oldw, oldh);
+    }
+
+    @Override
+    public void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this);
+    }
+}