blob: fbc67d8a08173f48fdb45132a98275bb8372252c [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
19import android.content.Context;
Winson Chung241c3b42010-08-25 16:53:03 -070020import android.content.pm.PackageManager;
21import android.content.pm.ResolveInfo;
Winson Chung64a3cd42010-09-17 16:47:33 -070022import android.content.res.TypedArray;
Winson Chungb3347bb2010-08-19 14:51:28 -070023import android.graphics.Bitmap;
Winson Chungb3347bb2010-08-19 14:51:28 -070024import android.graphics.Canvas;
Winson Chungb3347bb2010-08-19 14:51:28 -070025import android.graphics.Paint;
Michael Jurka0620bec2010-10-25 17:50:09 -070026import android.os.Handler;
27import android.os.HandlerThread;
28import android.os.Message;
Winson Chungb3347bb2010-08-19 14:51:28 -070029import android.util.AttributeSet;
Winson Chung03c568e2010-08-19 17:16:59 -070030import android.widget.Checkable;
Winson Chungb3347bb2010-08-19 14:51:28 -070031import android.widget.TextView;
32
Winson Chungc84001e2010-11-09 12:20:57 -080033import com.android.launcher.R;
34import com.android.launcher2.PagedView.PagedViewIconCache;
35
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 Jurka67b2f6c2010-11-17 12:33:46 -080042public class PagedViewIcon extends CacheableTextView 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 Chung241c3b42010-08-25 16:53:03 -070052 private Object mIconCacheKey;
53 private PagedViewIconCache mIconCache;
54
Winson Chung29d6fea2010-12-01 15:47:31 -080055 private int mAlpha;
Winson Chungb3347bb2010-08-19 14:51:28 -070056 private int mHolographicAlpha;
57
Winson Chung03c568e2010-08-19 17:16:59 -070058 private boolean mIsChecked;
59
Michael Jurkac9a96192010-11-01 11:52:08 -070060 // Highlight colors
Winson Chung64a3cd42010-09-17 16:47:33 -070061 private int mHoloBlurColor;
62 private int mHoloOutlineColor;
63 private int mCheckedBlurColor;
64 private int mCheckedOutlineColor;
65
Michael Jurka0620bec2010-10-25 17:50:09 -070066 private static final HandlerThread sWorkerThread = new HandlerThread("pagedviewicon-helper");
67 static {
68 sWorkerThread.start();
69 }
70
71 private static final int MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE = 1;
72
73 private static final Handler sWorker = new Handler(sWorkerThread.getLooper()) {
74 private DeferredHandler mHandler = new DeferredHandler();
75 private Paint mPaint = new Paint();
76 public void handleMessage(Message msg) {
77 final PagedViewIcon icon = (PagedViewIcon) msg.obj;
78
79 final Bitmap holographicOutline = Bitmap.createBitmap(
80 icon.mIcon.getWidth(), icon.mIcon.getHeight(), Bitmap.Config.ARGB_8888);
81 Canvas holographicOutlineCanvas = new Canvas(holographicOutline);
82 holographicOutlineCanvas.drawBitmap(icon.mIcon, 0, 0, mPaint);
83
Adam Cohen5bb50bd2010-12-03 11:39:55 -080084 sHolographicOutlineHelper.applyThickExpensiveOutlineWithBlur(holographicOutline,
Michael Jurka0620bec2010-10-25 17:50:09 -070085 holographicOutlineCanvas, icon.mHoloBlurColor, icon.mHoloOutlineColor);
86
87 mHandler.post(new Runnable() {
88 public void run() {
89 icon.mHolographicOutline = holographicOutline;
90 icon.mIconCache.addOutline(icon.mIconCacheKey, holographicOutline);
91 icon.invalidate();
92 }
93 });
94 }
95 };
Winson Chung64a3cd42010-09-17 16:47:33 -070096
Winson Chungb3347bb2010-08-19 14:51:28 -070097 public PagedViewIcon(Context context) {
98 this(context, null);
99 }
100
101 public PagedViewIcon(Context context, AttributeSet attrs) {
102 this(context, attrs, 0);
103 }
104
105 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
106 super(context, attrs, defStyle);
Winson Chung29d6fea2010-12-01 15:47:31 -0800107
Winson Chung64a3cd42010-09-17 16:47:33 -0700108 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0);
Winson Chung29d6fea2010-12-01 15:47:31 -0800109 mAlpha = 255;
Winson Chung64a3cd42010-09-17 16:47:33 -0700110 mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0);
111 mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0);
112 mCheckedBlurColor = a.getColor(R.styleable.PagedViewIcon_checkedBlurColor, 0);
113 mCheckedOutlineColor = a.getColor(R.styleable.PagedViewIcon_checkedOutlineColor, 0);
114 a.recycle();
Winson Chungb3347bb2010-08-19 14:51:28 -0700115
116 if (sHolographicOutlineHelper == null) {
Winson Chung64a3cd42010-09-17 16:47:33 -0700117 sHolographicOutlineHelper = new HolographicOutlineHelper();
Winson Chungb3347bb2010-08-19 14:51:28 -0700118 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700119
120 setFocusable(true);
121 setBackgroundDrawable(null);
122 }
123
Michael Jurka0620bec2010-10-25 17:50:09 -0700124 private void queueHolographicOutlineCreation() {
Winson Chung3b823cd2010-12-06 15:25:00 -0800125 /* Temporarily disabling holographic outline creation.
Michael Jurka0620bec2010-10-25 17:50:09 -0700126 // Generate the outline in the background
127 if (mHolographicOutline == null) {
128 Message m = sWorker.obtainMessage(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE);
129 m.obj = this;
130 sWorker.sendMessage(m);
131 }
Winson Chung3b823cd2010-12-06 15:25:00 -0800132 */
Michael Jurka0620bec2010-10-25 17:50:09 -0700133 }
134
Winson Chungdf4b83d2010-10-20 17:49:27 -0700135 public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache,
136 boolean scaleUp) {
Winson Chung241c3b42010-08-25 16:53:03 -0700137 mIconCache = cache;
138 mIconCacheKey = info;
139 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
140
Michael Jurkac9a96192010-11-01 11:52:08 -0700141 mIcon = info.iconBitmap;
Michael Jurka0620bec2010-10-25 17:50:09 -0700142 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700143 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800144 buildAndEnableCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700145 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700146
147 queueHolographicOutlineCreation();
Winson Chung241c3b42010-08-25 16:53:03 -0700148 }
149
150 public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
Michael Jurkac9a96192010-11-01 11:52:08 -0700151 PagedViewIconCache cache, IconCache modelIconCache) {
Winson Chung241c3b42010-08-25 16:53:03 -0700152 mIconCache = cache;
153 mIconCacheKey = info;
154 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
155
Michael Jurkac9a96192010-11-01 11:52:08 -0700156 mIcon = Utilities.createIconBitmap(
157 modelIconCache.getFullResIcon(info, packageManager), mContext);
Michael Jurka0620bec2010-10-25 17:50:09 -0700158 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700159 setText(info.loadLabel(packageManager));
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800160 buildAndEnableCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700161 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700162
163 queueHolographicOutlineCreation();
Winson Chung241c3b42010-08-25 16:53:03 -0700164 }
165
Winson Chungb3347bb2010-08-19 14:51:28 -0700166 @Override
167 public void setAlpha(float alpha) {
168 final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700169 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chunge22a8e92010-11-12 13:40:58 -0800170 int newViewAlpha = (int) (viewAlpha * 255);
171 int newHolographicAlpha = (int) (holographicAlpha * 255);
172 if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
173 mAlpha = newViewAlpha;
174 mHolographicAlpha = newHolographicAlpha;
175 super.setAlpha(viewAlpha);
176 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700177 }
178
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700179 public void invalidateCheckedImage() {
180 if (mCheckedOutline != null) {
181 mCheckedOutline.recycle();
182 mCheckedOutline = null;
183 }
184 }
185
Winson Chungb3347bb2010-08-19 14:51:28 -0700186 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700187 protected void onDraw(Canvas canvas) {
188 if (mAlpha > 0) {
189 super.onDraw(canvas);
190 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700191
Michael Jurka0620bec2010-10-25 17:50:09 -0700192 Bitmap overlay = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700193
Michael Jurka0620bec2010-10-25 17:50:09 -0700194 // draw any blended overlays
195 if (mCheckedOutline == null) {
196 if (mHolographicOutline != null && mHolographicAlpha > 0) {
197 mPaint.setAlpha(mHolographicAlpha);
198 overlay = mHolographicOutline;
199 }
200 } else {
201 mPaint.setAlpha(255);
202 overlay = mCheckedOutline;
203 }
204
205 if (overlay != null) {
Winson Chungc84001e2010-11-09 12:20:57 -0800206 final int offset = getScrollX();
Michael Jurka0620bec2010-10-25 17:50:09 -0700207 final int compoundPaddingLeft = getCompoundPaddingLeft();
208 final int compoundPaddingRight = getCompoundPaddingRight();
209 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
210 canvas.drawBitmap(overlay,
Winson Chungc84001e2010-11-09 12:20:57 -0800211 offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
Michael Jurka0620bec2010-10-25 17:50:09 -0700212 mPaddingTop,
213 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700214 }
215 }
216
Winson Chungb3347bb2010-08-19 14:51:28 -0700217 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700218 public void onDetachedFromWindow() {
219 super.onDetachedFromWindow();
220 sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this);
Winson Chungb3347bb2010-08-19 14:51:28 -0700221 }
Winson Chung03c568e2010-08-19 17:16:59 -0700222
223 @Override
224 public boolean isChecked() {
225 return mIsChecked;
226 }
227
228 @Override
229 public void setChecked(boolean checked) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700230 if (mIsChecked != checked) {
231 mIsChecked = checked;
232
233 if (mIsChecked) {
Michael Jurka0620bec2010-10-25 17:50:09 -0700234 mCheckedOutline = Bitmap.createBitmap(mIcon.getWidth(), mIcon.getHeight(),
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700235 Bitmap.Config.ARGB_8888);
Michael Jurka0620bec2010-10-25 17:50:09 -0700236 Canvas checkedOutlineCanvas = new Canvas(mCheckedOutline);
237 mPaint.setAlpha(255);
238 checkedOutlineCanvas.drawBitmap(mIcon, 0, 0, mPaint);
239
Adam Cohen5bb50bd2010-12-03 11:39:55 -0800240 sHolographicOutlineHelper.applyThickExpensiveOutlineWithBlur(mCheckedOutline,
Michael Jurka0620bec2010-10-25 17:50:09 -0700241 checkedOutlineCanvas, mCheckedBlurColor, mCheckedOutlineColor);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700242 } else {
243 invalidateCheckedImage();
244 }
245
246 invalidate();
247 }
Winson Chung03c568e2010-08-19 17:16:59 -0700248 }
249
250 @Override
251 public void toggle() {
252 setChecked(!mIsChecked);
253 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700254}