blob: 2d049244301f7145ca842ebfc0bf9d71e1d35657 [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;
28import android.graphics.Region.Op;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080030import android.util.AttributeSet;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080031import android.view.MotionEvent;
Romain Guyedcce092010-03-04 13:03:17 -080032
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033/**
34 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
35 * because we want to make the bubble taller than the text and TextView's clip is
36 * too aggressive.
37 */
Michael Jurka67b2f6c2010-11-17 12:33:46 -080038public class BubbleTextView extends CacheableTextView {
Winson Chung656d11c2010-11-29 17:15:47 -080039 static final float CORNER_RADIUS = 4.0f;
Winson Chung88127032010-12-13 12:11:33 -080040 static final float SHADOW_LARGE_RADIUS = 4.0f;
41 static final float SHADOW_SMALL_RADIUS = 1.75f;
42 static final float SHADOW_Y_OFFSET = 2.0f;
43 static final int SHADOW_LARGE_COLOUR = 0xCC000000;
44 static final int SHADOW_SMALL_COLOUR = 0xBB000000;
Winson Chung656d11c2010-11-29 17:15:47 -080045 static final float PADDING_H = 8.0f;
46 static final float PADDING_V = 3.0f;
47
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048 private Paint mPaint;
Winson Chung656d11c2010-11-29 17:15:47 -080049 private float mBubbleColorAlpha;
Winson Chunge22a8e92010-11-12 13:40:58 -080050 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
Michael Jurka38b4f7c2010-12-14 16:46:39 -080052 private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
53 private final Canvas mTempCanvas = new Canvas();
54 private final Rect mTempRect = new Rect();
55 private final Paint mTempPaint = new Paint();
56 private boolean mDidInvalidateForPressedState;
57 private Bitmap mPressedOrFocusedBackground;
58 private int mFocusedOutlineColor;
59 private int mFocusedGlowColor;
60 private int mPressedOutlineColor;
61 private int mPressedGlowColor;
62
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 private boolean mBackgroundSizeChanged;
64 private Drawable mBackground;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
66 public BubbleTextView(Context context) {
67 super(context);
68 init();
69 }
70
71 public BubbleTextView(Context context, AttributeSet attrs) {
72 super(context, attrs);
73 init();
74 }
75
76 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
77 super(context, attrs, defStyle);
78 init();
79 }
80
81 private void init() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 mBackground = getBackground();
Winson Chung656d11c2010-11-29 17:15:47 -080083 setFocusable(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 setBackgroundDrawable(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Winson Chung656d11c2010-11-29 17:15:47 -080086 final Resources res = getContext().getResources();
87 int bubbleColor = res.getColor(R.color.bubble_dark_background);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Winson Chung656d11c2010-11-29 17:15:47 -080089 mPaint.setColor(bubbleColor);
90 mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -080091 mFocusedOutlineColor = res.getColor(R.color.workspace_item_focused_outline_color);
92 mFocusedGlowColor = res.getColor(R.color.workspace_item_focused_glow_color);
93 mPressedOutlineColor = res.getColor(R.color.workspace_item_pressed_outline_color);
94 mPressedGlowColor = res.getColor(R.color.workspace_item_pressed_glow_color);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 }
96
Winson Chung88127032010-12-13 12:11:33 -080097 protected int getCacheTopPadding() {
Winson Chung656d11c2010-11-29 17:15:47 -080098 return (int) PADDING_V;
99 }
Winson Chung88127032010-12-13 12:11:33 -0800100 protected int getCacheBottomPadding() {
101 return (int) (PADDING_V + SHADOW_LARGE_RADIUS + SHADOW_Y_OFFSET);
102 }
103 protected int getCacheLeftPadding() {
104 return (int) (PADDING_H + SHADOW_LARGE_RADIUS);
105 }
106 protected int getCacheRightPadding() {
107 return (int) (PADDING_H + SHADOW_LARGE_RADIUS);
Winson Chung656d11c2010-11-29 17:15:47 -0800108 }
109
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800110 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
111 Bitmap b = info.getIcon(iconCache);
112
113 setCompoundDrawablesWithIntrinsicBounds(null,
114 new FastBitmapDrawable(b),
115 null, null);
116 setText(info.title);
117 buildAndEnableCache();
118 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800119 }
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 @Override
122 protected boolean setFrame(int left, int top, int right, int bottom) {
123 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
124 mBackgroundSizeChanged = true;
125 }
126 return super.setFrame(left, top, right, bottom);
127 }
128
129 @Override
130 protected boolean verifyDrawable(Drawable who) {
131 return who == mBackground || super.verifyDrawable(who);
132 }
133
134 @Override
135 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800136 if (isPressed()) {
137 // In this case, we have already created the pressed outline on ACTION_DOWN,
138 // so we just need to do an invalidate to trigger draw
139 if (!mDidInvalidateForPressedState) {
140 invalidate();
141 }
142 } else {
143 // Otherwise, either clear the pressed/focused background, or create a background
144 // for the focused state
145 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
146 mPressedOrFocusedBackground = null;
147 if (isFocused()) {
148 mPressedOrFocusedBackground = createGlowingOutline(
149 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
150 invalidate();
151 }
152 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
153 if (!backgroundEmptyBefore && backgroundEmptyNow) {
154 invalidate();
155 }
156 }
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 Drawable d = mBackground;
159 if (d != null && d.isStateful()) {
160 d.setState(getDrawableState());
161 }
162 super.drawableStateChanged();
163 }
164
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800165 /**
166 * Draw the View v into the given Canvas.
167 *
168 * @param v the view to draw
169 * @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);
186 draw(destCanvas);
187 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);
202
203 return b;
204 }
205
206 @Override
207 public boolean onTouchEvent(MotionEvent event) {
208 // Call the superclass onTouchEvent first, because sometimes it changes the state to
209 // isPressed() on an ACTION_UP
210 boolean result = super.onTouchEvent(event);
211
212 switch (event.getAction()) {
213 case MotionEvent.ACTION_DOWN:
214 // So that the pressed outline is visible immediately when isPressed() is true,
215 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
216 // to create it)
217 if (mPressedOrFocusedBackground == null) {
218 mPressedOrFocusedBackground = createGlowingOutline(
219 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
220 }
221 // Invalidate so the pressed state is visible, or set a flag so we know that we
222 // have to call invalidate as soon as the state is "pressed"
223 if (isPressed()) {
224 mDidInvalidateForPressedState = true;
225 invalidate();
226 } else {
227 mDidInvalidateForPressedState = false;
228 }
229 break;
230 case MotionEvent.ACTION_CANCEL:
231 case MotionEvent.ACTION_UP:
232 // If we've touched down and up on an item, and it's still not "pressed", then
233 // destroy the pressed outline
234 if (!isPressed()) {
235 mPressedOrFocusedBackground = null;
236 }
237 break;
238 }
239 return result;
240 }
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 @Override
243 public void draw(Canvas canvas) {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800244 if (mPressedOrFocusedBackground != null && (isPressed() || isFocused())) {
245 canvas.drawBitmap(mPressedOrFocusedBackground,
246 mScrollX - HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2,
247 mScrollY - HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2,
248 mTempPaint);
249 }
Winson Chung88127032010-12-13 12:11:33 -0800250 if (isBuildingCache()) {
251 // We enhance the shadow by drawing the shadow twice
252 this.setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
253 super.draw(canvas);
254 this.setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
255 super.draw(canvas);
256 } else {
257 final Drawable background = mBackground;
258 if (background != null) {
259 final int scrollX = mScrollX;
260 final int scrollY = mScrollY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
Winson Chung88127032010-12-13 12:11:33 -0800262 if (mBackgroundSizeChanged) {
263 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
264 mBackgroundSizeChanged = false;
265 }
266
267 if ((scrollX | scrollY) == 0) {
268 background.draw(canvas);
269 } else {
270 canvas.translate(scrollX, scrollY);
271 background.draw(canvas);
272 canvas.translate(-scrollX, -scrollY);
273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 }
Winson Chung88127032010-12-13 12:11:33 -0800275 super.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278
279 @Override
280 protected void onAttachedToWindow() {
281 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800282 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 }
284
285 @Override
286 protected void onDetachedFromWindow() {
287 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800288 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400289 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700290
291 @Override
292 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800293 if (mPrevAlpha != alpha) {
294 mPrevAlpha = alpha;
Winson Chung656d11c2010-11-29 17:15:47 -0800295 mPaint.setAlpha((int) (alpha * mBubbleColorAlpha));
Winson Chunge22a8e92010-11-12 13:40:58 -0800296 super.onSetAlpha(alpha);
297 }
298 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300}