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; |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
| 27 | import android.os.HandlerThread; |
| 28 | import android.os.Message; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 30 | import android.widget.Checkable; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 31 | import android.widget.TextView; |
| 32 | |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 33 | import com.android.launcher.R; |
| 34 | import com.android.launcher2.PagedView.PagedViewIconCache; |
| 35 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 36 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * An icon on a PagedView, specifically for items in the launcher's paged view (with compound |
| 40 | * drawables on the top). |
| 41 | */ |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 42 | public class PagedViewIcon extends CacheableTextView implements Checkable { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 43 | private static final String TAG = "PagedViewIcon"; |
| 44 | |
| 45 | // holographic outline |
| 46 | private final Paint mPaint = new Paint(); |
| 47 | private static HolographicOutlineHelper sHolographicOutlineHelper; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 48 | private Bitmap mCheckedOutline; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 49 | private Bitmap mHolographicOutline; |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 50 | private Bitmap mIcon; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 51 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 52 | private Object mIconCacheKey; |
| 53 | private PagedViewIconCache mIconCache; |
| 54 | |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 55 | private int mAlpha = -1; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 56 | private int mHolographicAlpha; |
| 57 | |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 58 | private boolean mIsChecked; |
| 59 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 60 | // Highlight colors |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 61 | private int mHoloBlurColor; |
| 62 | private int mHoloOutlineColor; |
| 63 | private int mCheckedBlurColor; |
| 64 | private int mCheckedOutlineColor; |
| 65 | |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 66 | private static final HandlerThread sWorkerThread = new HandlerThread("pagedviewicon-helper"); |
| 67 | static { |
| 68 | sWorkerThread.start(); |
| 69 | } |
| 70 | |
| 71 | private static final int MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE = 1; |
| 72 | |
| 73 | private static final Handler sWorker = new Handler(sWorkerThread.getLooper()) { |
| 74 | private DeferredHandler mHandler = new DeferredHandler(); |
| 75 | private Paint mPaint = new Paint(); |
| 76 | public void handleMessage(Message msg) { |
| 77 | final PagedViewIcon icon = (PagedViewIcon) msg.obj; |
| 78 | |
| 79 | final Bitmap holographicOutline = Bitmap.createBitmap( |
| 80 | icon.mIcon.getWidth(), icon.mIcon.getHeight(), Bitmap.Config.ARGB_8888); |
| 81 | Canvas holographicOutlineCanvas = new Canvas(holographicOutline); |
| 82 | holographicOutlineCanvas.drawBitmap(icon.mIcon, 0, 0, mPaint); |
| 83 | |
| 84 | sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(holographicOutline, |
| 85 | holographicOutlineCanvas, icon.mHoloBlurColor, icon.mHoloOutlineColor); |
| 86 | |
| 87 | mHandler.post(new Runnable() { |
| 88 | public void run() { |
| 89 | icon.mHolographicOutline = holographicOutline; |
| 90 | icon.mIconCache.addOutline(icon.mIconCacheKey, holographicOutline); |
| 91 | icon.invalidate(); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | }; |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 96 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 97 | public PagedViewIcon(Context context) { |
| 98 | this(context, null); |
| 99 | } |
| 100 | |
| 101 | public PagedViewIcon(Context context, AttributeSet attrs) { |
| 102 | this(context, attrs, 0); |
| 103 | } |
| 104 | |
| 105 | public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) { |
| 106 | super(context, attrs, defStyle); |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 107 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0); |
| 108 | mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0); |
| 109 | mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0); |
| 110 | mCheckedBlurColor = a.getColor(R.styleable.PagedViewIcon_checkedBlurColor, 0); |
| 111 | mCheckedOutlineColor = a.getColor(R.styleable.PagedViewIcon_checkedOutlineColor, 0); |
Winson Chung | 0e41ae6 | 2010-10-27 18:10:32 -0700 | [diff] [blame] | 112 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 113 | a.recycle(); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 114 | |
| 115 | if (sHolographicOutlineHelper == null) { |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 116 | sHolographicOutlineHelper = new HolographicOutlineHelper(); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 117 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 118 | |
| 119 | setFocusable(true); |
| 120 | setBackgroundDrawable(null); |
| 121 | } |
| 122 | |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 123 | private void queueHolographicOutlineCreation() { |
| 124 | // Generate the outline in the background |
| 125 | if (mHolographicOutline == null) { |
| 126 | Message m = sWorker.obtainMessage(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE); |
| 127 | m.obj = this; |
| 128 | sWorker.sendMessage(m); |
| 129 | } |
| 130 | } |
| 131 | |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 132 | public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache, |
| 133 | boolean scaleUp) { |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 134 | mIconCache = cache; |
| 135 | mIconCacheKey = info; |
| 136 | mHolographicOutline = mIconCache.getOutline(mIconCacheKey); |
| 137 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 138 | mIcon = info.iconBitmap; |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 139 | setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 140 | setText(info.title); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 141 | buildAndEnableCache(); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 142 | setTag(info); |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 143 | |
| 144 | queueHolographicOutlineCreation(); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager, |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 148 | PagedViewIconCache cache, IconCache modelIconCache) { |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 149 | mIconCache = cache; |
| 150 | mIconCacheKey = info; |
| 151 | mHolographicOutline = mIconCache.getOutline(mIconCacheKey); |
| 152 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 153 | mIcon = Utilities.createIconBitmap( |
| 154 | modelIconCache.getFullResIcon(info, packageManager), mContext); |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 155 | setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 156 | setText(info.loadLabel(packageManager)); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 157 | buildAndEnableCache(); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 158 | setTag(info); |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 159 | |
| 160 | queueHolographicOutlineCreation(); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 163 | @Override |
| 164 | public void setAlpha(float alpha) { |
| 165 | final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 166 | final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha); |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 167 | int newViewAlpha = (int) (viewAlpha * 255); |
| 168 | int newHolographicAlpha = (int) (holographicAlpha * 255); |
| 169 | if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) { |
| 170 | mAlpha = newViewAlpha; |
| 171 | mHolographicAlpha = newHolographicAlpha; |
| 172 | super.setAlpha(viewAlpha); |
| 173 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 176 | public void invalidateCheckedImage() { |
| 177 | if (mCheckedOutline != null) { |
| 178 | mCheckedOutline.recycle(); |
| 179 | mCheckedOutline = null; |
| 180 | } |
| 181 | } |
| 182 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 183 | @Override |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 184 | protected void onDraw(Canvas canvas) { |
| 185 | if (mAlpha > 0) { |
| 186 | super.onDraw(canvas); |
| 187 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 188 | |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 189 | Bitmap overlay = null; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 190 | |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 191 | // draw any blended overlays |
| 192 | if (mCheckedOutline == null) { |
| 193 | if (mHolographicOutline != null && mHolographicAlpha > 0) { |
| 194 | mPaint.setAlpha(mHolographicAlpha); |
| 195 | overlay = mHolographicOutline; |
| 196 | } |
| 197 | } else { |
| 198 | mPaint.setAlpha(255); |
| 199 | overlay = mCheckedOutline; |
| 200 | } |
| 201 | |
| 202 | if (overlay != null) { |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 203 | final int offset = getScrollX(); |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 204 | final int compoundPaddingLeft = getCompoundPaddingLeft(); |
| 205 | final int compoundPaddingRight = getCompoundPaddingRight(); |
| 206 | int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft; |
| 207 | canvas.drawBitmap(overlay, |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 208 | offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2, |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 209 | mPaddingTop, |
| 210 | mPaint); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 214 | @Override |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 215 | public void onDetachedFromWindow() { |
| 216 | super.onDetachedFromWindow(); |
| 217 | sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 218 | } |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 219 | |
| 220 | @Override |
| 221 | public boolean isChecked() { |
| 222 | return mIsChecked; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public void setChecked(boolean checked) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 227 | if (mIsChecked != checked) { |
| 228 | mIsChecked = checked; |
| 229 | |
| 230 | if (mIsChecked) { |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 231 | mCheckedOutline = Bitmap.createBitmap(mIcon.getWidth(), mIcon.getHeight(), |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 232 | Bitmap.Config.ARGB_8888); |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 233 | Canvas checkedOutlineCanvas = new Canvas(mCheckedOutline); |
| 234 | mPaint.setAlpha(255); |
| 235 | checkedOutlineCanvas.drawBitmap(mIcon, 0, 0, mPaint); |
| 236 | |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 237 | sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(mCheckedOutline, |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 238 | checkedOutlineCanvas, mCheckedBlurColor, mCheckedOutlineColor); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 239 | } else { |
| 240 | invalidateCheckedImage(); |
| 241 | } |
| 242 | |
| 243 | invalidate(); |
| 244 | } |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void toggle() { |
| 249 | setChecked(!mIsChecked); |
| 250 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 251 | } |