blob: a842d78ac9f54d8ffdb6b542c240dcbb90136725 [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;
Michael Jurkabdb5c532011-02-01 15:05:06 -080034import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080035
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036/**
37 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
38 * because we want to make the bubble taller than the text and TextView's clip is
39 * too aggressive.
40 */
Michael Jurkabdb5c532011-02-01 15:05:06 -080041public class BubbleTextView extends TextView implements VisibilityChangedBroadcaster {
Winson Chung656d11c2010-11-29 17:15:47 -080042 static final float CORNER_RADIUS = 4.0f;
Winson Chung88127032010-12-13 12:11:33 -080043 static final float SHADOW_LARGE_RADIUS = 4.0f;
44 static final float SHADOW_SMALL_RADIUS = 1.75f;
45 static final float SHADOW_Y_OFFSET = 2.0f;
46 static final int SHADOW_LARGE_COLOUR = 0xCC000000;
47 static final int SHADOW_SMALL_COLOUR = 0xBB000000;
Winson Chung656d11c2010-11-29 17:15:47 -080048 static final float PADDING_H = 8.0f;
49 static final float PADDING_V = 3.0f;
50
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051 private Paint mPaint;
Winson Chung656d11c2010-11-29 17:15:47 -080052 private float mBubbleColorAlpha;
Winson Chunge22a8e92010-11-12 13:40:58 -080053 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
Michael Jurka38b4f7c2010-12-14 16:46:39 -080055 private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
56 private final Canvas mTempCanvas = new Canvas();
57 private final Rect mTempRect = new Rect();
58 private final Paint mTempPaint = new Paint();
59 private boolean mDidInvalidateForPressedState;
60 private Bitmap mPressedOrFocusedBackground;
61 private int mFocusedOutlineColor;
62 private int mFocusedGlowColor;
63 private int mPressedOutlineColor;
64 private int mPressedGlowColor;
65
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 private boolean mBackgroundSizeChanged;
67 private Drawable mBackground;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Michael Jurka99b6a5b2011-01-07 15:37:17 -080069 private VisibilityChangedListener mOnVisibilityChangedListener;
70
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 public BubbleTextView(Context context) {
72 super(context);
73 init();
74 }
75
76 public BubbleTextView(Context context, AttributeSet attrs) {
77 super(context, attrs);
78 init();
79 }
80
81 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
82 super(context, attrs, defStyle);
83 init();
84 }
85
86 private void init() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 mBackground = getBackground();
Winson Chung656d11c2010-11-29 17:15:47 -080088 setFocusable(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 setBackgroundDrawable(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Winson Chung656d11c2010-11-29 17:15:47 -080091 final Resources res = getContext().getResources();
92 int bubbleColor = res.getColor(R.color.bubble_dark_background);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Winson Chung656d11c2010-11-29 17:15:47 -080094 mPaint.setColor(bubbleColor);
95 mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -080096 mFocusedOutlineColor = res.getColor(R.color.workspace_item_focused_outline_color);
97 mFocusedGlowColor = res.getColor(R.color.workspace_item_focused_glow_color);
98 mPressedOutlineColor = res.getColor(R.color.workspace_item_pressed_outline_color);
99 mPressedGlowColor = res.getColor(R.color.workspace_item_pressed_glow_color);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 }
101
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800102 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
103 Bitmap b = info.getIcon(iconCache);
104
105 setCompoundDrawablesWithIntrinsicBounds(null,
106 new FastBitmapDrawable(b),
107 null, null);
108 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800109 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800110 }
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 @Override
113 protected boolean setFrame(int left, int top, int right, int bottom) {
114 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
115 mBackgroundSizeChanged = true;
116 }
117 return super.setFrame(left, top, right, bottom);
118 }
119
120 @Override
121 protected boolean verifyDrawable(Drawable who) {
122 return who == mBackground || super.verifyDrawable(who);
123 }
124
125 @Override
126 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800127 if (isPressed()) {
128 // In this case, we have already created the pressed outline on ACTION_DOWN,
129 // so we just need to do an invalidate to trigger draw
130 if (!mDidInvalidateForPressedState) {
131 invalidate();
132 }
133 } else {
134 // Otherwise, either clear the pressed/focused background, or create a background
135 // for the focused state
136 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
137 mPressedOrFocusedBackground = null;
138 if (isFocused()) {
139 mPressedOrFocusedBackground = createGlowingOutline(
140 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
141 invalidate();
142 }
143 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
144 if (!backgroundEmptyBefore && backgroundEmptyNow) {
145 invalidate();
146 }
147 }
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 Drawable d = mBackground;
150 if (d != null && d.isStateful()) {
151 d.setState(getDrawableState());
152 }
153 super.drawableStateChanged();
154 }
155
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800156 /**
157 * Draw the View v into the given Canvas.
158 *
159 * @param v the view to draw
160 * @param destCanvas the canvas to draw on
161 * @param padding the horizontal and vertical padding to use when drawing
162 */
163 private void drawWithPadding(Canvas destCanvas, int padding) {
164 final Rect clipRect = mTempRect;
165 getDrawingRect(clipRect);
166
167 // adjust the clip rect so that we don't include the text label
168 clipRect.bottom =
169 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
170
171 // Draw the View into the bitmap.
172 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
173 // they set scrollX and scrollY to large values to achieve centered text
174 destCanvas.save();
175 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
176 destCanvas.clipRect(clipRect, Op.REPLACE);
177 draw(destCanvas);
178 destCanvas.restore();
179 }
180
181 /**
182 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
183 * Responsibility for the bitmap is transferred to the caller.
184 */
185 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
186 final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
187 final Bitmap b = Bitmap.createBitmap(
188 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
189
190 canvas.setBitmap(b);
191 drawWithPadding(canvas, padding);
192 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
193
194 return b;
195 }
196
197 @Override
198 public boolean onTouchEvent(MotionEvent event) {
199 // Call the superclass onTouchEvent first, because sometimes it changes the state to
200 // isPressed() on an ACTION_UP
201 boolean result = super.onTouchEvent(event);
202
203 switch (event.getAction()) {
204 case MotionEvent.ACTION_DOWN:
205 // So that the pressed outline is visible immediately when isPressed() is true,
206 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
207 // to create it)
208 if (mPressedOrFocusedBackground == null) {
209 mPressedOrFocusedBackground = createGlowingOutline(
210 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
211 }
212 // Invalidate so the pressed state is visible, or set a flag so we know that we
213 // have to call invalidate as soon as the state is "pressed"
214 if (isPressed()) {
215 mDidInvalidateForPressedState = true;
216 invalidate();
217 } else {
218 mDidInvalidateForPressedState = false;
219 }
220 break;
221 case MotionEvent.ACTION_CANCEL:
222 case MotionEvent.ACTION_UP:
223 // If we've touched down and up on an item, and it's still not "pressed", then
224 // destroy the pressed outline
225 if (!isPressed()) {
226 mPressedOrFocusedBackground = null;
227 }
228 break;
229 }
230 return result;
231 }
232
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800233 public void setVisibilityChangedListener(VisibilityChangedListener listener) {
234 mOnVisibilityChangedListener = listener;
235 }
236
237 @Override
238 protected void onVisibilityChanged(View changedView, int visibility) {
239 if (mOnVisibilityChangedListener != null) {
240 mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this);
241 }
242 super.onVisibilityChanged(changedView, visibility);
243 }
244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 @Override
246 public void draw(Canvas canvas) {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247 if (mPressedOrFocusedBackground != null && (isPressed() || isFocused())) {
Michael Jurka137142e2011-01-05 20:57:04 -0800248 // The blue glow can extend outside of our clip region, so we first temporarily expand
249 // the canvas's clip region
250 canvas.save(Canvas.CLIP_SAVE_FLAG);
251 int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
252 canvas.clipRect(-padding + mScrollX, -padding + mScrollY,
253 getWidth() + padding + mScrollX, getHeight() + padding + mScrollY,
254 Region.Op.REPLACE);
255 // draw blue glow
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800256 canvas.drawBitmap(mPressedOrFocusedBackground,
Michael Jurka137142e2011-01-05 20:57:04 -0800257 mScrollX - padding, mScrollY - padding, mTempPaint);
258 canvas.restore();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800259 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurkabdb5c532011-02-01 15:05:06 -0800261 final Drawable background = mBackground;
262 if (background != null) {
263 final int scrollX = mScrollX;
264 final int scrollY = mScrollY;
Winson Chung88127032010-12-13 12:11:33 -0800265
Michael Jurkabdb5c532011-02-01 15:05:06 -0800266 if (mBackgroundSizeChanged) {
267 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
268 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800270
271 if ((scrollX | scrollY) == 0) {
272 background.draw(canvas);
273 } else {
274 canvas.translate(scrollX, scrollY);
275 background.draw(canvas);
276 canvas.translate(-scrollX, -scrollY);
277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800279 // We enhance the shadow by drawing the shadow twice
280 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
281 super.draw(canvas);
282 canvas.save(Canvas.CLIP_SAVE_FLAG);
283 canvas.clipRect(mScrollX, mScrollY + getExtendedPaddingTop(), mScrollX + getWidth(),
284 mScrollY + getHeight(), Region.Op.REPLACE);
285 setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
286 super.draw(canvas);
287 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400289
290 @Override
291 protected void onAttachedToWindow() {
292 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800293 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294 }
295
296 @Override
297 protected void onDetachedFromWindow() {
298 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800299 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400300 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700301
302 @Override
303 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800304 if (mPrevAlpha != alpha) {
305 mPrevAlpha = alpha;
Winson Chung656d11c2010-11-29 17:15:47 -0800306 mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
Winson Chunge22a8e92010-11-12 13:40:58 -0800307 super.onSetAlpha(alpha);
308 }
309 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311}