blob: 30f0b1db2fb4ae708e9a9dea0163036215a94d01 [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 Chung03c568e2010-08-19 17:16:59 -070034import android.widget.Checkable;
Winson Chungc84001e2010-11-09 12:20:57 -080035
Winson Chung63257c12011-05-05 17:06:13 -070036import com.android.launcher.R;
Winson Chung241c3b42010-08-25 16:53:03 -070037
Winson Chungb3347bb2010-08-19 14:51:28 -070038
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 Jurkac0759f52011-02-03 16:47:14 -080043public class PagedViewIcon extends CachedTextView implements Checkable {
Winson Chungb3347bb2010-08-19 14:51:28 -070044 private static final String TAG = "PagedViewIcon";
45
46 // holographic outline
47 private final Paint mPaint = new Paint();
48 private static HolographicOutlineHelper sHolographicOutlineHelper;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070049 private Bitmap mCheckedOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -070050 private Bitmap mHolographicOutline;
Michael Jurka0620bec2010-10-25 17:50:09 -070051 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070052
Winson Chung04998342011-01-05 13:54:43 -080053 private PagedViewIconCache.Key mIconCacheKey;
Winson Chung241c3b42010-08-25 16:53:03 -070054 private PagedViewIconCache mIconCache;
55
Winson Chung88127032010-12-13 12:11:33 -080056 private int mAlpha = 255;
Winson Chungb3347bb2010-08-19 14:51:28 -070057 private int mHolographicAlpha;
58
Winson Chung03c568e2010-08-19 17:16:59 -070059 private boolean mIsChecked;
Winson Chungcd4bc492010-12-09 18:52:32 -080060 private ObjectAnimator mCheckedAlphaAnimator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080061 private float mCheckedAlpha = 1.0f;
62 private int mCheckedFadeInDuration;
63 private int mCheckedFadeOutDuration;
Winson Chung03c568e2010-08-19 17:16:59 -070064
Michael Jurkac9a96192010-11-01 11:52:08 -070065 // Highlight colors
Winson Chung64a3cd42010-09-17 16:47:33 -070066 private int mHoloBlurColor;
67 private int mHoloOutlineColor;
Winson Chung64a3cd42010-09-17 16:47:33 -070068
Michael Jurka8245a862011-02-01 17:53:59 -080069 HolographicPagedViewIcon mHolographicOutlineView;
70
Michael Jurka0620bec2010-10-25 17:50:09 -070071 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 Cohen5bb50bd2010-12-03 11:39:55 -080089 sHolographicOutlineHelper.applyThickExpensiveOutlineWithBlur(holographicOutline,
Michael Jurka0620bec2010-10-25 17:50:09 -070090 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 Jurka8245a862011-02-01 17:53:59 -080096 icon.getHolographicOutlineView().invalidate();
Michael Jurka0620bec2010-10-25 17:50:09 -070097 }
98 });
99 }
100 };
Winson Chung64a3cd42010-09-17 16:47:33 -0700101
Winson Chungb3347bb2010-08-19 14:51:28 -0700102 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 Chung29d6fea2010-12-01 15:47:31 -0800112
Winson Chung64a3cd42010-09-17 16:47:33 -0700113 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 Chung64a3cd42010-09-17 16:47:33 -0700116 a.recycle();
Winson Chungb3347bb2010-08-19 14:51:28 -0700117
118 if (sHolographicOutlineHelper == null) {
Winson Chung64a3cd42010-09-17 16:47:33 -0700119 sHolographicOutlineHelper = new HolographicOutlineHelper();
Winson Chungb3347bb2010-08-19 14:51:28 -0700120 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700121
Winson Chung59e1f9a2010-12-21 11:31:54 -0800122 // Set up fade in/out constants
123 final Resources r = context.getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -0700124 final int alpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha);
Winson Chung59e1f9a2010-12-21 11:31:54 -0800125 if (alpha > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700126 mCheckedAlpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha) / 256.0f;
127 mCheckedFadeInDuration =
128 r.getInteger(R.integer.config_dragAppsCustomizeIconFadeInDuration);
129 mCheckedFadeOutDuration =
130 r.getInteger(R.integer.config_dragAppsCustomizeIconFadeOutDuration);
Winson Chung59e1f9a2010-12-21 11:31:54 -0800131 }
132
Michael Jurka8245a862011-02-01 17:53:59 -0800133 mHolographicOutlineView = new HolographicPagedViewIcon(context, this);
134 }
135
136 protected HolographicPagedViewIcon getHolographicOutlineView() {
137 return mHolographicOutlineView;
138 }
139
140 protected Bitmap getHolographicOutline() {
141 return mHolographicOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -0700142 }
143
Winson Chung823c9692011-03-01 10:57:29 -0800144 private boolean queueHolographicOutlineCreation() {
Michael Jurka0620bec2010-10-25 17:50:09 -0700145 // 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);
Winson Chung823c9692011-03-01 10:57:29 -0800150 return true;
Michael Jurka0620bec2010-10-25 17:50:09 -0700151 }
Winson Chung823c9692011-03-01 10:57:29 -0800152 return false;
Michael Jurka0620bec2010-10-25 17:50:09 -0700153 }
154
Winson Chung6a70e9f2011-05-17 16:24:49 -0700155 public void loadHolographicIcon() {
156 if (mHolographicOutline == null) {
157 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
158 if (!queueHolographicOutlineCreation()) {
159 getHolographicOutlineView().invalidate();
160 }
161 }
162 }
163 public void clearHolographicIcon() {
164 mHolographicOutline = null;
165 getHolographicOutlineView().invalidate();
166 }
167
Winson Chungdf4b83d2010-10-20 17:49:27 -0700168 public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache,
Winson Chung04998342011-01-05 13:54:43 -0800169 boolean scaleUp, boolean createHolographicOutlines) {
Winson Chung6a70e9f2011-05-17 16:24:49 -0700170 mIconCache = cache;
171 mIconCacheKey = new PagedViewIconCache.Key(info);
Michael Jurkac9a96192010-11-01 11:52:08 -0700172 mIcon = info.iconBitmap;
Michael Jurka0620bec2010-10-25 17:50:09 -0700173 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700174 setText(info.title);
175 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700176
Winson Chung04998342011-01-05 13:54:43 -0800177 if (createHolographicOutlines) {
Winson Chung04998342011-01-05 13:54:43 -0800178 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
Michael Jurkab9b8ce92011-05-05 15:05:07 -0700179 if (!queueHolographicOutlineCreation()) {
Winson Chung823c9692011-03-01 10:57:29 -0800180 getHolographicOutlineView().invalidate();
181 }
Winson Chung04998342011-01-05 13:54:43 -0800182 }
Winson Chung241c3b42010-08-25 16:53:03 -0700183 }
184
185 public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
Winson Chung04998342011-01-05 13:54:43 -0800186 PagedViewIconCache cache, IconCache modelIconCache, boolean createHolographicOutlines) {
Winson Chung4dbea792011-05-05 14:21:32 -0700187 ComponentName cn = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
188 mIcon = modelIconCache.getIcon(cn, info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700189 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700190 setText(info.loadLabel(packageManager));
191 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700192
Winson Chung04998342011-01-05 13:54:43 -0800193 if (createHolographicOutlines) {
194 mIconCache = cache;
195 mIconCacheKey = new PagedViewIconCache.Key(info);
196 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
Michael Jurkab9b8ce92011-05-05 15:05:07 -0700197 if (!queueHolographicOutlineCreation()) {
Winson Chung823c9692011-03-01 10:57:29 -0800198 getHolographicOutlineView().invalidate();
199 }
Winson Chung04998342011-01-05 13:54:43 -0800200 }
Winson Chung241c3b42010-08-25 16:53:03 -0700201 }
202
Winson Chungb3347bb2010-08-19 14:51:28 -0700203 @Override
204 public void setAlpha(float alpha) {
205 final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700206 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chunge22a8e92010-11-12 13:40:58 -0800207 int newViewAlpha = (int) (viewAlpha * 255);
208 int newHolographicAlpha = (int) (holographicAlpha * 255);
209 if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
210 mAlpha = newViewAlpha;
211 mHolographicAlpha = newHolographicAlpha;
212 super.setAlpha(viewAlpha);
213 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700214 }
215
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700216 public void invalidateCheckedImage() {
217 if (mCheckedOutline != null) {
218 mCheckedOutline.recycle();
219 mCheckedOutline = null;
220 }
221 }
222
Winson Chungb3347bb2010-08-19 14:51:28 -0700223 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700224 protected void onDraw(Canvas canvas) {
225 if (mAlpha > 0) {
226 super.onDraw(canvas);
227 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700228
Michael Jurka0620bec2010-10-25 17:50:09 -0700229 Bitmap overlay = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700230
Michael Jurka0620bec2010-10-25 17:50:09 -0700231 // draw any blended overlays
Winson Chung6a70e9f2011-05-17 16:24:49 -0700232 if (mCheckedOutline != null) {
Michael Jurka0620bec2010-10-25 17:50:09 -0700233 mPaint.setAlpha(255);
234 overlay = mCheckedOutline;
235 }
236
237 if (overlay != null) {
Winson Chungc84001e2010-11-09 12:20:57 -0800238 final int offset = getScrollX();
Michael Jurka0620bec2010-10-25 17:50:09 -0700239 final int compoundPaddingLeft = getCompoundPaddingLeft();
240 final int compoundPaddingRight = getCompoundPaddingRight();
241 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
242 canvas.drawBitmap(overlay,
Winson Chungc84001e2010-11-09 12:20:57 -0800243 offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
Michael Jurka0620bec2010-10-25 17:50:09 -0700244 mPaddingTop,
245 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700246 }
247 }
248
Winson Chungb3347bb2010-08-19 14:51:28 -0700249 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700250 public void onDetachedFromWindow() {
251 super.onDetachedFromWindow();
252 sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this);
Winson Chungb3347bb2010-08-19 14:51:28 -0700253 }
Winson Chung03c568e2010-08-19 17:16:59 -0700254
255 @Override
Winson Chung97d85d22011-04-13 11:27:36 -0700256 public boolean onKeyDown(int keyCode, KeyEvent event) {
257 return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event)
258 || super.onKeyDown(keyCode, event);
259 }
260
261 @Override
262 public boolean onKeyUp(int keyCode, KeyEvent event) {
263 return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event)
264 || super.onKeyUp(keyCode, event);
265 }
266
267 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700268 public boolean isChecked() {
269 return mIsChecked;
270 }
271
Patrick Dubroy5f445422011-02-18 14:35:21 -0800272 void setChecked(boolean checked, boolean animate) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700273 if (mIsChecked != checked) {
274 mIsChecked = checked;
275
Winson Chungcd4bc492010-12-09 18:52:32 -0800276 float alpha;
277 int duration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700278 if (mIsChecked) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800279 alpha = mCheckedAlpha;
280 duration = mCheckedFadeInDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700281 } else {
Winson Chungcd4bc492010-12-09 18:52:32 -0800282 alpha = 1.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800283 duration = mCheckedFadeOutDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700284 }
285
Winson Chungcd4bc492010-12-09 18:52:32 -0800286 // Initialize the animator
287 if (mCheckedAlphaAnimator != null) {
288 mCheckedAlphaAnimator.cancel();
289 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800290 if (animate) {
291 mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha);
292 mCheckedAlphaAnimator.setDuration(duration);
293 mCheckedAlphaAnimator.start();
294 } else {
295 setAlpha(alpha);
296 }
Winson Chungcd4bc492010-12-09 18:52:32 -0800297
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700298 invalidate();
299 }
Winson Chung03c568e2010-08-19 17:16:59 -0700300 }
301
302 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800303 public void setChecked(boolean checked) {
304 setChecked(checked, true);
305 }
306
307 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700308 public void toggle() {
309 setChecked(!mIsChecked);
310 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700311}