blob: f46b63c025eadf91aca431893cea97f2101b19d0 [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 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();
125 final int alpha = r.getInteger(R.integer.icon_allAppsCustomizeFadeAlpha);
126 if (alpha > 0) {
127 mCheckedAlpha = r.getInteger(R.integer.icon_allAppsCustomizeFadeAlpha) / 256.0f;
128 mCheckedFadeInDuration = r.getInteger(R.integer.icon_allAppsCustomizeFadeInTime);
129 mCheckedFadeOutDuration = r.getInteger(R.integer.icon_allAppsCustomizeFadeOutTime);
130 }
131
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
Winson Chung823c9692011-03-01 10:57:29 -0800143 private boolean queueHolographicOutlineCreation() {
Michael Jurka0620bec2010-10-25 17:50:09 -0700144 // 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);
Winson Chung823c9692011-03-01 10:57:29 -0800149 return true;
Michael Jurka0620bec2010-10-25 17:50:09 -0700150 }
Winson Chung823c9692011-03-01 10:57:29 -0800151 return false;
Michael Jurka0620bec2010-10-25 17:50:09 -0700152 }
153
Winson Chungdf4b83d2010-10-20 17:49:27 -0700154 public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache,
Winson Chung04998342011-01-05 13:54:43 -0800155 boolean scaleUp, boolean createHolographicOutlines) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700156 mIcon = info.iconBitmap;
Michael Jurka0620bec2010-10-25 17:50:09 -0700157 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700158 setText(info.title);
159 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700160
Winson Chung04998342011-01-05 13:54:43 -0800161 if (createHolographicOutlines) {
162 mIconCache = cache;
163 mIconCacheKey = new PagedViewIconCache.Key(info);
164 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
Winson Chung823c9692011-03-01 10:57:29 -0800165 if (!queueHolographicOutlineCreation()) {
166 getHolographicOutlineView().invalidate();
167 }
Winson Chung04998342011-01-05 13:54:43 -0800168 }
Winson Chung241c3b42010-08-25 16:53:03 -0700169 }
170
171 public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
Winson Chung04998342011-01-05 13:54:43 -0800172 PagedViewIconCache cache, IconCache modelIconCache, boolean createHolographicOutlines) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700173 mIcon = Utilities.createIconBitmap(
174 modelIconCache.getFullResIcon(info, packageManager), mContext);
Michael Jurka0620bec2010-10-25 17:50:09 -0700175 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700176 setText(info.loadLabel(packageManager));
177 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700178
Winson Chung04998342011-01-05 13:54:43 -0800179 if (createHolographicOutlines) {
180 mIconCache = cache;
181 mIconCacheKey = new PagedViewIconCache.Key(info);
182 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
Winson Chung823c9692011-03-01 10:57:29 -0800183 if (!queueHolographicOutlineCreation()) {
184 getHolographicOutlineView().invalidate();
185 }
Winson Chung04998342011-01-05 13:54:43 -0800186 }
Winson Chung241c3b42010-08-25 16:53:03 -0700187 }
188
Winson Chungb3347bb2010-08-19 14:51:28 -0700189 @Override
190 public void setAlpha(float alpha) {
191 final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700192 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chunge22a8e92010-11-12 13:40:58 -0800193 int newViewAlpha = (int) (viewAlpha * 255);
194 int newHolographicAlpha = (int) (holographicAlpha * 255);
195 if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
196 mAlpha = newViewAlpha;
197 mHolographicAlpha = newHolographicAlpha;
198 super.setAlpha(viewAlpha);
199 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700200 }
201
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700202 public void invalidateCheckedImage() {
203 if (mCheckedOutline != null) {
204 mCheckedOutline.recycle();
205 mCheckedOutline = null;
206 }
207 }
208
Winson Chungb3347bb2010-08-19 14:51:28 -0700209 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700210 protected void onDraw(Canvas canvas) {
211 if (mAlpha > 0) {
212 super.onDraw(canvas);
213 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700214
Michael Jurka0620bec2010-10-25 17:50:09 -0700215 Bitmap overlay = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700216
Michael Jurka0620bec2010-10-25 17:50:09 -0700217 // draw any blended overlays
218 if (mCheckedOutline == null) {
219 if (mHolographicOutline != null && mHolographicAlpha > 0) {
220 mPaint.setAlpha(mHolographicAlpha);
221 overlay = mHolographicOutline;
222 }
223 } else {
224 mPaint.setAlpha(255);
225 overlay = mCheckedOutline;
226 }
227
228 if (overlay != null) {
Winson Chungc84001e2010-11-09 12:20:57 -0800229 final int offset = getScrollX();
Michael Jurka0620bec2010-10-25 17:50:09 -0700230 final int compoundPaddingLeft = getCompoundPaddingLeft();
231 final int compoundPaddingRight = getCompoundPaddingRight();
232 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
233 canvas.drawBitmap(overlay,
Winson Chungc84001e2010-11-09 12:20:57 -0800234 offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
Michael Jurka0620bec2010-10-25 17:50:09 -0700235 mPaddingTop,
236 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700237 }
238 }
239
Winson Chungb3347bb2010-08-19 14:51:28 -0700240 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700241 public void onDetachedFromWindow() {
242 super.onDetachedFromWindow();
243 sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this);
Winson Chungb3347bb2010-08-19 14:51:28 -0700244 }
Winson Chung03c568e2010-08-19 17:16:59 -0700245
246 @Override
Winson Chung97d85d22011-04-13 11:27:36 -0700247 public boolean onKeyDown(int keyCode, KeyEvent event) {
248 return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event)
249 || super.onKeyDown(keyCode, event);
250 }
251
252 @Override
253 public boolean onKeyUp(int keyCode, KeyEvent event) {
254 return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event)
255 || super.onKeyUp(keyCode, event);
256 }
257
258 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700259 public boolean isChecked() {
260 return mIsChecked;
261 }
262
Patrick Dubroy5f445422011-02-18 14:35:21 -0800263 void setChecked(boolean checked, boolean animate) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700264 if (mIsChecked != checked) {
265 mIsChecked = checked;
266
Winson Chungcd4bc492010-12-09 18:52:32 -0800267 float alpha;
268 int duration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700269 if (mIsChecked) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800270 alpha = mCheckedAlpha;
271 duration = mCheckedFadeInDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700272 } else {
Winson Chungcd4bc492010-12-09 18:52:32 -0800273 alpha = 1.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800274 duration = mCheckedFadeOutDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700275 }
276
Winson Chungcd4bc492010-12-09 18:52:32 -0800277 // Initialize the animator
278 if (mCheckedAlphaAnimator != null) {
279 mCheckedAlphaAnimator.cancel();
280 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800281 if (animate) {
282 mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha);
283 mCheckedAlphaAnimator.setDuration(duration);
284 mCheckedAlphaAnimator.start();
285 } else {
286 setAlpha(alpha);
287 }
Winson Chungcd4bc492010-12-09 18:52:32 -0800288
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700289 invalidate();
290 }
Winson Chung03c568e2010-08-19 17:16:59 -0700291 }
292
293 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800294 public void setChecked(boolean checked) {
295 setChecked(checked, true);
296 }
297
298 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700299 public void toggle() {
300 setChecked(!mIsChecked);
301 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700302}