blob: 348fe34b99780abadbabc44e240952f10c04551c [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Michael Jurka38b4f7c2010-12-14 16:46:39 -080019import com.android.launcher.R;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.Context;
Winson Chung656d11c2010-11-29 17:15:47 -080022import android.content.res.Resources;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080023import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Canvas;
Winson Chung656d11c2010-11-29 17:15:47 -080025import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.Paint;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080027import android.graphics.Rect;
Michael Jurka137142e2011-01-05 20:57:04 -080028import android.graphics.Region;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080029import android.graphics.Region.Op;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080031import android.util.AttributeSet;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080032import android.view.MotionEvent;
Romain Guyedcce092010-03-04 13:03:17 -080033
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034/**
35 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
36 * because we want to make the bubble taller than the text and TextView's clip is
37 * too aggressive.
38 */
Michael Jurka67b2f6c2010-11-17 12:33:46 -080039public class BubbleTextView extends CacheableTextView {
Winson Chung656d11c2010-11-29 17:15:47 -080040 static final float CORNER_RADIUS = 4.0f;
Winson Chung88127032010-12-13 12:11:33 -080041 static final float SHADOW_LARGE_RADIUS = 4.0f;
42 static final float SHADOW_SMALL_RADIUS = 1.75f;
43 static final float SHADOW_Y_OFFSET = 2.0f;
44 static final int SHADOW_LARGE_COLOUR = 0xCC000000;
45 static final int SHADOW_SMALL_COLOUR = 0xBB000000;
Winson Chung656d11c2010-11-29 17:15:47 -080046 static final float PADDING_H = 8.0f;
47 static final float PADDING_V = 3.0f;
48
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 private Paint mPaint;
Winson Chung656d11c2010-11-29 17:15:47 -080050 private float mBubbleColorAlpha;
Winson Chunge22a8e92010-11-12 13:40:58 -080051 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052
Michael Jurka38b4f7c2010-12-14 16:46:39 -080053 private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
54 private final Canvas mTempCanvas = new Canvas();
55 private final Rect mTempRect = new Rect();
56 private final Paint mTempPaint = new Paint();
57 private boolean mDidInvalidateForPressedState;
58 private Bitmap mPressedOrFocusedBackground;
59 private int mFocusedOutlineColor;
60 private int mFocusedGlowColor;
61 private int mPressedOutlineColor;
62 private int mPressedGlowColor;
63
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 private boolean mBackgroundSizeChanged;
65 private Drawable mBackground;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
67 public BubbleTextView(Context context) {
68 super(context);
69 init();
70 }
71
72 public BubbleTextView(Context context, AttributeSet attrs) {
73 super(context, attrs);
74 init();
75 }
76
77 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
78 super(context, attrs, defStyle);
79 init();
80 }
81
82 private void init() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 mBackground = getBackground();
Winson Chung656d11c2010-11-29 17:15:47 -080084 setFocusable(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 setBackgroundDrawable(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Winson Chung656d11c2010-11-29 17:15:47 -080087 final Resources res = getContext().getResources();
88 int bubbleColor = res.getColor(R.color.bubble_dark_background);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Winson Chung656d11c2010-11-29 17:15:47 -080090 mPaint.setColor(bubbleColor);
91 mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -080092 mFocusedOutlineColor = res.getColor(R.color.workspace_item_focused_outline_color);
93 mFocusedGlowColor = res.getColor(R.color.workspace_item_focused_glow_color);
94 mPressedOutlineColor = res.getColor(R.color.workspace_item_pressed_outline_color);
95 mPressedGlowColor = res.getColor(R.color.workspace_item_pressed_glow_color);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 }
97
Winson Chung88127032010-12-13 12:11:33 -080098 protected int getCacheTopPadding() {
Winson Chung656d11c2010-11-29 17:15:47 -080099 return (int) PADDING_V;
100 }
Winson Chung88127032010-12-13 12:11:33 -0800101 protected int getCacheBottomPadding() {
102 return (int) (PADDING_V + SHADOW_LARGE_RADIUS + SHADOW_Y_OFFSET);
103 }
104 protected int getCacheLeftPadding() {
105 return (int) (PADDING_H + SHADOW_LARGE_RADIUS);
106 }
107 protected int getCacheRightPadding() {
108 return (int) (PADDING_H + SHADOW_LARGE_RADIUS);
Winson Chung656d11c2010-11-29 17:15:47 -0800109 }
110
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800111 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
112 Bitmap b = info.getIcon(iconCache);
113
114 setCompoundDrawablesWithIntrinsicBounds(null,
115 new FastBitmapDrawable(b),
116 null, null);
117 setText(info.title);
118 buildAndEnableCache();
119 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800120 }
121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 @Override
123 protected boolean setFrame(int left, int top, int right, int bottom) {
124 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
125 mBackgroundSizeChanged = true;
126 }
127 return super.setFrame(left, top, right, bottom);
128 }
129
130 @Override
131 protected boolean verifyDrawable(Drawable who) {
132 return who == mBackground || super.verifyDrawable(who);
133 }
134
135 @Override
136 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800137 if (isPressed()) {
138 // In this case, we have already created the pressed outline on ACTION_DOWN,
139 // so we just need to do an invalidate to trigger draw
140 if (!mDidInvalidateForPressedState) {
141 invalidate();
142 }
143 } else {
144 // Otherwise, either clear the pressed/focused background, or create a background
145 // for the focused state
146 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
147 mPressedOrFocusedBackground = null;
148 if (isFocused()) {
149 mPressedOrFocusedBackground = createGlowingOutline(
150 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
151 invalidate();
152 }
153 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
154 if (!backgroundEmptyBefore && backgroundEmptyNow) {
155 invalidate();
156 }
157 }
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 Drawable d = mBackground;
160 if (d != null && d.isStateful()) {
161 d.setState(getDrawableState());
162 }
163 super.drawableStateChanged();
164 }
165
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800166 /**
167 * Draw the View v into the given Canvas.
168 *
169 * @param v the view to draw
170 * @param destCanvas the canvas to draw on
171 * @param padding the horizontal and vertical padding to use when drawing
172 */
173 private void drawWithPadding(Canvas destCanvas, int padding) {
174 final Rect clipRect = mTempRect;
175 getDrawingRect(clipRect);
176
177 // adjust the clip rect so that we don't include the text label
178 clipRect.bottom =
179 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
180
181 // Draw the View into the bitmap.
182 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
183 // they set scrollX and scrollY to large values to achieve centered text
184 destCanvas.save();
185 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
186 destCanvas.clipRect(clipRect, Op.REPLACE);
187 draw(destCanvas);
188 destCanvas.restore();
189 }
190
191 /**
192 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
193 * Responsibility for the bitmap is transferred to the caller.
194 */
195 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
196 final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
197 final Bitmap b = Bitmap.createBitmap(
198 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
199
200 canvas.setBitmap(b);
201 drawWithPadding(canvas, padding);
202 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
203
204 return b;
205 }
206
207 @Override
208 public boolean onTouchEvent(MotionEvent event) {
209 // Call the superclass onTouchEvent first, because sometimes it changes the state to
210 // isPressed() on an ACTION_UP
211 boolean result = super.onTouchEvent(event);
212
213 switch (event.getAction()) {
214 case MotionEvent.ACTION_DOWN:
215 // So that the pressed outline is visible immediately when isPressed() is true,
216 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
217 // to create it)
218 if (mPressedOrFocusedBackground == null) {
219 mPressedOrFocusedBackground = createGlowingOutline(
220 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
221 }
222 // Invalidate so the pressed state is visible, or set a flag so we know that we
223 // have to call invalidate as soon as the state is "pressed"
224 if (isPressed()) {
225 mDidInvalidateForPressedState = true;
226 invalidate();
227 } else {
228 mDidInvalidateForPressedState = false;
229 }
230 break;
231 case MotionEvent.ACTION_CANCEL:
232 case MotionEvent.ACTION_UP:
233 // If we've touched down and up on an item, and it's still not "pressed", then
234 // destroy the pressed outline
235 if (!isPressed()) {
236 mPressedOrFocusedBackground = null;
237 }
238 break;
239 }
240 return result;
241 }
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 @Override
244 public void draw(Canvas canvas) {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800245 if (mPressedOrFocusedBackground != null && (isPressed() || isFocused())) {
Michael Jurka137142e2011-01-05 20:57:04 -0800246 // The blue glow can extend outside of our clip region, so we first temporarily expand
247 // the canvas's clip region
248 canvas.save(Canvas.CLIP_SAVE_FLAG);
249 int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
250 canvas.clipRect(-padding + mScrollX, -padding + mScrollY,
251 getWidth() + padding + mScrollX, getHeight() + padding + mScrollY,
252 Region.Op.REPLACE);
253 // draw blue glow
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800254 canvas.drawBitmap(mPressedOrFocusedBackground,
Michael Jurka137142e2011-01-05 20:57:04 -0800255 mScrollX - padding, mScrollY - padding, mTempPaint);
256 canvas.restore();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800257 }
Winson Chung88127032010-12-13 12:11:33 -0800258 if (isBuildingCache()) {
259 // We enhance the shadow by drawing the shadow twice
260 this.setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
261 super.draw(canvas);
262 this.setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
263 super.draw(canvas);
264 } else {
265 final Drawable background = mBackground;
266 if (background != null) {
267 final int scrollX = mScrollX;
268 final int scrollY = mScrollY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
Winson Chung88127032010-12-13 12:11:33 -0800270 if (mBackgroundSizeChanged) {
271 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
272 mBackgroundSizeChanged = false;
273 }
274
275 if ((scrollX | scrollY) == 0) {
276 background.draw(canvas);
277 } else {
278 canvas.translate(scrollX, scrollY);
279 background.draw(canvas);
280 canvas.translate(-scrollX, -scrollY);
281 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 }
Winson Chung88127032010-12-13 12:11:33 -0800283 super.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286
287 @Override
288 protected void onAttachedToWindow() {
289 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800290 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 }
292
293 @Override
294 protected void onDetachedFromWindow() {
295 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800296 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400297 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700298
299 @Override
300 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800301 if (mPrevAlpha != alpha) {
302 mPrevAlpha = alpha;
Winson Chung656d11c2010-11-29 17:15:47 -0800303 mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
Winson Chunge22a8e92010-11-12 13:40:58 -0800304 super.onSetAlpha(alpha);
305 }
306 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308}