blob: 4d1dbf88c9c611217d1b49c7af0db241bfb0cd2c [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;
Michael Jurka99b6a5b2011-01-07 15:37:17 -080033import android.view.View;
Romain Guyedcce092010-03-04 13:03:17 -080034
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035/**
36 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
37 * because we want to make the bubble taller than the text and TextView's clip is
38 * too aggressive.
39 */
Michael Jurka99b6a5b2011-01-07 15:37:17 -080040public class BubbleTextView extends CacheableTextView implements VisibilityChangedBroadcaster {
Winson Chung656d11c2010-11-29 17:15:47 -080041 static final float CORNER_RADIUS = 4.0f;
Winson Chung88127032010-12-13 12:11:33 -080042 static final float SHADOW_LARGE_RADIUS = 4.0f;
43 static final float SHADOW_SMALL_RADIUS = 1.75f;
44 static final float SHADOW_Y_OFFSET = 2.0f;
45 static final int SHADOW_LARGE_COLOUR = 0xCC000000;
46 static final int SHADOW_SMALL_COLOUR = 0xBB000000;
Winson Chung656d11c2010-11-29 17:15:47 -080047 static final float PADDING_H = 8.0f;
48 static final float PADDING_V = 3.0f;
49
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 private Paint mPaint;
Winson Chung656d11c2010-11-29 17:15:47 -080051 private float mBubbleColorAlpha;
Winson Chunge22a8e92010-11-12 13:40:58 -080052 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
Michael Jurka38b4f7c2010-12-14 16:46:39 -080054 private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
55 private final Canvas mTempCanvas = new Canvas();
56 private final Rect mTempRect = new Rect();
57 private final Paint mTempPaint = new Paint();
58 private boolean mDidInvalidateForPressedState;
59 private Bitmap mPressedOrFocusedBackground;
60 private int mFocusedOutlineColor;
61 private int mFocusedGlowColor;
62 private int mPressedOutlineColor;
63 private int mPressedGlowColor;
64
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 private boolean mBackgroundSizeChanged;
66 private Drawable mBackground;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
Michael Jurka99b6a5b2011-01-07 15:37:17 -080068 private VisibilityChangedListener mOnVisibilityChangedListener;
69
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 public BubbleTextView(Context context) {
71 super(context);
72 init();
73 }
74
75 public BubbleTextView(Context context, AttributeSet attrs) {
76 super(context, attrs);
77 init();
78 }
79
80 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
81 super(context, attrs, defStyle);
82 init();
83 }
84
85 private void init() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 mBackground = getBackground();
Winson Chung656d11c2010-11-29 17:15:47 -080087 setFocusable(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 setBackgroundDrawable(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Winson Chung656d11c2010-11-29 17:15:47 -080090 final Resources res = getContext().getResources();
91 int bubbleColor = res.getColor(R.color.bubble_dark_background);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Winson Chung656d11c2010-11-29 17:15:47 -080093 mPaint.setColor(bubbleColor);
94 mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -080095 mFocusedOutlineColor = res.getColor(R.color.workspace_item_focused_outline_color);
96 mFocusedGlowColor = res.getColor(R.color.workspace_item_focused_glow_color);
97 mPressedOutlineColor = res.getColor(R.color.workspace_item_pressed_outline_color);
98 mPressedGlowColor = res.getColor(R.color.workspace_item_pressed_glow_color);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 }
100
Winson Chung88127032010-12-13 12:11:33 -0800101 protected int getCacheTopPadding() {
Winson Chung656d11c2010-11-29 17:15:47 -0800102 return (int) PADDING_V;
103 }
Winson Chung88127032010-12-13 12:11:33 -0800104 protected int getCacheBottomPadding() {
105 return (int) (PADDING_V + SHADOW_LARGE_RADIUS + SHADOW_Y_OFFSET);
106 }
107 protected int getCacheLeftPadding() {
108 return (int) (PADDING_H + SHADOW_LARGE_RADIUS);
109 }
110 protected int getCacheRightPadding() {
111 return (int) (PADDING_H + SHADOW_LARGE_RADIUS);
Winson Chung656d11c2010-11-29 17:15:47 -0800112 }
113
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800114 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
115 Bitmap b = info.getIcon(iconCache);
116
117 setCompoundDrawablesWithIntrinsicBounds(null,
118 new FastBitmapDrawable(b),
119 null, null);
120 setText(info.title);
121 buildAndEnableCache();
122 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800123 }
124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 @Override
126 protected boolean setFrame(int left, int top, int right, int bottom) {
127 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
128 mBackgroundSizeChanged = true;
129 }
130 return super.setFrame(left, top, right, bottom);
131 }
132
133 @Override
134 protected boolean verifyDrawable(Drawable who) {
135 return who == mBackground || super.verifyDrawable(who);
136 }
137
138 @Override
139 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800140 if (isPressed()) {
141 // In this case, we have already created the pressed outline on ACTION_DOWN,
142 // so we just need to do an invalidate to trigger draw
143 if (!mDidInvalidateForPressedState) {
144 invalidate();
145 }
146 } else {
147 // Otherwise, either clear the pressed/focused background, or create a background
148 // for the focused state
149 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
150 mPressedOrFocusedBackground = null;
151 if (isFocused()) {
152 mPressedOrFocusedBackground = createGlowingOutline(
153 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
154 invalidate();
155 }
156 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
157 if (!backgroundEmptyBefore && backgroundEmptyNow) {
158 invalidate();
159 }
160 }
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 Drawable d = mBackground;
163 if (d != null && d.isStateful()) {
164 d.setState(getDrawableState());
165 }
166 super.drawableStateChanged();
167 }
168
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800169 /**
170 * Draw the View v into the given Canvas.
171 *
172 * @param v the view to draw
173 * @param destCanvas the canvas to draw on
174 * @param padding the horizontal and vertical padding to use when drawing
175 */
176 private void drawWithPadding(Canvas destCanvas, int padding) {
177 final Rect clipRect = mTempRect;
178 getDrawingRect(clipRect);
179
180 // adjust the clip rect so that we don't include the text label
181 clipRect.bottom =
182 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
183
184 // Draw the View into the bitmap.
185 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
186 // they set scrollX and scrollY to large values to achieve centered text
187 destCanvas.save();
188 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
189 destCanvas.clipRect(clipRect, Op.REPLACE);
190 draw(destCanvas);
191 destCanvas.restore();
192 }
193
194 /**
195 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
196 * Responsibility for the bitmap is transferred to the caller.
197 */
198 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
199 final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
200 final Bitmap b = Bitmap.createBitmap(
201 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
202
203 canvas.setBitmap(b);
204 drawWithPadding(canvas, padding);
205 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
206
207 return b;
208 }
209
210 @Override
211 public boolean onTouchEvent(MotionEvent event) {
212 // Call the superclass onTouchEvent first, because sometimes it changes the state to
213 // isPressed() on an ACTION_UP
214 boolean result = super.onTouchEvent(event);
215
216 switch (event.getAction()) {
217 case MotionEvent.ACTION_DOWN:
218 // So that the pressed outline is visible immediately when isPressed() is true,
219 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
220 // to create it)
221 if (mPressedOrFocusedBackground == null) {
222 mPressedOrFocusedBackground = createGlowingOutline(
223 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
224 }
225 // Invalidate so the pressed state is visible, or set a flag so we know that we
226 // have to call invalidate as soon as the state is "pressed"
227 if (isPressed()) {
228 mDidInvalidateForPressedState = true;
229 invalidate();
230 } else {
231 mDidInvalidateForPressedState = false;
232 }
233 break;
234 case MotionEvent.ACTION_CANCEL:
235 case MotionEvent.ACTION_UP:
236 // If we've touched down and up on an item, and it's still not "pressed", then
237 // destroy the pressed outline
238 if (!isPressed()) {
239 mPressedOrFocusedBackground = null;
240 }
241 break;
242 }
243 return result;
244 }
245
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800246 public void setVisibilityChangedListener(VisibilityChangedListener listener) {
247 mOnVisibilityChangedListener = listener;
248 }
249
250 @Override
251 protected void onVisibilityChanged(View changedView, int visibility) {
252 if (mOnVisibilityChangedListener != null) {
253 mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this);
254 }
255 super.onVisibilityChanged(changedView, visibility);
256 }
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 @Override
259 public void draw(Canvas canvas) {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800260 if (mPressedOrFocusedBackground != null && (isPressed() || isFocused())) {
Michael Jurka137142e2011-01-05 20:57:04 -0800261 // The blue glow can extend outside of our clip region, so we first temporarily expand
262 // the canvas's clip region
263 canvas.save(Canvas.CLIP_SAVE_FLAG);
264 int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
265 canvas.clipRect(-padding + mScrollX, -padding + mScrollY,
266 getWidth() + padding + mScrollX, getHeight() + padding + mScrollY,
267 Region.Op.REPLACE);
268 // draw blue glow
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800269 canvas.drawBitmap(mPressedOrFocusedBackground,
Michael Jurka137142e2011-01-05 20:57:04 -0800270 mScrollX - padding, mScrollY - padding, mTempPaint);
271 canvas.restore();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800272 }
Winson Chung88127032010-12-13 12:11:33 -0800273 if (isBuildingCache()) {
274 // We enhance the shadow by drawing the shadow twice
275 this.setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
276 super.draw(canvas);
277 this.setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
278 super.draw(canvas);
279 } else {
280 final Drawable background = mBackground;
281 if (background != null) {
282 final int scrollX = mScrollX;
283 final int scrollY = mScrollY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
Winson Chung88127032010-12-13 12:11:33 -0800285 if (mBackgroundSizeChanged) {
286 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
287 mBackgroundSizeChanged = false;
288 }
289
290 if ((scrollX | scrollY) == 0) {
291 background.draw(canvas);
292 } else {
293 canvas.translate(scrollX, scrollY);
294 background.draw(canvas);
295 canvas.translate(-scrollX, -scrollY);
296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 }
Winson Chung88127032010-12-13 12:11:33 -0800298 super.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301
302 @Override
303 protected void onAttachedToWindow() {
304 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800305 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400306 }
307
308 @Override
309 protected void onDetachedFromWindow() {
310 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800311 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400312 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700313
314 @Override
315 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800316 if (mPrevAlpha != alpha) {
317 mPrevAlpha = alpha;
Winson Chung656d11c2010-11-29 17:15:47 -0800318 mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
Winson Chunge22a8e92010-11-12 13:40:58 -0800319 super.onSetAlpha(alpha);
320 }
321 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323}