Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.content.Context; |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 20 | import android.content.pm.PackageManager; |
| 21 | import android.content.pm.ResolveInfo; |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 24 | import android.graphics.Canvas; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 25 | import android.graphics.Paint; |
| 26 | import android.graphics.PointF; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 27 | import android.graphics.Rect; |
| 28 | import android.graphics.Region.Op; |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 31 | import android.widget.Checkable; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 32 | import android.widget.TextView; |
| 33 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 34 | import com.android.launcher.R; |
| 35 | import com.android.launcher2.PagedView.PagedViewIconCache; |
| 36 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 37 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * An icon on a PagedView, specifically for items in the launcher's paged view (with compound |
| 41 | * drawables on the top). |
| 42 | */ |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 43 | public class PagedViewIcon extends TextView implements Checkable { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 44 | private static final String TAG = "PagedViewIcon"; |
| 45 | |
| 46 | // holographic outline |
| 47 | private final Paint mPaint = new Paint(); |
| 48 | private static HolographicOutlineHelper sHolographicOutlineHelper; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 49 | private Bitmap mCheckedOutline; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 50 | private Bitmap mHolographicOutline; |
| 51 | private Canvas mHolographicOutlineCanvas; |
| 52 | private boolean mIsHolographicUpdatePass; |
| 53 | private Rect mDrawableClipRect; |
| 54 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 55 | private Object mIconCacheKey; |
| 56 | private PagedViewIconCache mIconCache; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 57 | private int mScaledIconSize; |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 58 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 59 | private int mAlpha; |
| 60 | private int mHolographicAlpha; |
| 61 | |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 62 | private boolean mIsChecked; |
| 63 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 64 | // Highlight colours |
| 65 | private int mHoloBlurColor; |
| 66 | private int mHoloOutlineColor; |
| 67 | private int mCheckedBlurColor; |
| 68 | private int mCheckedOutlineColor; |
| 69 | |
| 70 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 71 | public PagedViewIcon(Context context) { |
| 72 | this(context, null); |
| 73 | } |
| 74 | |
| 75 | public PagedViewIcon(Context context, AttributeSet attrs) { |
| 76 | this(context, attrs, 0); |
| 77 | } |
| 78 | |
| 79 | public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) { |
| 80 | super(context, attrs, defStyle); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 81 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0); |
| 82 | mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0); |
| 83 | mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0); |
| 84 | mCheckedBlurColor = a.getColor(R.styleable.PagedViewIcon_checkedBlurColor, 0); |
| 85 | mCheckedOutlineColor = a.getColor(R.styleable.PagedViewIcon_checkedOutlineColor, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 86 | mScaledIconSize = a.getDimensionPixelSize(R.styleable.PagedViewIcon_scaledIconSize, 64); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 87 | a.recycle(); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 88 | |
| 89 | if (sHolographicOutlineHelper == null) { |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 90 | sHolographicOutlineHelper = new HolographicOutlineHelper(); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 91 | } |
| 92 | mDrawableClipRect = new Rect(); |
| 93 | |
| 94 | setFocusable(true); |
| 95 | setBackgroundDrawable(null); |
| 96 | } |
| 97 | |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 98 | public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache, |
| 99 | boolean scaleUp) { |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 100 | mIconCache = cache; |
| 101 | mIconCacheKey = info; |
| 102 | mHolographicOutline = mIconCache.getOutline(mIconCacheKey); |
| 103 | |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 104 | Bitmap icon; |
| 105 | if (scaleUp) { |
| 106 | icon = Bitmap.createScaledBitmap(info.iconBitmap, mScaledIconSize, |
| 107 | mScaledIconSize, true); |
| 108 | } else { |
| 109 | icon = info.iconBitmap; |
| 110 | } |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 111 | setCompoundDrawablesWithIntrinsicBounds(null, |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 112 | new FastBitmapDrawable(icon), null, null); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 113 | setText(info.title); |
| 114 | setTag(info); |
| 115 | } |
| 116 | |
| 117 | public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager, |
| 118 | PagedViewIconCache cache) { |
| 119 | mIconCache = cache; |
| 120 | mIconCacheKey = info; |
| 121 | mHolographicOutline = mIconCache.getOutline(mIconCacheKey); |
| 122 | |
Winson Chung | c1f48b9 | 2010-09-30 12:07:45 -0700 | [diff] [blame] | 123 | Bitmap image = Utilities.createIconBitmap(info.loadIcon(packageManager), mContext); |
| 124 | setCompoundDrawablesWithIntrinsicBounds(null, |
| 125 | new FastBitmapDrawable(image), null, null); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 126 | setText(info.loadLabel(packageManager)); |
| 127 | setTag(info); |
| 128 | } |
| 129 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public void setAlpha(float alpha) { |
| 132 | final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 133 | final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha); |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 134 | mAlpha = (int) (viewAlpha * 255); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 135 | mHolographicAlpha = (int) (holographicAlpha * 255); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 136 | super.setAlpha(viewAlpha); |
| 137 | } |
| 138 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 139 | public void invalidateCheckedImage() { |
| 140 | if (mCheckedOutline != null) { |
| 141 | mCheckedOutline.recycle(); |
| 142 | mCheckedOutline = null; |
| 143 | } |
| 144 | } |
| 145 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 146 | @Override |
| 147 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 148 | super.onLayout(changed, left, top, right, bottom); |
| 149 | |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 150 | if (mIconCache != null && mHolographicOutline == null) { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 151 | // update the clipping rect to be used in the holographic pass below |
| 152 | getDrawingRect(mDrawableClipRect); |
| 153 | mDrawableClipRect.bottom = getPaddingTop() + getCompoundPaddingTop(); |
| 154 | |
| 155 | // set a flag to indicate that we are going to draw the view at full alpha with the text |
| 156 | // clipped for the generation of the holographic icon |
| 157 | mIsHolographicUpdatePass = true; |
| 158 | mHolographicOutline = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), |
| 159 | Bitmap.Config.ARGB_8888); |
| 160 | mHolographicOutlineCanvas = new Canvas(mHolographicOutline); |
| 161 | mHolographicOutlineCanvas.concat(getMatrix()); |
| 162 | draw(mHolographicOutlineCanvas); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 163 | sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(mHolographicOutline, |
| 164 | mHolographicOutlineCanvas, mHoloBlurColor, mHoloOutlineColor); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 165 | mIsHolographicUpdatePass = false; |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 166 | mIconCache.addOutline(mIconCacheKey, mHolographicOutline); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 167 | mHolographicOutlineCanvas = null; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 171 | @Override |
| 172 | protected void onDraw(Canvas canvas) { |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 173 | // draw the view itself |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 174 | if (mIsHolographicUpdatePass) { |
| 175 | // only clip to the text view (restore its alpha so that we get a proper outline) |
| 176 | canvas.save(); |
| 177 | canvas.clipRect(mDrawableClipRect, Op.REPLACE); |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 178 | final float alpha = getAlpha(); |
| 179 | super.setAlpha(1.0f); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 180 | super.onDraw(canvas); |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 181 | super.setAlpha(alpha); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 182 | canvas.restore(); |
| 183 | } else { |
| 184 | if (mAlpha > 0) { |
| 185 | super.onDraw(canvas); |
| 186 | } |
| 187 | } |
| 188 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 189 | // draw any blended overlays |
| 190 | if (!mIsHolographicUpdatePass) { |
| 191 | if (mCheckedOutline == null) { |
| 192 | if (mHolographicOutline != null && mHolographicAlpha > 0) { |
| 193 | mPaint.setAlpha(mHolographicAlpha); |
| 194 | canvas.drawBitmap(mHolographicOutline, 0, 0, mPaint); |
| 195 | } |
| 196 | } else { |
| 197 | mPaint.setAlpha(255); |
| 198 | canvas.drawBitmap(mCheckedOutline, 0, 0, mPaint); |
| 199 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 202 | |
| 203 | @Override |
| 204 | public boolean isChecked() { |
| 205 | return mIsChecked; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void setChecked(boolean checked) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 210 | if (mIsChecked != checked) { |
| 211 | mIsChecked = checked; |
| 212 | |
| 213 | if (mIsChecked) { |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 214 | // update the clipping rect to be used in the holographic pass below |
| 215 | getDrawingRect(mDrawableClipRect); |
| 216 | mDrawableClipRect.bottom = getPaddingTop() + getCompoundPaddingTop(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 217 | |
| 218 | // set a flag to indicate that we are going to draw the view at full alpha with the text |
| 219 | // clipped for the generation of the holographic icon |
| 220 | mIsHolographicUpdatePass = true; |
| 221 | mCheckedOutline = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), |
| 222 | Bitmap.Config.ARGB_8888); |
| 223 | mHolographicOutlineCanvas = new Canvas(mCheckedOutline); |
| 224 | mHolographicOutlineCanvas.concat(getMatrix()); |
| 225 | draw(mHolographicOutlineCanvas); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 226 | sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(mCheckedOutline, |
| 227 | mHolographicOutlineCanvas, mCheckedBlurColor, mCheckedOutlineColor); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 228 | mIsHolographicUpdatePass = false; |
| 229 | mHolographicOutlineCanvas = null; |
| 230 | } else { |
| 231 | invalidateCheckedImage(); |
| 232 | } |
| 233 | |
| 234 | invalidate(); |
| 235 | } |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void toggle() { |
| 240 | setChecked(!mIsChecked); |
| 241 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 242 | } |