blob: 3ef054637df12b7d80fe4c94aa589e0fc1f39a44 [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 Jurkaddd62e92011-02-16 17:49:14 -080069 private boolean mStayPressed;
70
Michael Jurka99b6a5b2011-01-07 15:37:17 -080071 private VisibilityChangedListener mOnVisibilityChangedListener;
72
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073 public BubbleTextView(Context context) {
74 super(context);
75 init();
76 }
77
78 public BubbleTextView(Context context, AttributeSet attrs) {
79 super(context, attrs);
80 init();
81 }
82
83 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
84 super(context, attrs, defStyle);
85 init();
86 }
87
88 private void init() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 mBackground = getBackground();
Winson Chung656d11c2010-11-29 17:15:47 -080090 setFocusable(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 setBackgroundDrawable(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Winson Chung656d11c2010-11-29 17:15:47 -080093 final Resources res = getContext().getResources();
94 int bubbleColor = res.getColor(R.color.bubble_dark_background);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Winson Chung656d11c2010-11-29 17:15:47 -080096 mPaint.setColor(bubbleColor);
97 mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -080098 mFocusedOutlineColor = res.getColor(R.color.workspace_item_focused_outline_color);
99 mFocusedGlowColor = res.getColor(R.color.workspace_item_focused_glow_color);
100 mPressedOutlineColor = res.getColor(R.color.workspace_item_pressed_outline_color);
101 mPressedGlowColor = res.getColor(R.color.workspace_item_pressed_glow_color);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800102
103 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 }
105
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800106 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
107 Bitmap b = info.getIcon(iconCache);
108
109 setCompoundDrawablesWithIntrinsicBounds(null,
110 new FastBitmapDrawable(b),
111 null, null);
112 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800113 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800114 }
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 @Override
117 protected boolean setFrame(int left, int top, int right, int bottom) {
118 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
119 mBackgroundSizeChanged = true;
120 }
121 return super.setFrame(left, top, right, bottom);
122 }
123
124 @Override
125 protected boolean verifyDrawable(Drawable who) {
126 return who == mBackground || super.verifyDrawable(who);
127 }
128
Michael Jurkaddd62e92011-02-16 17:49:14 -0800129 private void invalidatePressedOrFocusedBackground() {
130 int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
131 ((View)getParent()).invalidate(getLeft() - padding, getTop() - padding,
132 getRight() + padding, getBottom() + padding);
133 invalidate();
134 }
135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 @Override
137 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800138 if (isPressed()) {
139 // In this case, we have already created the pressed outline on ACTION_DOWN,
140 // so we just need to do an invalidate to trigger draw
141 if (!mDidInvalidateForPressedState) {
Michael Jurkaddd62e92011-02-16 17:49:14 -0800142 invalidatePressedOrFocusedBackground();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800143 }
144 } else {
145 // Otherwise, either clear the pressed/focused background, or create a background
146 // for the focused state
147 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800148 if (!mStayPressed) {
149 mPressedOrFocusedBackground = null;
150 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800151 if (isFocused()) {
152 mPressedOrFocusedBackground = createGlowingOutline(
153 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
Michael Jurkaddd62e92011-02-16 17:49:14 -0800154 mStayPressed = false;
155 invalidatePressedOrFocusedBackground();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800156 }
157 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
158 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Michael Jurkaddd62e92011-02-16 17:49:14 -0800159 invalidatePressedOrFocusedBackground();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800160 }
161 }
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 Drawable d = mBackground;
164 if (d != null && d.isStateful()) {
165 d.setState(getDrawableState());
166 }
167 super.drawableStateChanged();
168 }
169
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800170 /**
171 * Draw the View v into the given Canvas.
172 *
173 * @param v the view to draw
174 * @param destCanvas the canvas to draw on
175 * @param padding the horizontal and vertical padding to use when drawing
176 */
177 private void drawWithPadding(Canvas destCanvas, int padding) {
178 final Rect clipRect = mTempRect;
179 getDrawingRect(clipRect);
180
181 // adjust the clip rect so that we don't include the text label
182 clipRect.bottom =
183 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
184
185 // Draw the View into the bitmap.
186 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
187 // they set scrollX and scrollY to large values to achieve centered text
188 destCanvas.save();
189 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
190 destCanvas.clipRect(clipRect, Op.REPLACE);
191 draw(destCanvas);
192 destCanvas.restore();
193 }
194
195 /**
196 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
197 * Responsibility for the bitmap is transferred to the caller.
198 */
199 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
200 final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
201 final Bitmap b = Bitmap.createBitmap(
202 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
203
204 canvas.setBitmap(b);
205 drawWithPadding(canvas, padding);
206 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
207
208 return b;
209 }
210
211 @Override
212 public boolean onTouchEvent(MotionEvent event) {
213 // Call the superclass onTouchEvent first, because sometimes it changes the state to
214 // isPressed() on an ACTION_UP
215 boolean result = super.onTouchEvent(event);
216
217 switch (event.getAction()) {
218 case MotionEvent.ACTION_DOWN:
219 // So that the pressed outline is visible immediately when isPressed() is true,
220 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
221 // to create it)
222 if (mPressedOrFocusedBackground == null) {
223 mPressedOrFocusedBackground = createGlowingOutline(
224 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
225 }
226 // Invalidate so the pressed state is visible, or set a flag so we know that we
227 // have to call invalidate as soon as the state is "pressed"
228 if (isPressed()) {
229 mDidInvalidateForPressedState = true;
230 invalidate();
231 } else {
232 mDidInvalidateForPressedState = false;
233 }
234 break;
235 case MotionEvent.ACTION_CANCEL:
236 case MotionEvent.ACTION_UP:
237 // If we've touched down and up on an item, and it's still not "pressed", then
238 // destroy the pressed outline
239 if (!isPressed()) {
240 mPressedOrFocusedBackground = null;
241 }
242 break;
243 }
244 return result;
245 }
246
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800247 public void setVisibilityChangedListener(VisibilityChangedListener listener) {
248 mOnVisibilityChangedListener = listener;
249 }
250
251 @Override
252 protected void onVisibilityChanged(View changedView, int visibility) {
253 if (mOnVisibilityChangedListener != null) {
254 mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this);
255 }
256 super.onVisibilityChanged(changedView, visibility);
257 }
258
Michael Jurkaddd62e92011-02-16 17:49:14 -0800259 void setStayPressed(boolean stayPressed) {
260 mStayPressed = stayPressed;
261 if (!stayPressed) {
262 mPressedOrFocusedBackground = null;
263 }
264 invalidatePressedOrFocusedBackground();
265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 @Override
267 public void draw(Canvas canvas) {
Michael Jurkaddd62e92011-02-16 17:49:14 -0800268 if (mPressedOrFocusedBackground != null && (isPressed() || isFocused() || mStayPressed)) {
Michael Jurka137142e2011-01-05 20:57:04 -0800269 // The blue glow can extend outside of our clip region, so we first temporarily expand
270 // the canvas's clip region
271 canvas.save(Canvas.CLIP_SAVE_FLAG);
272 int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
273 canvas.clipRect(-padding + mScrollX, -padding + mScrollY,
274 getWidth() + padding + mScrollX, getHeight() + padding + mScrollY,
275 Region.Op.REPLACE);
276 // draw blue glow
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800277 canvas.drawBitmap(mPressedOrFocusedBackground,
Michael Jurka137142e2011-01-05 20:57:04 -0800278 mScrollX - padding, mScrollY - padding, mTempPaint);
279 canvas.restore();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800280 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
Michael Jurkabdb5c532011-02-01 15:05:06 -0800282 final Drawable background = mBackground;
283 if (background != null) {
284 final int scrollX = mScrollX;
285 final int scrollY = mScrollY;
Winson Chung88127032010-12-13 12:11:33 -0800286
Michael Jurkabdb5c532011-02-01 15:05:06 -0800287 if (mBackgroundSizeChanged) {
288 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
289 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800291
292 if ((scrollX | scrollY) == 0) {
293 background.draw(canvas);
294 } else {
295 canvas.translate(scrollX, scrollY);
296 background.draw(canvas);
297 canvas.translate(-scrollX, -scrollY);
298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800300 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800301 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800302 super.draw(canvas);
303 canvas.save(Canvas.CLIP_SAVE_FLAG);
304 canvas.clipRect(mScrollX, mScrollY + getExtendedPaddingTop(), mScrollX + getWidth(),
Winson Chung09693002011-02-03 23:56:47 -0800305 mScrollY + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800306 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800307 super.draw(canvas);
308 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400310
311 @Override
312 protected void onAttachedToWindow() {
313 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800314 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400315 }
316
317 @Override
318 protected void onDetachedFromWindow() {
319 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800320 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400321 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700322
323 @Override
324 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800325 if (mPrevAlpha != alpha) {
326 mPrevAlpha = alpha;
Winson Chung656d11c2010-11-29 17:15:47 -0800327 mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
Winson Chunge22a8e92010-11-12 13:40:58 -0800328 super.onSetAlpha(alpha);
329 }
330 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700331 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332}