blob: 45f58a4fd6453c813be3cc90e404c847c91cd631 [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();
Michael Jurka38b4f7c2010-12-14 16:46:39 -080058 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 Jurkaddd62e92011-02-16 17:49:14 -080068 private boolean mStayPressed;
69
Michael Jurka99b6a5b2011-01-07 15:37:17 -080070 private VisibilityChangedListener mOnVisibilityChangedListener;
71
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 public BubbleTextView(Context context) {
73 super(context);
74 init();
75 }
76
77 public BubbleTextView(Context context, AttributeSet attrs) {
78 super(context, attrs);
79 init();
80 }
81
82 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
83 super(context, attrs, defStyle);
84 init();
85 }
86
87 private void init() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 mBackground = getBackground();
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 Chungea359c62011-08-03 17:06:35 -070095 mFocusedOutlineColor = res.getColor(android.R.color.holo_blue_light);
96 mFocusedGlowColor = res.getColor(android.R.color.holo_blue_light);
97 mPressedOutlineColor = res.getColor(android.R.color.holo_blue_light);
98 mPressedGlowColor = res.getColor(android.R.color.holo_blue_light);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -080099
100 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
102
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800103 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
104 Bitmap b = info.getIcon(iconCache);
105
106 setCompoundDrawablesWithIntrinsicBounds(null,
107 new FastBitmapDrawable(b),
108 null, null);
109 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800110 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800111 }
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 @Override
114 protected boolean setFrame(int left, int top, int right, int bottom) {
115 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
116 mBackgroundSizeChanged = true;
117 }
118 return super.setFrame(left, top, right, bottom);
119 }
120
121 @Override
122 protected boolean verifyDrawable(Drawable who) {
123 return who == mBackground || super.verifyDrawable(who);
124 }
125
126 @Override
127 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800128 if (isPressed()) {
129 // In this case, we have already created the pressed outline on ACTION_DOWN,
130 // so we just need to do an invalidate to trigger draw
131 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800132 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800133 }
134 } else {
135 // Otherwise, either clear the pressed/focused background, or create a background
136 // for the focused state
137 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800138 if (!mStayPressed) {
139 mPressedOrFocusedBackground = null;
140 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800141 if (isFocused()) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800142 if (mLayout == null) {
143 // In some cases, we get focus before we have been layed out. Set the
144 // background to null so that it will get created when the view is drawn.
145 mPressedOrFocusedBackground = null;
146 } else {
147 mPressedOrFocusedBackground = createGlowingOutline(
148 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
149 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800150 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800151 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800152 }
153 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
154 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800155 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800156 }
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 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800167 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800168 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800169 * @param destCanvas the canvas to draw on
170 * @param padding the horizontal and vertical padding to use when drawing
171 */
172 private void drawWithPadding(Canvas destCanvas, int padding) {
173 final Rect clipRect = mTempRect;
174 getDrawingRect(clipRect);
175
176 // adjust the clip rect so that we don't include the text label
177 clipRect.bottom =
178 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
179
180 // Draw the View into the bitmap.
181 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
182 // they set scrollX and scrollY to large values to achieve centered text
183 destCanvas.save();
184 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
185 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800186 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800187 destCanvas.restore();
188 }
189
190 /**
191 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
192 * Responsibility for the bitmap is transferred to the caller.
193 */
194 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
195 final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
196 final Bitmap b = Bitmap.createBitmap(
197 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
198
199 canvas.setBitmap(b);
200 drawWithPadding(canvas, padding);
201 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700202 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800203
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
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800243 public void setVisibilityChangedListener(VisibilityChangedListener listener) {
244 mOnVisibilityChangedListener = listener;
245 }
246
247 @Override
248 protected void onVisibilityChanged(View changedView, int visibility) {
249 if (mOnVisibilityChangedListener != null) {
250 mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this);
251 }
252 super.onVisibilityChanged(changedView, visibility);
253 }
254
Michael Jurkaddd62e92011-02-16 17:49:14 -0800255 void setStayPressed(boolean stayPressed) {
256 mStayPressed = stayPressed;
257 if (!stayPressed) {
258 mPressedOrFocusedBackground = null;
259 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800260 setCellLayoutPressedOrFocusedIcon();
261 }
262
263 void setCellLayoutPressedOrFocusedIcon() {
Adam Cohen76fc0852011-06-17 13:26:23 -0700264 if (getParent() instanceof CellLayoutChildren) {
265 CellLayoutChildren parent = (CellLayoutChildren) getParent();
266 if (parent != null) {
267 CellLayout layout = (CellLayout) parent.getParent();
268 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
269 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700270 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800271 }
272
273 Bitmap getPressedOrFocusedBackground() {
274 return mPressedOrFocusedBackground;
275 }
276
277 int getPressedOrFocusedBackgroundPadding() {
278 return HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800279 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 @Override
282 public void draw(Canvas canvas) {
Michael Jurkabdb5c532011-02-01 15:05:06 -0800283 final Drawable background = mBackground;
284 if (background != null) {
285 final int scrollX = mScrollX;
286 final int scrollY = mScrollY;
Winson Chung88127032010-12-13 12:11:33 -0800287
Michael Jurkabdb5c532011-02-01 15:05:06 -0800288 if (mBackgroundSizeChanged) {
289 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
290 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800292
293 if ((scrollX | scrollY) == 0) {
294 background.draw(canvas);
295 } else {
296 canvas.translate(scrollX, scrollY);
297 background.draw(canvas);
298 canvas.translate(-scrollX, -scrollY);
299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800301 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800302 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800303 super.draw(canvas);
304 canvas.save(Canvas.CLIP_SAVE_FLAG);
305 canvas.clipRect(mScrollX, mScrollY + getExtendedPaddingTop(), mScrollX + getWidth(),
Winson Chung09693002011-02-03 23:56:47 -0800306 mScrollY + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800307 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800308 super.draw(canvas);
309 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400311
312 @Override
313 protected void onAttachedToWindow() {
314 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800315 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400316 }
317
318 @Override
319 protected void onDetachedFromWindow() {
320 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800321 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400322 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700323
324 @Override
325 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800326 if (mPrevAlpha != alpha) {
327 mPrevAlpha = alpha;
Winson Chung656d11c2010-11-29 17:15:47 -0800328 mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
Winson Chunge22a8e92010-11-12 13:40:58 -0800329 super.onSetAlpha(alpha);
330 }
331 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333}