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