blob: be4999d584947a1eead1d976bf658809d150f52e [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 Jurka0620bec2010-10-25 17:50:09 -070019import com.android.launcher.R;
20import com.android.launcher2.PagedView.PagedViewIconCache;
21
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 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;
Winson Chungb3347bb2010-08-19 14:51:28 -070029import android.graphics.Rect;
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 Chung03c568e2010-08-19 17:16:59 -070034import android.widget.Checkable;
Winson Chungb3347bb2010-08-19 14:51:28 -070035import android.widget.TextView;
36
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 */
Winson Chung03c568e2010-08-19 17:16:59 -070043public class PagedViewIcon extends TextView 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;
51 private Canvas mHolographicOutlineCanvas;
Winson Chungb3347bb2010-08-19 14:51:28 -070052 private Rect mDrawableClipRect;
Michael Jurka0620bec2010-10-25 17:50:09 -070053 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070054
Winson Chung241c3b42010-08-25 16:53:03 -070055 private Object mIconCacheKey;
56 private PagedViewIconCache mIconCache;
Winson Chungdf4b83d2010-10-20 17:49:27 -070057 private int mScaledIconSize;
Winson Chung241c3b42010-08-25 16:53:03 -070058
Winson Chungb3347bb2010-08-19 14:51:28 -070059 private int mAlpha;
60 private int mHolographicAlpha;
61
Winson Chung03c568e2010-08-19 17:16:59 -070062 private boolean mIsChecked;
63
Winson Chung64a3cd42010-09-17 16:47:33 -070064 // Highlight colours
65 private int mHoloBlurColor;
66 private int mHoloOutlineColor;
67 private int mCheckedBlurColor;
68 private int mCheckedOutlineColor;
69
Michael Jurka0620bec2010-10-25 17:50:09 -070070 private static final HandlerThread sWorkerThread = new HandlerThread("pagedviewicon-helper");
71 static {
72 sWorkerThread.start();
73 }
74
75 private static final int MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE = 1;
76
77 private static final Handler sWorker = new Handler(sWorkerThread.getLooper()) {
78 private DeferredHandler mHandler = new DeferredHandler();
79 private Paint mPaint = new Paint();
80 public void handleMessage(Message msg) {
81 final PagedViewIcon icon = (PagedViewIcon) msg.obj;
82
83 final Bitmap holographicOutline = Bitmap.createBitmap(
84 icon.mIcon.getWidth(), icon.mIcon.getHeight(), Bitmap.Config.ARGB_8888);
85 Canvas holographicOutlineCanvas = new Canvas(holographicOutline);
86 holographicOutlineCanvas.drawBitmap(icon.mIcon, 0, 0, mPaint);
87
88 sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(holographicOutline,
89 holographicOutlineCanvas, icon.mHoloBlurColor, icon.mHoloOutlineColor);
90
91 mHandler.post(new Runnable() {
92 public void run() {
93 icon.mHolographicOutline = holographicOutline;
94 icon.mIconCache.addOutline(icon.mIconCacheKey, holographicOutline);
95 icon.invalidate();
96 }
97 });
98 }
99 };
Winson Chung64a3cd42010-09-17 16:47:33 -0700100
Winson Chungb3347bb2010-08-19 14:51:28 -0700101 public PagedViewIcon(Context context) {
102 this(context, null);
103 }
104
105 public PagedViewIcon(Context context, AttributeSet attrs) {
106 this(context, attrs, 0);
107 }
108
109 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
110 super(context, attrs, defStyle);
Winson Chung64a3cd42010-09-17 16:47:33 -0700111 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0);
112 mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0);
113 mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0);
114 mCheckedBlurColor = a.getColor(R.styleable.PagedViewIcon_checkedBlurColor, 0);
115 mCheckedOutlineColor = a.getColor(R.styleable.PagedViewIcon_checkedOutlineColor, 0);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700116 mScaledIconSize = a.getDimensionPixelSize(R.styleable.PagedViewIcon_scaledIconSize, 64);
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 }
122 mDrawableClipRect = new Rect();
123
124 setFocusable(true);
125 setBackgroundDrawable(null);
126 }
127
Michael Jurka0620bec2010-10-25 17:50:09 -0700128 private void queueHolographicOutlineCreation() {
129 // Generate the outline in the background
130 if (mHolographicOutline == null) {
131 Message m = sWorker.obtainMessage(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE);
132 m.obj = this;
133 sWorker.sendMessage(m);
134 }
135 }
136
Winson Chungdf4b83d2010-10-20 17:49:27 -0700137 public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache,
138 boolean scaleUp) {
Winson Chung241c3b42010-08-25 16:53:03 -0700139 mIconCache = cache;
140 mIconCacheKey = info;
141 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
142
Winson Chungdf4b83d2010-10-20 17:49:27 -0700143 if (scaleUp) {
Michael Jurka0620bec2010-10-25 17:50:09 -0700144 mIcon = Bitmap.createScaledBitmap(info.iconBitmap, mScaledIconSize,
Winson Chungdf4b83d2010-10-20 17:49:27 -0700145 mScaledIconSize, true);
146 } else {
Michael Jurka0620bec2010-10-25 17:50:09 -0700147 mIcon = info.iconBitmap;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700148 }
Michael Jurka0620bec2010-10-25 17:50:09 -0700149 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700150 setText(info.title);
151 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700152
153 queueHolographicOutlineCreation();
Winson Chung241c3b42010-08-25 16:53:03 -0700154 }
155
156 public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
157 PagedViewIconCache cache) {
158 mIconCache = cache;
159 mIconCacheKey = info;
160 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
161
Michael Jurka0620bec2010-10-25 17:50:09 -0700162 mIcon = Utilities.createIconBitmap(info.loadIcon(packageManager), mContext);
163 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700164 setText(info.loadLabel(packageManager));
165 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700166
167 queueHolographicOutlineCreation();
Winson Chung241c3b42010-08-25 16:53:03 -0700168 }
169
Winson Chungb3347bb2010-08-19 14:51:28 -0700170 @Override
171 public void setAlpha(float alpha) {
172 final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700173 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chungaffd7b42010-08-20 15:11:56 -0700174 mAlpha = (int) (viewAlpha * 255);
Winson Chungb3347bb2010-08-19 14:51:28 -0700175 mHolographicAlpha = (int) (holographicAlpha * 255);
Winson Chungb3347bb2010-08-19 14:51:28 -0700176 super.setAlpha(viewAlpha);
177 }
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) {
206 final int compoundPaddingLeft = getCompoundPaddingLeft();
207 final int compoundPaddingRight = getCompoundPaddingRight();
208 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
209 canvas.drawBitmap(overlay,
210 compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
211 mPaddingTop,
212 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700213 }
214 }
215
Winson Chungb3347bb2010-08-19 14:51:28 -0700216 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700217 public void onDetachedFromWindow() {
218 super.onDetachedFromWindow();
219 sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this);
Winson Chungb3347bb2010-08-19 14:51:28 -0700220 }
Winson Chung03c568e2010-08-19 17:16:59 -0700221
222 @Override
223 public boolean isChecked() {
224 return mIsChecked;
225 }
226
227 @Override
228 public void setChecked(boolean checked) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700229 if (mIsChecked != checked) {
230 mIsChecked = checked;
231
232 if (mIsChecked) {
Michael Jurka0620bec2010-10-25 17:50:09 -0700233 mCheckedOutline = Bitmap.createBitmap(mIcon.getWidth(), mIcon.getHeight(),
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700234 Bitmap.Config.ARGB_8888);
Michael Jurka0620bec2010-10-25 17:50:09 -0700235 Canvas checkedOutlineCanvas = new Canvas(mCheckedOutline);
236 mPaint.setAlpha(255);
237 checkedOutlineCanvas.drawBitmap(mIcon, 0, 0, mPaint);
238
Winson Chung64a3cd42010-09-17 16:47:33 -0700239 sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(mCheckedOutline,
Michael Jurka0620bec2010-10-25 17:50:09 -0700240 checkedOutlineCanvas, mCheckedBlurColor, mCheckedOutlineColor);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700241 } else {
242 invalidateCheckedImage();
243 }
244
245 invalidate();
246 }
Winson Chung03c568e2010-08-19 17:16:59 -0700247 }
248
249 @Override
250 public void toggle() {
251 setChecked(!mIsChecked);
252 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700253}