blob: 306ff9e7b83a22bb6ac767cf9b30791c17fdc8e4 [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 Chung4dbea792011-05-05 14:21:32 -070020import android.content.ComponentName;
Winson Chungb3347bb2010-08-19 14:51:28 -070021import android.content.Context;
Winson Chung241c3b42010-08-25 16:53:03 -070022import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
Winson Chung59e1f9a2010-12-21 11:31:54 -080024import android.content.res.Resources;
Winson Chung64a3cd42010-09-17 16:47:33 -070025import android.content.res.TypedArray;
Winson Chungb3347bb2010-08-19 14:51:28 -070026import android.graphics.Bitmap;
Winson Chungb3347bb2010-08-19 14:51:28 -070027import android.graphics.Canvas;
Winson Chungb3347bb2010-08-19 14:51:28 -070028import android.graphics.Paint;
Michael Jurka0620bec2010-10-25 17:50:09 -070029import android.os.Handler;
30import android.os.HandlerThread;
31import android.os.Message;
Winson Chungb3347bb2010-08-19 14:51:28 -070032import android.util.AttributeSet;
Winson Chung97d85d22011-04-13 11:27:36 -070033import android.view.KeyEvent;
Winson Chung63257c12011-05-05 17:06:13 -070034import android.view.View;
Winson Chung03c568e2010-08-19 17:16:59 -070035import android.widget.Checkable;
Winson Chungc84001e2010-11-09 12:20:57 -080036
Winson Chung63257c12011-05-05 17:06:13 -070037import com.android.launcher.R;
Winson Chung241c3b42010-08-25 16:53:03 -070038
Winson Chungb3347bb2010-08-19 14:51:28 -070039
40/**
41 * An icon on a PagedView, specifically for items in the launcher's paged view (with compound
42 * drawables on the top).
43 */
Michael Jurkac0759f52011-02-03 16:47:14 -080044public class PagedViewIcon extends CachedTextView implements Checkable {
Winson Chungb3347bb2010-08-19 14:51:28 -070045 private static final String TAG = "PagedViewIcon";
46
47 // holographic outline
48 private final Paint mPaint = new Paint();
49 private static HolographicOutlineHelper sHolographicOutlineHelper;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070050 private Bitmap mCheckedOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -070051 private Bitmap mHolographicOutline;
Michael Jurka0620bec2010-10-25 17:50:09 -070052 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070053
Winson Chung04998342011-01-05 13:54:43 -080054 private PagedViewIconCache.Key mIconCacheKey;
Winson Chung241c3b42010-08-25 16:53:03 -070055 private PagedViewIconCache mIconCache;
56
Winson Chung88127032010-12-13 12:11:33 -080057 private int mAlpha = 255;
Winson Chungb3347bb2010-08-19 14:51:28 -070058 private int mHolographicAlpha;
59
Winson Chung03c568e2010-08-19 17:16:59 -070060 private boolean mIsChecked;
Winson Chungcd4bc492010-12-09 18:52:32 -080061 private ObjectAnimator mCheckedAlphaAnimator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080062 private float mCheckedAlpha = 1.0f;
63 private int mCheckedFadeInDuration;
64 private int mCheckedFadeOutDuration;
Winson Chung03c568e2010-08-19 17:16:59 -070065
Michael Jurkac9a96192010-11-01 11:52:08 -070066 // Highlight colors
Winson Chung64a3cd42010-09-17 16:47:33 -070067 private int mHoloBlurColor;
68 private int mHoloOutlineColor;
Winson Chung64a3cd42010-09-17 16:47:33 -070069
Michael Jurka8245a862011-02-01 17:53:59 -080070 HolographicPagedViewIcon mHolographicOutlineView;
71
Michael Jurka0620bec2010-10-25 17:50:09 -070072 private static final HandlerThread sWorkerThread = new HandlerThread("pagedviewicon-helper");
73 static {
74 sWorkerThread.start();
75 }
76
77 private static final int MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE = 1;
78
79 private static final Handler sWorker = new Handler(sWorkerThread.getLooper()) {
80 private DeferredHandler mHandler = new DeferredHandler();
81 private Paint mPaint = new Paint();
82 public void handleMessage(Message msg) {
83 final PagedViewIcon icon = (PagedViewIcon) msg.obj;
84
85 final Bitmap holographicOutline = Bitmap.createBitmap(
86 icon.mIcon.getWidth(), icon.mIcon.getHeight(), Bitmap.Config.ARGB_8888);
87 Canvas holographicOutlineCanvas = new Canvas(holographicOutline);
88 holographicOutlineCanvas.drawBitmap(icon.mIcon, 0, 0, mPaint);
89
Adam Cohen5bb50bd2010-12-03 11:39:55 -080090 sHolographicOutlineHelper.applyThickExpensiveOutlineWithBlur(holographicOutline,
Michael Jurka0620bec2010-10-25 17:50:09 -070091 holographicOutlineCanvas, icon.mHoloBlurColor, icon.mHoloOutlineColor);
92
93 mHandler.post(new Runnable() {
94 public void run() {
95 icon.mHolographicOutline = holographicOutline;
96 icon.mIconCache.addOutline(icon.mIconCacheKey, holographicOutline);
Michael Jurka8245a862011-02-01 17:53:59 -080097 icon.getHolographicOutlineView().invalidate();
Michael Jurka0620bec2010-10-25 17:50:09 -070098 }
99 });
100 }
101 };
Winson Chung64a3cd42010-09-17 16:47:33 -0700102
Winson Chungb3347bb2010-08-19 14:51:28 -0700103 public PagedViewIcon(Context context) {
104 this(context, null);
105 }
106
107 public PagedViewIcon(Context context, AttributeSet attrs) {
108 this(context, attrs, 0);
109 }
110
111 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
112 super(context, attrs, defStyle);
Winson Chung29d6fea2010-12-01 15:47:31 -0800113
Winson Chung64a3cd42010-09-17 16:47:33 -0700114 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0);
115 mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0);
116 mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0);
Winson Chung64a3cd42010-09-17 16:47:33 -0700117 a.recycle();
Winson Chungb3347bb2010-08-19 14:51:28 -0700118
119 if (sHolographicOutlineHelper == null) {
Winson Chung64a3cd42010-09-17 16:47:33 -0700120 sHolographicOutlineHelper = new HolographicOutlineHelper();
Winson Chungb3347bb2010-08-19 14:51:28 -0700121 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700122
Winson Chung59e1f9a2010-12-21 11:31:54 -0800123 // Set up fade in/out constants
124 final Resources r = context.getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -0700125 final int alpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha);
Winson Chung59e1f9a2010-12-21 11:31:54 -0800126 if (alpha > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700127 mCheckedAlpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha) / 256.0f;
128 mCheckedFadeInDuration =
129 r.getInteger(R.integer.config_dragAppsCustomizeIconFadeInDuration);
130 mCheckedFadeOutDuration =
131 r.getInteger(R.integer.config_dragAppsCustomizeIconFadeOutDuration);
Winson Chung59e1f9a2010-12-21 11:31:54 -0800132 }
133
Michael Jurka8245a862011-02-01 17:53:59 -0800134 mHolographicOutlineView = new HolographicPagedViewIcon(context, this);
135 }
136
137 protected HolographicPagedViewIcon getHolographicOutlineView() {
138 return mHolographicOutlineView;
139 }
140
141 protected Bitmap getHolographicOutline() {
142 return mHolographicOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -0700143 }
144
Winson Chung823c9692011-03-01 10:57:29 -0800145 private boolean queueHolographicOutlineCreation() {
Michael Jurka0620bec2010-10-25 17:50:09 -0700146 // Generate the outline in the background
147 if (mHolographicOutline == null) {
148 Message m = sWorker.obtainMessage(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE);
149 m.obj = this;
150 sWorker.sendMessage(m);
Winson Chung823c9692011-03-01 10:57:29 -0800151 return true;
Michael Jurka0620bec2010-10-25 17:50:09 -0700152 }
Winson Chung823c9692011-03-01 10:57:29 -0800153 return false;
Michael Jurka0620bec2010-10-25 17:50:09 -0700154 }
155
Winson Chung6a70e9f2011-05-17 16:24:49 -0700156 public void loadHolographicIcon() {
157 if (mHolographicOutline == null) {
158 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
159 if (!queueHolographicOutlineCreation()) {
160 getHolographicOutlineView().invalidate();
161 }
162 }
163 }
164 public void clearHolographicIcon() {
165 mHolographicOutline = null;
166 getHolographicOutlineView().invalidate();
167 }
168
Winson Chungdf4b83d2010-10-20 17:49:27 -0700169 public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache,
Winson Chung04998342011-01-05 13:54:43 -0800170 boolean scaleUp, boolean createHolographicOutlines) {
Winson Chung6a70e9f2011-05-17 16:24:49 -0700171 mIconCache = cache;
172 mIconCacheKey = new PagedViewIconCache.Key(info);
Michael Jurkac9a96192010-11-01 11:52:08 -0700173 mIcon = info.iconBitmap;
Michael Jurka0620bec2010-10-25 17:50:09 -0700174 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700175 setText(info.title);
176 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700177
Winson Chung04998342011-01-05 13:54:43 -0800178 if (createHolographicOutlines) {
Winson Chung04998342011-01-05 13:54:43 -0800179 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
Michael Jurkab9b8ce92011-05-05 15:05:07 -0700180 if (!queueHolographicOutlineCreation()) {
Winson Chung823c9692011-03-01 10:57:29 -0800181 getHolographicOutlineView().invalidate();
182 }
Winson Chung04998342011-01-05 13:54:43 -0800183 }
Winson Chung241c3b42010-08-25 16:53:03 -0700184 }
185
186 public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
Winson Chung04998342011-01-05 13:54:43 -0800187 PagedViewIconCache cache, IconCache modelIconCache, boolean createHolographicOutlines) {
Winson Chung4dbea792011-05-05 14:21:32 -0700188 ComponentName cn = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
189 mIcon = modelIconCache.getIcon(cn, info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700190 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700191 setText(info.loadLabel(packageManager));
192 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700193
Winson Chung04998342011-01-05 13:54:43 -0800194 if (createHolographicOutlines) {
195 mIconCache = cache;
196 mIconCacheKey = new PagedViewIconCache.Key(info);
197 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
Michael Jurkab9b8ce92011-05-05 15:05:07 -0700198 if (!queueHolographicOutlineCreation()) {
Winson Chung823c9692011-03-01 10:57:29 -0800199 getHolographicOutlineView().invalidate();
200 }
Winson Chung04998342011-01-05 13:54:43 -0800201 }
Winson Chung241c3b42010-08-25 16:53:03 -0700202 }
203
Winson Chungb3347bb2010-08-19 14:51:28 -0700204 @Override
205 public void setAlpha(float alpha) {
206 final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700207 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chunge22a8e92010-11-12 13:40:58 -0800208 int newViewAlpha = (int) (viewAlpha * 255);
209 int newHolographicAlpha = (int) (holographicAlpha * 255);
210 if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
211 mAlpha = newViewAlpha;
212 mHolographicAlpha = newHolographicAlpha;
213 super.setAlpha(viewAlpha);
214 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700215 }
216
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700217 public void invalidateCheckedImage() {
218 if (mCheckedOutline != null) {
219 mCheckedOutline.recycle();
220 mCheckedOutline = null;
221 }
222 }
223
Winson Chungb3347bb2010-08-19 14:51:28 -0700224 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700225 protected void onDraw(Canvas canvas) {
226 if (mAlpha > 0) {
227 super.onDraw(canvas);
228 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700229
Michael Jurka0620bec2010-10-25 17:50:09 -0700230 Bitmap overlay = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700231
Michael Jurka0620bec2010-10-25 17:50:09 -0700232 // draw any blended overlays
Winson Chung6a70e9f2011-05-17 16:24:49 -0700233 if (mCheckedOutline != null) {
Michael Jurka0620bec2010-10-25 17:50:09 -0700234 mPaint.setAlpha(255);
235 overlay = mCheckedOutline;
236 }
237
238 if (overlay != null) {
Winson Chungc84001e2010-11-09 12:20:57 -0800239 final int offset = getScrollX();
Michael Jurka0620bec2010-10-25 17:50:09 -0700240 final int compoundPaddingLeft = getCompoundPaddingLeft();
241 final int compoundPaddingRight = getCompoundPaddingRight();
242 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
243 canvas.drawBitmap(overlay,
Winson Chungc84001e2010-11-09 12:20:57 -0800244 offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
Michael Jurka0620bec2010-10-25 17:50:09 -0700245 mPaddingTop,
246 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700247 }
248 }
249
Winson Chungb3347bb2010-08-19 14:51:28 -0700250 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700251 public void onDetachedFromWindow() {
252 super.onDetachedFromWindow();
253 sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this);
Winson Chungb3347bb2010-08-19 14:51:28 -0700254 }
Winson Chung03c568e2010-08-19 17:16:59 -0700255
256 @Override
Winson Chung97d85d22011-04-13 11:27:36 -0700257 public boolean onKeyDown(int keyCode, KeyEvent event) {
258 return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event)
259 || super.onKeyDown(keyCode, event);
260 }
261
262 @Override
263 public boolean onKeyUp(int keyCode, KeyEvent event) {
264 return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event)
265 || super.onKeyUp(keyCode, event);
266 }
267
268 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700269 public boolean isChecked() {
270 return mIsChecked;
271 }
272
Patrick Dubroy5f445422011-02-18 14:35:21 -0800273 void setChecked(boolean checked, boolean animate) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700274 if (mIsChecked != checked) {
275 mIsChecked = checked;
276
Winson Chungcd4bc492010-12-09 18:52:32 -0800277 float alpha;
278 int duration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700279 if (mIsChecked) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800280 alpha = mCheckedAlpha;
281 duration = mCheckedFadeInDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700282 } else {
Winson Chungcd4bc492010-12-09 18:52:32 -0800283 alpha = 1.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800284 duration = mCheckedFadeOutDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700285 }
286
Winson Chungcd4bc492010-12-09 18:52:32 -0800287 // Initialize the animator
288 if (mCheckedAlphaAnimator != null) {
289 mCheckedAlphaAnimator.cancel();
290 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800291 if (animate) {
292 mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha);
293 mCheckedAlphaAnimator.setDuration(duration);
294 mCheckedAlphaAnimator.start();
295 } else {
296 setAlpha(alpha);
297 }
Winson Chungcd4bc492010-12-09 18:52:32 -0800298
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700299 invalidate();
300 }
Winson Chung03c568e2010-08-19 17:16:59 -0700301 }
302
303 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800304 public void setChecked(boolean checked) {
305 setChecked(checked, true);
306 }
307
308 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700309 public void toggle() {
310 setChecked(!mIsChecked);
311 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700312}