blob: 300bab5f00b8d2acb3100a95e6f030a7576f91cb [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() {
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);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800142 buildAndEnableCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700143 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700144
145 queueHolographicOutlineCreation();
Winson Chung241c3b42010-08-25 16:53:03 -0700146 }
147
148 public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
Michael Jurkac9a96192010-11-01 11:52:08 -0700149 PagedViewIconCache cache, IconCache modelIconCache) {
Winson Chung241c3b42010-08-25 16:53:03 -0700150 mIconCache = cache;
151 mIconCacheKey = info;
152 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
153
Michael Jurkac9a96192010-11-01 11:52:08 -0700154 mIcon = Utilities.createIconBitmap(
155 modelIconCache.getFullResIcon(info, packageManager), mContext);
Michael Jurka0620bec2010-10-25 17:50:09 -0700156 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700157 setText(info.loadLabel(packageManager));
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800158 buildAndEnableCache();
Winson Chung241c3b42010-08-25 16:53:03 -0700159 setTag(info);
Michael Jurka0620bec2010-10-25 17:50:09 -0700160
161 queueHolographicOutlineCreation();
Winson Chung241c3b42010-08-25 16:53:03 -0700162 }
163
Winson Chungb3347bb2010-08-19 14:51:28 -0700164 @Override
165 public void setAlpha(float alpha) {
166 final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700167 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chunge22a8e92010-11-12 13:40:58 -0800168 int newViewAlpha = (int) (viewAlpha * 255);
169 int newHolographicAlpha = (int) (holographicAlpha * 255);
170 if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
171 mAlpha = newViewAlpha;
172 mHolographicAlpha = newHolographicAlpha;
173 super.setAlpha(viewAlpha);
174 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700175 }
176
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700177 public void invalidateCheckedImage() {
178 if (mCheckedOutline != null) {
179 mCheckedOutline.recycle();
180 mCheckedOutline = null;
181 }
182 }
183
Winson Chungb3347bb2010-08-19 14:51:28 -0700184 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700185 protected void onDraw(Canvas canvas) {
186 if (mAlpha > 0) {
187 super.onDraw(canvas);
188 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700189
Michael Jurka0620bec2010-10-25 17:50:09 -0700190 Bitmap overlay = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700191
Michael Jurka0620bec2010-10-25 17:50:09 -0700192 // draw any blended overlays
193 if (mCheckedOutline == null) {
194 if (mHolographicOutline != null && mHolographicAlpha > 0) {
195 mPaint.setAlpha(mHolographicAlpha);
196 overlay = mHolographicOutline;
197 }
198 } else {
199 mPaint.setAlpha(255);
200 overlay = mCheckedOutline;
201 }
202
203 if (overlay != null) {
Winson Chungc84001e2010-11-09 12:20:57 -0800204 final int offset = getScrollX();
Michael Jurka0620bec2010-10-25 17:50:09 -0700205 final int compoundPaddingLeft = getCompoundPaddingLeft();
206 final int compoundPaddingRight = getCompoundPaddingRight();
207 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
208 canvas.drawBitmap(overlay,
Winson Chungc84001e2010-11-09 12:20:57 -0800209 offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
Michael Jurka0620bec2010-10-25 17:50:09 -0700210 mPaddingTop,
211 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700212 }
213 }
214
Winson Chungb3347bb2010-08-19 14:51:28 -0700215 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700216 public void onDetachedFromWindow() {
217 super.onDetachedFromWindow();
218 sWorker.removeMessages(MESSAGE_CREATE_HOLOGRAPHIC_OUTLINE, this);
Winson Chungb3347bb2010-08-19 14:51:28 -0700219 }
Winson Chung03c568e2010-08-19 17:16:59 -0700220
221 @Override
222 public boolean isChecked() {
223 return mIsChecked;
224 }
225
226 @Override
227 public void setChecked(boolean checked) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700228 if (mIsChecked != checked) {
229 mIsChecked = checked;
230
231 if (mIsChecked) {
Michael Jurka0620bec2010-10-25 17:50:09 -0700232 mCheckedOutline = Bitmap.createBitmap(mIcon.getWidth(), mIcon.getHeight(),
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700233 Bitmap.Config.ARGB_8888);
Michael Jurka0620bec2010-10-25 17:50:09 -0700234 Canvas checkedOutlineCanvas = new Canvas(mCheckedOutline);
235 mPaint.setAlpha(255);
236 checkedOutlineCanvas.drawBitmap(mIcon, 0, 0, mPaint);
237
Adam Cohen5bb50bd2010-12-03 11:39:55 -0800238 sHolographicOutlineHelper.applyThickExpensiveOutlineWithBlur(mCheckedOutline,
Michael Jurka0620bec2010-10-25 17:50:09 -0700239 checkedOutlineCanvas, mCheckedBlurColor, mCheckedOutlineColor);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700240 } else {
241 invalidateCheckedImage();
242 }
243
244 invalidate();
245 }
Winson Chung03c568e2010-08-19 17:16:59 -0700246 }
247
248 @Override
249 public void toggle() {
250 setChecked(!mIsChecked);
251 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700252}