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