blob: 702e227d537a4f64d7eee988c6ea4f0f6519f8c3 [file] [log] [blame]
Winson Chungb3347bb2010-08-19 14:51:28 -07001/*
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
17package com.android.launcher2;
18
Winson Chungcd4bc492010-12-09 18:52:32 -080019import android.animation.ObjectAnimator;
Winson Chungb3347bb2010-08-19 14:51:28 -070020import android.content.Context;
Winson Chung241c3b42010-08-25 16:53:03 -070021import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Winson Chung59e1f9a2010-12-21 11:31:54 -080023import android.content.res.Resources;
Winson Chung64a3cd42010-09-17 16:47:33 -070024import android.content.res.TypedArray;
Winson Chungb3347bb2010-08-19 14:51:28 -070025import android.graphics.Bitmap;
Winson Chungb3347bb2010-08-19 14:51:28 -070026import android.graphics.Canvas;
Winson Chungb3347bb2010-08-19 14:51:28 -070027import android.graphics.Paint;
Michael Jurka0620bec2010-10-25 17:50:09 -070028import android.os.Handler;
29import android.os.HandlerThread;
30import android.os.Message;
Winson Chungb3347bb2010-08-19 14:51:28 -070031import android.util.AttributeSet;
Winson Chung03c568e2010-08-19 17:16:59 -070032import android.widget.Checkable;
Winson Chungb3347bb2010-08-19 14:51:28 -070033
Winson Chungc84001e2010-11-09 12:20:57 -080034import com.android.launcher.R;
Winson Chungc84001e2010-11-09 12:20:57 -080035
Winson Chung241c3b42010-08-25 16:53:03 -070036
Winson Chungb3347bb2010-08-19 14:51:28 -070037
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 Jurka002bd922011-01-14 15:50:07 -080042public class PagedViewIcon extends CachedTextView implements Checkable {
Winson Chungb3347bb2010-08-19 14:51:28 -070043 private static final String TAG = "PagedViewIcon";
44
45 // holographic outline
46 private final Paint mPaint = new Paint();
47 private static HolographicOutlineHelper sHolographicOutlineHelper;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070048 private Bitmap mCheckedOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -070049 private Bitmap mHolographicOutline;
Michael Jurka0620bec2010-10-25 17:50:09 -070050 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070051
Winson Chung04998342011-01-05 13:54:43 -080052 private PagedViewIconCache.Key mIconCacheKey;
Winson Chung241c3b42010-08-25 16:53:03 -070053 private PagedViewIconCache mIconCache;
54
Winson Chung88127032010-12-13 12:11:33 -080055 private int mAlpha = 255;
Winson Chungb3347bb2010-08-19 14:51:28 -070056 private int mHolographicAlpha;
57
Winson Chung03c568e2010-08-19 17:16:59 -070058 private boolean mIsChecked;
Winson Chungcd4bc492010-12-09 18:52:32 -080059 private ObjectAnimator mCheckedAlphaAnimator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080060 private float mCheckedAlpha = 1.0f;
61 private int mCheckedFadeInDuration;
62 private int mCheckedFadeOutDuration;
Winson Chung03c568e2010-08-19 17:16:59 -070063
Michael Jurkac9a96192010-11-01 11:52:08 -070064 // Highlight colors
Winson Chung64a3cd42010-09-17 16:47:33 -070065 private int mHoloBlurColor;
66 private int mHoloOutlineColor;
Winson Chung64a3cd42010-09-17 16:47:33 -070067
Michael Jurka8245a862011-02-01 17:53:59 -080068 HolographicPagedViewIcon mHolographicOutlineView;
69
Michael Jurka0620bec2010-10-25 17:50:09 -070070 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 Cohen5bb50bd2010-12-03 11:39:55 -080088 sHolographicOutlineHelper.applyThickExpensiveOutlineWithBlur(holographicOutline,
Michael Jurka0620bec2010-10-25 17:50:09 -070089 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);
Michael Jurka8245a862011-02-01 17:53:59 -080095 icon.getHolographicOutlineView().invalidate();
Michael Jurka0620bec2010-10-25 17:50:09 -070096 }
97 });
98 }
99 };
Winson Chung64a3cd42010-09-17 16:47:33 -0700100
Winson Chungb3347bb2010-08-19 14:51:28 -0700101 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 Chung29d6fea2010-12-01 15:47:31 -0800111
Winson Chung64a3cd42010-09-17 16:47:33 -0700112 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0);
113 mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0);
114 mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0);
Winson Chung64a3cd42010-09-17 16:47:33 -0700115 a.recycle();
Winson Chungb3347bb2010-08-19 14:51:28 -0700116
117 if (sHolographicOutlineHelper == null) {
Winson Chung64a3cd42010-09-17 16:47:33 -0700118 sHolographicOutlineHelper = new HolographicOutlineHelper();
Winson Chungb3347bb2010-08-19 14:51:28 -0700119 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700120
Winson Chung59e1f9a2010-12-21 11:31:54 -0800121 // Set up fade in/out constants
122 final Resources r = context.getResources();
123 final int alpha = r.getInteger(R.integer.icon_allAppsCustomizeFadeAlpha);
124 if (alpha > 0) {
125 mCheckedAlpha = r.getInteger(R.integer.icon_allAppsCustomizeFadeAlpha) / 256.0f;
126 mCheckedFadeInDuration = r.getInteger(R.integer.icon_allAppsCustomizeFadeInTime);
127 mCheckedFadeOutDuration = r.getInteger(R.integer.icon_allAppsCustomizeFadeOutTime);
128 }
129
Winson Chungb3347bb2010-08-19 14:51:28 -0700130 setFocusable(true);
131 setBackgroundDrawable(null);
Michael Jurka8245a862011-02-01 17:53:59 -0800132 mHolographicOutlineView = new HolographicPagedViewIcon(context, this);
133 }
134
135 protected HolographicPagedViewIcon getHolographicOutlineView() {
136 return mHolographicOutlineView;
137 }
138
139 protected Bitmap getHolographicOutline() {
140 return mHolographicOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -0700141 }
142
Michael Jurka0620bec2010-10-25 17:50:09 -0700143 private void queueHolographicOutlineCreation() {
144 // Generate the outline in the background
145 if (mHolographicOutline == null) {
146 Message m = sWorker.obtainMessage(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE);
147 m.obj = this;
148 sWorker.sendMessage(m);
149 }
150 }
151
Winson Chungdf4b83d2010-10-20 17:49:27 -0700152 public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache,
Winson Chung04998342011-01-05 13:54:43 -0800153 boolean scaleUp, boolean createHolographicOutlines) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700154 mIcon = info.iconBitmap;
Michael Jurka0620bec2010-10-25 17:50:09 -0700155 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700156 setText(info.title);
157 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700158
Winson Chung04998342011-01-05 13:54:43 -0800159 if (createHolographicOutlines) {
160 mIconCache = cache;
161 mIconCacheKey = new PagedViewIconCache.Key(info);
162 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
163 queueHolographicOutlineCreation();
164 }
Winson Chung241c3b42010-08-25 16:53:03 -0700165 }
166
167 public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
Winson Chung04998342011-01-05 13:54:43 -0800168 PagedViewIconCache cache, IconCache modelIconCache, boolean createHolographicOutlines) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700169 mIcon = Utilities.createIconBitmap(
170 modelIconCache.getFullResIcon(info, packageManager), mContext);
Michael Jurka0620bec2010-10-25 17:50:09 -0700171 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700172 setText(info.loadLabel(packageManager));
173 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700174
Winson Chung04998342011-01-05 13:54:43 -0800175 if (createHolographicOutlines) {
176 mIconCache = cache;
177 mIconCacheKey = new PagedViewIconCache.Key(info);
178 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
179 queueHolographicOutlineCreation();
180 }
Winson Chung241c3b42010-08-25 16:53:03 -0700181 }
182
Winson Chungb3347bb2010-08-19 14:51:28 -0700183 @Override
184 public void setAlpha(float alpha) {
185 final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700186 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chunge22a8e92010-11-12 13:40:58 -0800187 int newViewAlpha = (int) (viewAlpha * 255);
188 int newHolographicAlpha = (int) (holographicAlpha * 255);
189 if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
190 mAlpha = newViewAlpha;
191 mHolographicAlpha = newHolographicAlpha;
192 super.setAlpha(viewAlpha);
193 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700194 }
195
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700196 public void invalidateCheckedImage() {
197 if (mCheckedOutline != null) {
198 mCheckedOutline.recycle();
199 mCheckedOutline = null;
200 }
201 }
202
Winson Chungb3347bb2010-08-19 14:51:28 -0700203 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700204 protected void onDraw(Canvas canvas) {
205 if (mAlpha > 0) {
206 super.onDraw(canvas);
207 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700208
Michael Jurka0620bec2010-10-25 17:50:09 -0700209 Bitmap overlay = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700210
Michael Jurka0620bec2010-10-25 17:50:09 -0700211 // draw any blended overlays
212 if (mCheckedOutline == null) {
213 if (mHolographicOutline != null && mHolographicAlpha > 0) {
214 mPaint.setAlpha(mHolographicAlpha);
215 overlay = mHolographicOutline;
216 }
217 } else {
218 mPaint.setAlpha(255);
219 overlay = mCheckedOutline;
220 }
221
222 if (overlay != null) {
Winson Chungc84001e2010-11-09 12:20:57 -0800223 final int offset = getScrollX();
Michael Jurka0620bec2010-10-25 17:50:09 -0700224 final int compoundPaddingLeft = getCompoundPaddingLeft();
225 final int compoundPaddingRight = getCompoundPaddingRight();
226 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
227 canvas.drawBitmap(overlay,
Winson Chungc84001e2010-11-09 12:20:57 -0800228 offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
Michael Jurka0620bec2010-10-25 17:50:09 -0700229 mPaddingTop,
230 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700231 }
232 }
233
Winson Chungb3347bb2010-08-19 14:51:28 -0700234 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700235 public void onDetachedFromWindow() {
236 super.onDetachedFromWindow();
237 sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this);
Winson Chungb3347bb2010-08-19 14:51:28 -0700238 }
Winson Chung03c568e2010-08-19 17:16:59 -0700239
240 @Override
241 public boolean isChecked() {
242 return mIsChecked;
243 }
244
245 @Override
246 public void setChecked(boolean checked) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700247 if (mIsChecked != checked) {
248 mIsChecked = checked;
249
Winson Chungcd4bc492010-12-09 18:52:32 -0800250 float alpha;
251 int duration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700252 if (mIsChecked) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800253 alpha = mCheckedAlpha;
254 duration = mCheckedFadeInDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700255 } else {
Winson Chungcd4bc492010-12-09 18:52:32 -0800256 alpha = 1.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800257 duration = mCheckedFadeOutDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700258 }
259
Winson Chungcd4bc492010-12-09 18:52:32 -0800260 // Initialize the animator
261 if (mCheckedAlphaAnimator != null) {
262 mCheckedAlphaAnimator.cancel();
263 }
Winson Chung88127032010-12-13 12:11:33 -0800264 mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha);
Winson Chungcd4bc492010-12-09 18:52:32 -0800265 mCheckedAlphaAnimator.setDuration(duration);
266 mCheckedAlphaAnimator.start();
267
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700268 invalidate();
269 }
Winson Chung03c568e2010-08-19 17:16:59 -0700270 }
271
272 @Override
273 public void toggle() {
274 setChecked(!mIsChecked);
275 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700276}