blob: 95bb72f49eacdc2a1ed84b7c4fb67565a3a1ebfa [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
Michael Jurka742574b2011-02-02 23:51:01 -080019import com.android.launcher.R;
20
Winson Chungcd4bc492010-12-09 18:52:32 -080021import android.animation.ObjectAnimator;
Winson Chungb3347bb2010-08-19 14:51:28 -070022import android.content.Context;
Winson Chung241c3b42010-08-25 16:53:03 -070023import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
Winson Chung59e1f9a2010-12-21 11:31:54 -080025import android.content.res.Resources;
Winson Chung64a3cd42010-09-17 16:47:33 -070026import android.content.res.TypedArray;
Winson Chungb3347bb2010-08-19 14:51:28 -070027import android.graphics.Bitmap;
Winson Chungb3347bb2010-08-19 14:51:28 -070028import android.graphics.Canvas;
Winson Chungb3347bb2010-08-19 14:51:28 -070029import android.graphics.Paint;
Michael Jurka0620bec2010-10-25 17:50:09 -070030import android.os.Handler;
31import android.os.HandlerThread;
32import android.os.Message;
Winson Chungb3347bb2010-08-19 14:51:28 -070033import android.util.AttributeSet;
Winson Chung97d85d22011-04-13 11:27:36 -070034import android.view.KeyEvent;
Winson Chung03c568e2010-08-19 17:16:59 -070035import android.widget.Checkable;
Michael Jurka742574b2011-02-02 23:51:01 -080036import android.widget.TextView;
Winson Chungc84001e2010-11-09 12:20:57 -080037
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 Chungb46a2d12011-04-22 14:13:05 -070060 private boolean mHolographicEffectsEnabled;
Winson Chung03c568e2010-08-19 17:16:59 -070061 private boolean mIsChecked;
Winson Chungcd4bc492010-12-09 18:52:32 -080062 private ObjectAnimator mCheckedAlphaAnimator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080063 private float mCheckedAlpha = 1.0f;
64 private int mCheckedFadeInDuration;
65 private int mCheckedFadeOutDuration;
Winson Chung03c568e2010-08-19 17:16:59 -070066
Michael Jurkac9a96192010-11-01 11:52:08 -070067 // Highlight colors
Winson Chung64a3cd42010-09-17 16:47:33 -070068 private int mHoloBlurColor;
69 private int mHoloOutlineColor;
Winson Chung64a3cd42010-09-17 16:47:33 -070070
Michael Jurka8245a862011-02-01 17:53:59 -080071 HolographicPagedViewIcon mHolographicOutlineView;
72
Michael Jurka0620bec2010-10-25 17:50:09 -070073 private static final HandlerThread sWorkerThread = new HandlerThread("pagedviewicon-helper");
74 static {
75 sWorkerThread.start();
76 }
77
78 private static final int MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE = 1;
79
80 private static final Handler sWorker = new Handler(sWorkerThread.getLooper()) {
81 private DeferredHandler mHandler = new DeferredHandler();
82 private Paint mPaint = new Paint();
83 public void handleMessage(Message msg) {
84 final PagedViewIcon icon = (PagedViewIcon) msg.obj;
85
86 final Bitmap holographicOutline = Bitmap.createBitmap(
87 icon.mIcon.getWidth(), icon.mIcon.getHeight(), Bitmap.Config.ARGB_8888);
88 Canvas holographicOutlineCanvas = new Canvas(holographicOutline);
89 holographicOutlineCanvas.drawBitmap(icon.mIcon, 0, 0, mPaint);
90
Adam Cohen5bb50bd2010-12-03 11:39:55 -080091 sHolographicOutlineHelper.applyThickExpensiveOutlineWithBlur(holographicOutline,
Michael Jurka0620bec2010-10-25 17:50:09 -070092 holographicOutlineCanvas, icon.mHoloBlurColor, icon.mHoloOutlineColor);
93
94 mHandler.post(new Runnable() {
95 public void run() {
96 icon.mHolographicOutline = holographicOutline;
97 icon.mIconCache.addOutline(icon.mIconCacheKey, holographicOutline);
Michael Jurka8245a862011-02-01 17:53:59 -080098 icon.getHolographicOutlineView().invalidate();
Michael Jurka0620bec2010-10-25 17:50:09 -070099 }
100 });
101 }
102 };
Winson Chung64a3cd42010-09-17 16:47:33 -0700103
Winson Chungb3347bb2010-08-19 14:51:28 -0700104 public PagedViewIcon(Context context) {
105 this(context, null);
106 }
107
108 public PagedViewIcon(Context context, AttributeSet attrs) {
109 this(context, attrs, 0);
110 }
111
112 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
113 super(context, attrs, defStyle);
Winson Chung29d6fea2010-12-01 15:47:31 -0800114
Winson Chung64a3cd42010-09-17 16:47:33 -0700115 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0);
116 mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0);
117 mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0);
Winson Chung64a3cd42010-09-17 16:47:33 -0700118 a.recycle();
Winson Chungb3347bb2010-08-19 14:51:28 -0700119
120 if (sHolographicOutlineHelper == null) {
Winson Chung64a3cd42010-09-17 16:47:33 -0700121 sHolographicOutlineHelper = new HolographicOutlineHelper();
Winson Chungb3347bb2010-08-19 14:51:28 -0700122 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700123
Winson Chung59e1f9a2010-12-21 11:31:54 -0800124 // Set up fade in/out constants
125 final Resources r = context.getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -0700126 final int alpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha);
Winson Chung59e1f9a2010-12-21 11:31:54 -0800127 if (alpha > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700128 mCheckedAlpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha) / 256.0f;
129 mCheckedFadeInDuration =
130 r.getInteger(R.integer.config_dragAppsCustomizeIconFadeInDuration);
131 mCheckedFadeOutDuration =
132 r.getInteger(R.integer.config_dragAppsCustomizeIconFadeOutDuration);
Winson Chung59e1f9a2010-12-21 11:31:54 -0800133 }
134
Michael Jurka8245a862011-02-01 17:53:59 -0800135 mHolographicOutlineView = new HolographicPagedViewIcon(context, this);
Winson Chungb46a2d12011-04-22 14:13:05 -0700136 mHolographicEffectsEnabled = isHardwareAccelerated();
Michael Jurka8245a862011-02-01 17:53:59 -0800137 }
138
139 protected HolographicPagedViewIcon getHolographicOutlineView() {
140 return mHolographicOutlineView;
141 }
142
143 protected Bitmap getHolographicOutline() {
144 return mHolographicOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -0700145 }
146
Winson Chung823c9692011-03-01 10:57:29 -0800147 private boolean queueHolographicOutlineCreation() {
Michael Jurka0620bec2010-10-25 17:50:09 -0700148 // Generate the outline in the background
149 if (mHolographicOutline == null) {
150 Message m = sWorker.obtainMessage(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE);
151 m.obj = this;
152 sWorker.sendMessage(m);
Winson Chung823c9692011-03-01 10:57:29 -0800153 return true;
Michael Jurka0620bec2010-10-25 17:50:09 -0700154 }
Winson Chung823c9692011-03-01 10:57:29 -0800155 return false;
Michael Jurka0620bec2010-10-25 17:50:09 -0700156 }
157
Winson Chungdf4b83d2010-10-20 17:49:27 -0700158 public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache,
Winson Chung04998342011-01-05 13:54:43 -0800159 boolean scaleUp, boolean createHolographicOutlines) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700160 mIcon = info.iconBitmap;
Michael Jurka0620bec2010-10-25 17:50:09 -0700161 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700162 setText(info.title);
163 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700164
Winson Chung04998342011-01-05 13:54:43 -0800165 if (createHolographicOutlines) {
166 mIconCache = cache;
167 mIconCacheKey = new PagedViewIconCache.Key(info);
168 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
Winson Chungb46a2d12011-04-22 14:13:05 -0700169 if (mHolographicEffectsEnabled && !queueHolographicOutlineCreation()) {
Winson Chung823c9692011-03-01 10:57:29 -0800170 getHolographicOutlineView().invalidate();
171 }
Winson Chung04998342011-01-05 13:54:43 -0800172 }
Winson Chung241c3b42010-08-25 16:53:03 -0700173 }
174
175 public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
Winson Chung04998342011-01-05 13:54:43 -0800176 PagedViewIconCache cache, IconCache modelIconCache, boolean createHolographicOutlines) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700177 mIcon = Utilities.createIconBitmap(
178 modelIconCache.getFullResIcon(info, packageManager), mContext);
Michael Jurka0620bec2010-10-25 17:50:09 -0700179 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700180 setText(info.loadLabel(packageManager));
181 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700182
Winson Chung04998342011-01-05 13:54:43 -0800183 if (createHolographicOutlines) {
184 mIconCache = cache;
185 mIconCacheKey = new PagedViewIconCache.Key(info);
186 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
Winson Chungb46a2d12011-04-22 14:13:05 -0700187 if (mHolographicEffectsEnabled && !queueHolographicOutlineCreation()) {
Winson Chung823c9692011-03-01 10:57:29 -0800188 getHolographicOutlineView().invalidate();
189 }
Winson Chung04998342011-01-05 13:54:43 -0800190 }
Winson Chung241c3b42010-08-25 16:53:03 -0700191 }
192
Winson Chungb3347bb2010-08-19 14:51:28 -0700193 @Override
194 public void setAlpha(float alpha) {
195 final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700196 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chunge22a8e92010-11-12 13:40:58 -0800197 int newViewAlpha = (int) (viewAlpha * 255);
198 int newHolographicAlpha = (int) (holographicAlpha * 255);
199 if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
200 mAlpha = newViewAlpha;
201 mHolographicAlpha = newHolographicAlpha;
202 super.setAlpha(viewAlpha);
203 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700204 }
205
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700206 public void invalidateCheckedImage() {
207 if (mCheckedOutline != null) {
208 mCheckedOutline.recycle();
209 mCheckedOutline = null;
210 }
211 }
212
Winson Chungb3347bb2010-08-19 14:51:28 -0700213 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700214 protected void onDraw(Canvas canvas) {
215 if (mAlpha > 0) {
216 super.onDraw(canvas);
217 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700218
Michael Jurka0620bec2010-10-25 17:50:09 -0700219 Bitmap overlay = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700220
Michael Jurka0620bec2010-10-25 17:50:09 -0700221 // draw any blended overlays
222 if (mCheckedOutline == null) {
Winson Chungb46a2d12011-04-22 14:13:05 -0700223 if (mHolographicEffectsEnabled && mHolographicOutline != null
224 && mHolographicAlpha > 0) {
Michael Jurka0620bec2010-10-25 17:50:09 -0700225 mPaint.setAlpha(mHolographicAlpha);
226 overlay = mHolographicOutline;
227 }
228 } else {
229 mPaint.setAlpha(255);
230 overlay = mCheckedOutline;
231 }
232
233 if (overlay != null) {
Winson Chungc84001e2010-11-09 12:20:57 -0800234 final int offset = getScrollX();
Michael Jurka0620bec2010-10-25 17:50:09 -0700235 final int compoundPaddingLeft = getCompoundPaddingLeft();
236 final int compoundPaddingRight = getCompoundPaddingRight();
237 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
238 canvas.drawBitmap(overlay,
Winson Chungc84001e2010-11-09 12:20:57 -0800239 offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
Michael Jurka0620bec2010-10-25 17:50:09 -0700240 mPaddingTop,
241 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700242 }
243 }
244
Winson Chungb3347bb2010-08-19 14:51:28 -0700245 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700246 public void onDetachedFromWindow() {
247 super.onDetachedFromWindow();
248 sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this);
Winson Chungb3347bb2010-08-19 14:51:28 -0700249 }
Winson Chung03c568e2010-08-19 17:16:59 -0700250
251 @Override
Winson Chung97d85d22011-04-13 11:27:36 -0700252 public boolean onKeyDown(int keyCode, KeyEvent event) {
253 return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event)
254 || super.onKeyDown(keyCode, event);
255 }
256
257 @Override
258 public boolean onKeyUp(int keyCode, KeyEvent event) {
259 return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event)
260 || super.onKeyUp(keyCode, event);
261 }
262
263 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700264 public boolean isChecked() {
265 return mIsChecked;
266 }
267
Patrick Dubroy5f445422011-02-18 14:35:21 -0800268 void setChecked(boolean checked, boolean animate) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700269 if (mIsChecked != checked) {
270 mIsChecked = checked;
271
Winson Chungcd4bc492010-12-09 18:52:32 -0800272 float alpha;
273 int duration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700274 if (mIsChecked) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800275 alpha = mCheckedAlpha;
276 duration = mCheckedFadeInDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700277 } else {
Winson Chungcd4bc492010-12-09 18:52:32 -0800278 alpha = 1.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800279 duration = mCheckedFadeOutDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700280 }
281
Winson Chungcd4bc492010-12-09 18:52:32 -0800282 // Initialize the animator
283 if (mCheckedAlphaAnimator != null) {
284 mCheckedAlphaAnimator.cancel();
285 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800286 if (animate) {
287 mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha);
288 mCheckedAlphaAnimator.setDuration(duration);
289 mCheckedAlphaAnimator.start();
290 } else {
291 setAlpha(alpha);
292 }
Winson Chungcd4bc492010-12-09 18:52:32 -0800293
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700294 invalidate();
295 }
Winson Chung03c568e2010-08-19 17:16:59 -0700296 }
297
298 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800299 public void setChecked(boolean checked) {
300 setChecked(checked, true);
301 }
302
303 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700304 public void toggle() {
305 setChecked(!mIsChecked);
306 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700307}