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; |
| 57 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 58 | private int mAlpha; |
| 59 | private int mHolographicAlpha; |
| 60 | |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 61 | private boolean mIsChecked; |
| 62 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 63 | // Highlight colours |
| 64 | private int mHoloBlurColor; |
| 65 | private int mHoloOutlineColor; |
| 66 | private int mCheckedBlurColor; |
| 67 | private int mCheckedOutlineColor; |
| 68 | |
| 69 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 70 | public PagedViewIcon(Context context) { |
| 71 | this(context, null); |
| 72 | } |
| 73 | |
| 74 | public PagedViewIcon(Context context, AttributeSet attrs) { |
| 75 | this(context, attrs, 0); |
| 76 | } |
| 77 | |
| 78 | public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) { |
| 79 | super(context, attrs, defStyle); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 80 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0); |
| 81 | mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0); |
| 82 | mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0); |
| 83 | mCheckedBlurColor = a.getColor(R.styleable.PagedViewIcon_checkedBlurColor, 0); |
| 84 | mCheckedOutlineColor = a.getColor(R.styleable.PagedViewIcon_checkedOutlineColor, 0); |
| 85 | a.recycle(); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 86 | |
| 87 | if (sHolographicOutlineHelper == null) { |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 88 | sHolographicOutlineHelper = new HolographicOutlineHelper(); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 89 | } |
| 90 | mDrawableClipRect = new Rect(); |
| 91 | |
| 92 | setFocusable(true); |
| 93 | setBackgroundDrawable(null); |
| 94 | } |
| 95 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 96 | public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache) { |
| 97 | mIconCache = cache; |
| 98 | mIconCacheKey = info; |
| 99 | mHolographicOutline = mIconCache.getOutline(mIconCacheKey); |
| 100 | |
| 101 | setCompoundDrawablesWithIntrinsicBounds(null, |
| 102 | new FastBitmapDrawable(info.iconBitmap), null, null); |
| 103 | setText(info.title); |
| 104 | setTag(info); |
| 105 | } |
| 106 | |
| 107 | public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager, |
| 108 | PagedViewIconCache cache) { |
| 109 | mIconCache = cache; |
| 110 | mIconCacheKey = info; |
| 111 | mHolographicOutline = mIconCache.getOutline(mIconCacheKey); |
| 112 | |
Winson Chung | c1f48b9 | 2010-09-30 12:07:45 -0700 | [diff] [blame] | 113 | Bitmap image = Utilities.createIconBitmap(info.loadIcon(packageManager), mContext); |
| 114 | setCompoundDrawablesWithIntrinsicBounds(null, |
| 115 | new FastBitmapDrawable(image), null, null); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 116 | setText(info.loadLabel(packageManager)); |
| 117 | setTag(info); |
| 118 | } |
| 119 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 120 | @Override |
| 121 | public void setAlpha(float alpha) { |
| 122 | final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 123 | final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha); |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 124 | mAlpha = (int) (viewAlpha * 255); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 125 | mHolographicAlpha = (int) (holographicAlpha * 255); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 126 | super.setAlpha(viewAlpha); |
| 127 | } |
| 128 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 129 | public void invalidateCheckedImage() { |
| 130 | if (mCheckedOutline != null) { |
| 131 | mCheckedOutline.recycle(); |
| 132 | mCheckedOutline = null; |
| 133 | } |
| 134 | } |
| 135 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 136 | @Override |
| 137 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 138 | super.onLayout(changed, left, top, right, bottom); |
| 139 | |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 140 | if (mIconCache != null && mHolographicOutline == null) { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 141 | // update the clipping rect to be used in the holographic pass below |
| 142 | getDrawingRect(mDrawableClipRect); |
| 143 | mDrawableClipRect.bottom = getPaddingTop() + getCompoundPaddingTop(); |
| 144 | |
| 145 | // set a flag to indicate that we are going to draw the view at full alpha with the text |
| 146 | // clipped for the generation of the holographic icon |
| 147 | mIsHolographicUpdatePass = true; |
| 148 | mHolographicOutline = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), |
| 149 | Bitmap.Config.ARGB_8888); |
| 150 | mHolographicOutlineCanvas = new Canvas(mHolographicOutline); |
| 151 | mHolographicOutlineCanvas.concat(getMatrix()); |
| 152 | draw(mHolographicOutlineCanvas); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 153 | sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(mHolographicOutline, |
| 154 | mHolographicOutlineCanvas, mHoloBlurColor, mHoloOutlineColor); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 155 | mIsHolographicUpdatePass = false; |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 156 | mIconCache.addOutline(mIconCacheKey, mHolographicOutline); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 157 | mHolographicOutlineCanvas = null; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 161 | @Override |
| 162 | protected void onDraw(Canvas canvas) { |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 163 | // draw the view itself |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 164 | if (mIsHolographicUpdatePass) { |
| 165 | // only clip to the text view (restore its alpha so that we get a proper outline) |
| 166 | canvas.save(); |
| 167 | canvas.clipRect(mDrawableClipRect, Op.REPLACE); |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 168 | final float alpha = getAlpha(); |
| 169 | super.setAlpha(1.0f); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 170 | super.onDraw(canvas); |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 171 | super.setAlpha(alpha); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 172 | canvas.restore(); |
| 173 | } else { |
| 174 | if (mAlpha > 0) { |
| 175 | super.onDraw(canvas); |
| 176 | } |
| 177 | } |
| 178 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 179 | // draw any blended overlays |
| 180 | if (!mIsHolographicUpdatePass) { |
| 181 | if (mCheckedOutline == null) { |
| 182 | if (mHolographicOutline != null && mHolographicAlpha > 0) { |
| 183 | mPaint.setAlpha(mHolographicAlpha); |
| 184 | canvas.drawBitmap(mHolographicOutline, 0, 0, mPaint); |
| 185 | } |
| 186 | } else { |
| 187 | mPaint.setAlpha(255); |
| 188 | canvas.drawBitmap(mCheckedOutline, 0, 0, mPaint); |
| 189 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 192 | |
| 193 | @Override |
| 194 | public boolean isChecked() { |
| 195 | return mIsChecked; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void setChecked(boolean checked) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 200 | if (mIsChecked != checked) { |
| 201 | mIsChecked = checked; |
| 202 | |
| 203 | if (mIsChecked) { |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 204 | // update the clipping rect to be used in the holographic pass below |
| 205 | getDrawingRect(mDrawableClipRect); |
| 206 | mDrawableClipRect.bottom = getPaddingTop() + getCompoundPaddingTop(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 207 | |
| 208 | // set a flag to indicate that we are going to draw the view at full alpha with the text |
| 209 | // clipped for the generation of the holographic icon |
| 210 | mIsHolographicUpdatePass = true; |
| 211 | mCheckedOutline = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), |
| 212 | Bitmap.Config.ARGB_8888); |
| 213 | mHolographicOutlineCanvas = new Canvas(mCheckedOutline); |
| 214 | mHolographicOutlineCanvas.concat(getMatrix()); |
| 215 | draw(mHolographicOutlineCanvas); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 216 | sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(mCheckedOutline, |
| 217 | mHolographicOutlineCanvas, mCheckedBlurColor, mCheckedOutlineColor); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 218 | mIsHolographicUpdatePass = false; |
| 219 | mHolographicOutlineCanvas = null; |
| 220 | } else { |
| 221 | invalidateCheckedImage(); |
| 222 | } |
| 223 | |
| 224 | invalidate(); |
| 225 | } |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void toggle() { |
| 230 | setChecked(!mIsChecked); |
| 231 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 232 | } |