blob: 89cf331dd0f89d7d9208e7dc61d768900bd791c5 [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;
Michael Jurka0620bec2010-10-25 17:50:09 -070051 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070052
Winson Chung241c3b42010-08-25 16:53:03 -070053 private Object mIconCacheKey;
54 private PagedViewIconCache mIconCache;
55
Winson Chungb3347bb2010-08-19 14:51:28 -070056 private int mAlpha;
57 private int mHolographicAlpha;
58
Winson Chung03c568e2010-08-19 17:16:59 -070059 private boolean mIsChecked;
60
Michael Jurkac9a96192010-11-01 11:52:08 -070061 // Highlight colors
Winson Chung64a3cd42010-09-17 16:47:33 -070062 private int mHoloBlurColor;
63 private int mHoloOutlineColor;
64 private int mCheckedBlurColor;
65 private int mCheckedOutlineColor;
66
Michael Jurka0620bec2010-10-25 17:50:09 -070067 private static final HandlerThread sWorkerThread = new HandlerThread("pagedviewicon-helper");
68 static {
69 sWorkerThread.start();
70 }
71
72 private static final int MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE = 1;
73
74 private static final Handler sWorker = new Handler(sWorkerThread.getLooper()) {
75 private DeferredHandler mHandler = new DeferredHandler();
76 private Paint mPaint = new Paint();
77 public void handleMessage(Message msg) {
78 final PagedViewIcon icon = (PagedViewIcon) msg.obj;
79
80 final Bitmap holographicOutline = Bitmap.createBitmap(
81 icon.mIcon.getWidth(), icon.mIcon.getHeight(), Bitmap.Config.ARGB_8888);
82 Canvas holographicOutlineCanvas = new Canvas(holographicOutline);
83 holographicOutlineCanvas.drawBitmap(icon.mIcon, 0, 0, mPaint);
84
85 sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(holographicOutline,
86 holographicOutlineCanvas, icon.mHoloBlurColor, icon.mHoloOutlineColor);
87
88 mHandler.post(new Runnable() {
89 public void run() {
90 icon.mHolographicOutline = holographicOutline;
91 icon.mIconCache.addOutline(icon.mIconCacheKey, holographicOutline);
92 icon.invalidate();
93 }
94 });
95 }
96 };
Winson Chung64a3cd42010-09-17 16:47:33 -070097
Winson Chungb3347bb2010-08-19 14:51:28 -070098 public PagedViewIcon(Context context) {
99 this(context, null);
100 }
101
102 public PagedViewIcon(Context context, AttributeSet attrs) {
103 this(context, attrs, 0);
104 }
105
106 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
107 super(context, attrs, defStyle);
Winson Chung64a3cd42010-09-17 16:47:33 -0700108 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0);
109 mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0);
110 mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0);
111 mCheckedBlurColor = a.getColor(R.styleable.PagedViewIcon_checkedBlurColor, 0);
112 mCheckedOutlineColor = a.getColor(R.styleable.PagedViewIcon_checkedOutlineColor, 0);
Winson Chung0e41ae62010-10-27 18:10:32 -0700113
Winson Chung64a3cd42010-09-17 16:47:33 -0700114 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() {
125 // Generate the outline in the background
126 if (mHolographicOutline == null) {
127 Message m = sWorker.obtainMessage(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE);
128 m.obj = this;
129 sWorker.sendMessage(m);
130 }
131 }
132
Winson Chungdf4b83d2010-10-20 17:49:27 -0700133 public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache,
134 boolean scaleUp) {
Winson Chung241c3b42010-08-25 16:53:03 -0700135 mIconCache = cache;
136 mIconCacheKey = info;
137 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
138
Michael Jurkac9a96192010-11-01 11:52:08 -0700139 mIcon = info.iconBitmap;
Michael Jurka0620bec2010-10-25 17:50:09 -0700140 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700141 setText(info.title);
142 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700143
144 queueHolographicOutlineCreation();
Winson Chung241c3b42010-08-25 16:53:03 -0700145 }
146
147 public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
Michael Jurkac9a96192010-11-01 11:52:08 -0700148 PagedViewIconCache cache, IconCache modelIconCache) {
Winson Chung241c3b42010-08-25 16:53:03 -0700149 mIconCache = cache;
150 mIconCacheKey = info;
151 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
152
Michael Jurkac9a96192010-11-01 11:52:08 -0700153 mIcon = Utilities.createIconBitmap(
154 modelIconCache.getFullResIcon(info, packageManager), mContext);
Michael Jurka0620bec2010-10-25 17:50:09 -0700155 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700156 setText(info.loadLabel(packageManager));
157 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700158
159 queueHolographicOutlineCreation();
Winson Chung241c3b42010-08-25 16:53:03 -0700160 }
161
Winson Chungb3347bb2010-08-19 14:51:28 -0700162 @Override
163 public void setAlpha(float alpha) {
164 final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700165 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chungaffd7b42010-08-20 15:11:56 -0700166 mAlpha = (int) (viewAlpha * 255);
Winson Chungb3347bb2010-08-19 14:51:28 -0700167 mHolographicAlpha = (int) (holographicAlpha * 255);
Winson Chungb3347bb2010-08-19 14:51:28 -0700168 super.setAlpha(viewAlpha);
169 }
170
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700171 public void invalidateCheckedImage() {
172 if (mCheckedOutline != null) {
173 mCheckedOutline.recycle();
174 mCheckedOutline = null;
175 }
176 }
177
Winson Chungb3347bb2010-08-19 14:51:28 -0700178 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700179 protected void onDraw(Canvas canvas) {
180 if (mAlpha > 0) {
181 super.onDraw(canvas);
182 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700183
Michael Jurka0620bec2010-10-25 17:50:09 -0700184 Bitmap overlay = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700185
Michael Jurka0620bec2010-10-25 17:50:09 -0700186 // draw any blended overlays
187 if (mCheckedOutline == null) {
188 if (mHolographicOutline != null && mHolographicAlpha > 0) {
189 mPaint.setAlpha(mHolographicAlpha);
190 overlay = mHolographicOutline;
191 }
192 } else {
193 mPaint.setAlpha(255);
194 overlay = mCheckedOutline;
195 }
196
197 if (overlay != null) {
198 final int compoundPaddingLeft = getCompoundPaddingLeft();
199 final int compoundPaddingRight = getCompoundPaddingRight();
200 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
201 canvas.drawBitmap(overlay,
202 compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
203 mPaddingTop,
204 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700205 }
206 }
207
Winson Chungb3347bb2010-08-19 14:51:28 -0700208 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700209 public void onDetachedFromWindow() {
210 super.onDetachedFromWindow();
211 sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this);
Winson Chungb3347bb2010-08-19 14:51:28 -0700212 }
Winson Chung03c568e2010-08-19 17:16:59 -0700213
214 @Override
215 public boolean isChecked() {
216 return mIsChecked;
217 }
218
219 @Override
220 public void setChecked(boolean checked) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700221 if (mIsChecked != checked) {
222 mIsChecked = checked;
223
224 if (mIsChecked) {
Michael Jurka0620bec2010-10-25 17:50:09 -0700225 mCheckedOutline = Bitmap.createBitmap(mIcon.getWidth(), mIcon.getHeight(),
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700226 Bitmap.Config.ARGB_8888);
Michael Jurka0620bec2010-10-25 17:50:09 -0700227 Canvas checkedOutlineCanvas = new Canvas(mCheckedOutline);
228 mPaint.setAlpha(255);
229 checkedOutlineCanvas.drawBitmap(mIcon, 0, 0, mPaint);
230
Winson Chung64a3cd42010-09-17 16:47:33 -0700231 sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(mCheckedOutline,
Michael Jurka0620bec2010-10-25 17:50:09 -0700232 checkedOutlineCanvas, mCheckedBlurColor, mCheckedOutlineColor);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700233 } else {
234 invalidateCheckedImage();
235 }
236
237 invalidate();
238 }
Winson Chung03c568e2010-08-19 17:16:59 -0700239 }
240
241 @Override
242 public void toggle() {
243 setChecked(!mIsChecked);
244 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700245}