blob: 0714a933cf6be0c4fb2daa7abc68b39fe0998429 [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;
26import android.graphics.PointF;
Winson Chungb3347bb2010-08-19 14:51:28 -070027import android.graphics.Rect;
28import android.graphics.Region.Op;
Winson Chung03c568e2010-08-19 17:16:59 -070029import android.graphics.drawable.Drawable;
Winson Chungb3347bb2010-08-19 14:51:28 -070030import android.util.AttributeSet;
Winson Chung03c568e2010-08-19 17:16:59 -070031import android.widget.Checkable;
Winson Chungb3347bb2010-08-19 14:51:28 -070032import android.widget.TextView;
33
Winson Chung64a3cd42010-09-17 16:47:33 -070034import com.android.launcher.R;
35import com.android.launcher2.PagedView.PagedViewIconCache;
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;
52 private boolean mIsHolographicUpdatePass;
53 private Rect mDrawableClipRect;
54
Winson Chung241c3b42010-08-25 16:53:03 -070055 private Object mIconCacheKey;
56 private PagedViewIconCache mIconCache;
57
Winson Chungb3347bb2010-08-19 14:51:28 -070058 private int mAlpha;
59 private int mHolographicAlpha;
60
Winson Chung03c568e2010-08-19 17:16:59 -070061 private boolean mIsChecked;
62
Winson Chung64a3cd42010-09-17 16:47:33 -070063 // Highlight colours
64 private int mHoloBlurColor;
65 private int mHoloOutlineColor;
66 private int mCheckedBlurColor;
67 private int mCheckedOutlineColor;
68
69
Winson Chungb3347bb2010-08-19 14:51:28 -070070 public PagedViewIcon(Context context) {
71 this(context, null);
72 }
73
74 public PagedViewIcon(Context context, AttributeSet attrs) {
75 this(context, attrs, 0);
76 }
77
78 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
79 super(context, attrs, defStyle);
Winson Chung64a3cd42010-09-17 16:47:33 -070080 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedViewIcon, defStyle, 0);
81 mHoloBlurColor = a.getColor(R.styleable.PagedViewIcon_blurColor, 0);
82 mHoloOutlineColor = a.getColor(R.styleable.PagedViewIcon_outlineColor, 0);
83 mCheckedBlurColor = a.getColor(R.styleable.PagedViewIcon_checkedBlurColor, 0);
84 mCheckedOutlineColor = a.getColor(R.styleable.PagedViewIcon_checkedOutlineColor, 0);
85 a.recycle();
Winson Chungb3347bb2010-08-19 14:51:28 -070086
87 if (sHolographicOutlineHelper == null) {
Winson Chung64a3cd42010-09-17 16:47:33 -070088 sHolographicOutlineHelper = new HolographicOutlineHelper();
Winson Chungb3347bb2010-08-19 14:51:28 -070089 }
90 mDrawableClipRect = new Rect();
91
92 setFocusable(true);
93 setBackgroundDrawable(null);
94 }
95
Winson Chung241c3b42010-08-25 16:53:03 -070096 public void applyFromApplicationInfo(ApplicationInfo info, PagedViewIconCache cache) {
97 mIconCache = cache;
98 mIconCacheKey = info;
99 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
100
101 setCompoundDrawablesWithIntrinsicBounds(null,
102 new FastBitmapDrawable(info.iconBitmap), null, null);
103 setText(info.title);
104 setTag(info);
105 }
106
107 public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager,
108 PagedViewIconCache cache) {
109 mIconCache = cache;
110 mIconCacheKey = info;
111 mHolographicOutline = mIconCache.getOutline(mIconCacheKey);
112
113 Drawable image = info.loadIcon(packageManager);
114 image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
115 setCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
116 setText(info.loadLabel(packageManager));
117 setTag(info);
118 }
119
Winson Chungb3347bb2010-08-19 14:51:28 -0700120 @Override
121 public void setAlpha(float alpha) {
122 final float viewAlpha = sHolographicOutlineHelper.viewAlphaInterpolator(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700123 final float holographicAlpha = sHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chungaffd7b42010-08-20 15:11:56 -0700124 mAlpha = (int) (viewAlpha * 255);
Winson Chungb3347bb2010-08-19 14:51:28 -0700125 mHolographicAlpha = (int) (holographicAlpha * 255);
Winson Chungb3347bb2010-08-19 14:51:28 -0700126 super.setAlpha(viewAlpha);
127 }
128
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700129 public void invalidateCheckedImage() {
130 if (mCheckedOutline != null) {
131 mCheckedOutline.recycle();
132 mCheckedOutline = null;
133 }
134 }
135
Winson Chungb3347bb2010-08-19 14:51:28 -0700136 @Override
137 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
138 super.onLayout(changed, left, top, right, bottom);
139
140 if (mHolographicOutline == null) {
Winson Chungb3347bb2010-08-19 14:51:28 -0700141 // update the clipping rect to be used in the holographic pass below
142 getDrawingRect(mDrawableClipRect);
143 mDrawableClipRect.bottom = getPaddingTop() + getCompoundPaddingTop();
144
145 // set a flag to indicate that we are going to draw the view at full alpha with the text
146 // clipped for the generation of the holographic icon
147 mIsHolographicUpdatePass = true;
148 mHolographicOutline = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
149 Bitmap.Config.ARGB_8888);
150 mHolographicOutlineCanvas = new Canvas(mHolographicOutline);
151 mHolographicOutlineCanvas.concat(getMatrix());
152 draw(mHolographicOutlineCanvas);
Winson Chung64a3cd42010-09-17 16:47:33 -0700153 sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(mHolographicOutline,
154 mHolographicOutlineCanvas, mHoloBlurColor, mHoloOutlineColor);
Winson Chungb3347bb2010-08-19 14:51:28 -0700155 mIsHolographicUpdatePass = false;
Winson Chung241c3b42010-08-25 16:53:03 -0700156 mIconCache.addOutline(mIconCacheKey, mHolographicOutline);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700157 mHolographicOutlineCanvas = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700158 }
159 }
160
Winson Chungb3347bb2010-08-19 14:51:28 -0700161 @Override
162 protected void onDraw(Canvas canvas) {
Winson Chungaffd7b42010-08-20 15:11:56 -0700163 // draw the view itself
Winson Chungb3347bb2010-08-19 14:51:28 -0700164 if (mIsHolographicUpdatePass) {
165 // only clip to the text view (restore its alpha so that we get a proper outline)
166 canvas.save();
167 canvas.clipRect(mDrawableClipRect, Op.REPLACE);
Winson Chungaffd7b42010-08-20 15:11:56 -0700168 final float alpha = getAlpha();
169 super.setAlpha(1.0f);
Winson Chungb3347bb2010-08-19 14:51:28 -0700170 super.onDraw(canvas);
Winson Chungaffd7b42010-08-20 15:11:56 -0700171 super.setAlpha(alpha);
Winson Chungb3347bb2010-08-19 14:51:28 -0700172 canvas.restore();
173 } else {
174 if (mAlpha > 0) {
175 super.onDraw(canvas);
176 }
177 }
178
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700179 // draw any blended overlays
180 if (!mIsHolographicUpdatePass) {
181 if (mCheckedOutline == null) {
182 if (mHolographicOutline != null && mHolographicAlpha > 0) {
183 mPaint.setAlpha(mHolographicAlpha);
184 canvas.drawBitmap(mHolographicOutline, 0, 0, mPaint);
185 }
186 } else {
187 mPaint.setAlpha(255);
188 canvas.drawBitmap(mCheckedOutline, 0, 0, mPaint);
189 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700190 }
191 }
Winson Chung03c568e2010-08-19 17:16:59 -0700192
193 @Override
194 public boolean isChecked() {
195 return mIsChecked;
196 }
197
198 @Override
199 public void setChecked(boolean checked) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700200 if (mIsChecked != checked) {
201 mIsChecked = checked;
202
203 if (mIsChecked) {
Winson Chung64a3cd42010-09-17 16:47:33 -0700204 // update the clipping rect to be used in the holographic pass below
205 getDrawingRect(mDrawableClipRect);
206 mDrawableClipRect.bottom = getPaddingTop() + getCompoundPaddingTop();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700207
208 // set a flag to indicate that we are going to draw the view at full alpha with the text
209 // clipped for the generation of the holographic icon
210 mIsHolographicUpdatePass = true;
211 mCheckedOutline = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
212 Bitmap.Config.ARGB_8888);
213 mHolographicOutlineCanvas = new Canvas(mCheckedOutline);
214 mHolographicOutlineCanvas.concat(getMatrix());
215 draw(mHolographicOutlineCanvas);
Winson Chung64a3cd42010-09-17 16:47:33 -0700216 sHolographicOutlineHelper.applyExpensiveOutlineWithBlur(mCheckedOutline,
217 mHolographicOutlineCanvas, mCheckedBlurColor, mCheckedOutlineColor);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700218 mIsHolographicUpdatePass = false;
219 mHolographicOutlineCanvas = null;
220 } else {
221 invalidateCheckedImage();
222 }
223
224 invalidate();
225 }
Winson Chung03c568e2010-08-19 17:16:59 -0700226 }
227
228 @Override
229 public void toggle() {
230 setChecked(!mIsChecked);
231 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700232}