blob: d02f597b89d6be9c41b1cc7f3bc6be50035cf711 [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);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800100
101 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 }
103
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800104 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
105 Bitmap b = info.getIcon(iconCache);
106
107 setCompoundDrawablesWithIntrinsicBounds(null,
108 new FastBitmapDrawable(b),
109 null, null);
110 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800111 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800112 }
113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 @Override
115 protected boolean setFrame(int left, int top, int right, int bottom) {
116 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
117 mBackgroundSizeChanged = true;
118 }
119 return super.setFrame(left, top, right, bottom);
120 }
121
122 @Override
123 protected boolean verifyDrawable(Drawable who) {
124 return who == mBackground || super.verifyDrawable(who);
125 }
126
127 @Override
128 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800129 if (isPressed()) {
130 // In this case, we have already created the pressed outline on ACTION_DOWN,
131 // so we just need to do an invalidate to trigger draw
132 if (!mDidInvalidateForPressedState) {
133 invalidate();
134 }
135 } else {
136 // Otherwise, either clear the pressed/focused background, or create a background
137 // for the focused state
138 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
139 mPressedOrFocusedBackground = null;
140 if (isFocused()) {
141 mPressedOrFocusedBackground = createGlowingOutline(
142 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
143 invalidate();
144 }
145 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
146 if (!backgroundEmptyBefore && backgroundEmptyNow) {
147 invalidate();
148 }
149 }
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 Drawable d = mBackground;
152 if (d != null && d.isStateful()) {
153 d.setState(getDrawableState());
154 }
155 super.drawableStateChanged();
156 }
157
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800158 /**
159 * Draw the View v into the given Canvas.
160 *
161 * @param v the view to draw
162 * @param destCanvas the canvas to draw on
163 * @param padding the horizontal and vertical padding to use when drawing
164 */
165 private void drawWithPadding(Canvas destCanvas, int padding) {
166 final Rect clipRect = mTempRect;
167 getDrawingRect(clipRect);
168
169 // adjust the clip rect so that we don't include the text label
170 clipRect.bottom =
171 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
172
173 // Draw the View into the bitmap.
174 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
175 // they set scrollX and scrollY to large values to achieve centered text
176 destCanvas.save();
177 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
178 destCanvas.clipRect(clipRect, Op.REPLACE);
179 draw(destCanvas);
180 destCanvas.restore();
181 }
182
183 /**
184 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
185 * Responsibility for the bitmap is transferred to the caller.
186 */
187 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
188 final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
189 final Bitmap b = Bitmap.createBitmap(
190 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
191
192 canvas.setBitmap(b);
193 drawWithPadding(canvas, padding);
194 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
195
196 return b;
197 }
198
199 @Override
200 public boolean onTouchEvent(MotionEvent event) {
201 // Call the superclass onTouchEvent first, because sometimes it changes the state to
202 // isPressed() on an ACTION_UP
203 boolean result = super.onTouchEvent(event);
204
205 switch (event.getAction()) {
206 case MotionEvent.ACTION_DOWN:
207 // So that the pressed outline is visible immediately when isPressed() is true,
208 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
209 // to create it)
210 if (mPressedOrFocusedBackground == null) {
211 mPressedOrFocusedBackground = createGlowingOutline(
212 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
213 }
214 // Invalidate so the pressed state is visible, or set a flag so we know that we
215 // have to call invalidate as soon as the state is "pressed"
216 if (isPressed()) {
217 mDidInvalidateForPressedState = true;
218 invalidate();
219 } else {
220 mDidInvalidateForPressedState = false;
221 }
222 break;
223 case MotionEvent.ACTION_CANCEL:
224 case MotionEvent.ACTION_UP:
225 // If we've touched down and up on an item, and it's still not "pressed", then
226 // destroy the pressed outline
227 if (!isPressed()) {
228 mPressedOrFocusedBackground = null;
229 }
230 break;
231 }
232 return result;
233 }
234
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800235 public void setVisibilityChangedListener(VisibilityChangedListener listener) {
236 mOnVisibilityChangedListener = listener;
237 }
238
239 @Override
240 protected void onVisibilityChanged(View changedView, int visibility) {
241 if (mOnVisibilityChangedListener != null) {
242 mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this);
243 }
244 super.onVisibilityChanged(changedView, visibility);
245 }
246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 @Override
248 public void draw(Canvas canvas) {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800249 if (mPressedOrFocusedBackground != null && (isPressed() || isFocused())) {
Michael Jurka137142e2011-01-05 20:57:04 -0800250 // The blue glow can extend outside of our clip region, so we first temporarily expand
251 // the canvas's clip region
252 canvas.save(Canvas.CLIP_SAVE_FLAG);
253 int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
254 canvas.clipRect(-padding + mScrollX, -padding + mScrollY,
255 getWidth() + padding + mScrollX, getHeight() + padding + mScrollY,
256 Region.Op.REPLACE);
257 // draw blue glow
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800258 canvas.drawBitmap(mPressedOrFocusedBackground,
Michael Jurka137142e2011-01-05 20:57:04 -0800259 mScrollX - padding, mScrollY - padding, mTempPaint);
260 canvas.restore();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
Michael Jurkabdb5c532011-02-01 15:05:06 -0800263 final Drawable background = mBackground;
264 if (background != null) {
265 final int scrollX = mScrollX;
266 final int scrollY = mScrollY;
Winson Chung88127032010-12-13 12:11:33 -0800267
Michael Jurkabdb5c532011-02-01 15:05:06 -0800268 if (mBackgroundSizeChanged) {
269 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
270 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800272
273 if ((scrollX | scrollY) == 0) {
274 background.draw(canvas);
275 } else {
276 canvas.translate(scrollX, scrollY);
277 background.draw(canvas);
278 canvas.translate(-scrollX, -scrollY);
279 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800281 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800282 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800283 super.draw(canvas);
284 canvas.save(Canvas.CLIP_SAVE_FLAG);
285 canvas.clipRect(mScrollX, mScrollY + getExtendedPaddingTop(), mScrollX + getWidth(),
286 mScrollY + getHeight(), Region.Op.REPLACE);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800287 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800288 super.draw(canvas);
289 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291
292 @Override
293 protected void onAttachedToWindow() {
294 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800295 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296 }
297
298 @Override
299 protected void onDetachedFromWindow() {
300 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800301 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700303
304 @Override
305 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800306 if (mPrevAlpha != alpha) {
307 mPrevAlpha = alpha;
Winson Chung656d11c2010-11-29 17:15:47 -0800308 mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
Winson Chunge22a8e92010-11-12 13:40:58 -0800309 super.onSetAlpha(alpha);
310 }
311 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313}