blob: ddc4b9fa09d707db90223357dcdc1ccc582f2555 [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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019import android.content.Context;
Winson Chung656d11c2010-11-29 17:15:47 -080020import android.content.res.Resources;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080021import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.graphics.Canvas;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080023import android.graphics.Rect;
Michael Jurka137142e2011-01-05 20:57:04 -080024import android.graphics.Region;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080025import android.graphics.Region.Op;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080027import android.util.AttributeSet;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080028import android.view.MotionEvent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080029import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080030
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031/**
32 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
33 * because we want to make the bubble taller than the text and TextView's clip is
34 * too aggressive.
35 */
Michael Jurka08ee7702011-08-11 16:53:35 -070036public class BubbleTextView extends TextView {
Winson Chung656d11c2010-11-29 17:15:47 -080037 static final float CORNER_RADIUS = 4.0f;
Winson Chung88127032010-12-13 12:11:33 -080038 static final float SHADOW_LARGE_RADIUS = 4.0f;
39 static final float SHADOW_SMALL_RADIUS = 1.75f;
40 static final float SHADOW_Y_OFFSET = 2.0f;
Winson Chungc7aef8c2011-09-15 18:53:04 -070041 static final int SHADOW_LARGE_COLOUR = 0xDD000000;
42 static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080043 static final float PADDING_H = 8.0f;
44 static final float PADDING_V = 3.0f;
45
Winson Chunge22a8e92010-11-12 13:40:58 -080046 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
Michael Jurka38b4f7c2010-12-14 16:46:39 -080048 private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
49 private final Canvas mTempCanvas = new Canvas();
50 private final Rect mTempRect = new Rect();
Michael Jurka38b4f7c2010-12-14 16:46:39 -080051 private boolean mDidInvalidateForPressedState;
52 private Bitmap mPressedOrFocusedBackground;
53 private int mFocusedOutlineColor;
54 private int mFocusedGlowColor;
55 private int mPressedOutlineColor;
56 private int mPressedGlowColor;
57
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 private boolean mBackgroundSizeChanged;
59 private Drawable mBackground;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
Michael Jurkaddd62e92011-02-16 17:49:14 -080061 private boolean mStayPressed;
Winson Chung88f33452012-02-23 15:23:44 -080062 private CheckLongPressHelper mLongPressHelper;
Michael Jurkaddd62e92011-02-16 17:49:14 -080063
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 public BubbleTextView(Context context) {
65 super(context);
66 init();
67 }
68
69 public BubbleTextView(Context context, AttributeSet attrs) {
70 super(context, attrs);
71 init();
72 }
73
74 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
75 super(context, attrs, defStyle);
76 init();
77 }
78
79 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -080080 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 mBackground = getBackground();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082
Winson Chung656d11c2010-11-29 17:15:47 -080083 final Resources res = getContext().getResources();
Winson Chung7d3810d2011-10-07 13:11:56 -070084 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor =
85 res.getColor(android.R.color.holo_blue_light);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -080086
87 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 }
89
Michael Jurka67b2f6c2010-11-17 12:33:46 -080090 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
91 Bitmap b = info.getIcon(iconCache);
92
93 setCompoundDrawablesWithIntrinsicBounds(null,
94 new FastBitmapDrawable(b),
95 null, null);
96 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -080097 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -080098 }
99
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 @Override
101 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700102 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 mBackgroundSizeChanged = true;
104 }
105 return super.setFrame(left, top, right, bottom);
106 }
107
108 @Override
109 protected boolean verifyDrawable(Drawable who) {
110 return who == mBackground || super.verifyDrawable(who);
111 }
112
113 @Override
114 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800115 if (isPressed()) {
116 // In this case, we have already created the pressed outline on ACTION_DOWN,
117 // so we just need to do an invalidate to trigger draw
118 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800119 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800120 }
121 } else {
122 // Otherwise, either clear the pressed/focused background, or create a background
123 // for the focused state
124 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800125 if (!mStayPressed) {
126 mPressedOrFocusedBackground = null;
127 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800128 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800129 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800130 // In some cases, we get focus before we have been layed out. Set the
131 // background to null so that it will get created when the view is drawn.
132 mPressedOrFocusedBackground = null;
133 } else {
134 mPressedOrFocusedBackground = createGlowingOutline(
135 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
136 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800137 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800138 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800139 }
140 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
141 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800142 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800143 }
144 }
145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 Drawable d = mBackground;
147 if (d != null && d.isStateful()) {
148 d.setState(getDrawableState());
149 }
150 super.drawableStateChanged();
151 }
152
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800153 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800154 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800155 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800156 * @param destCanvas the canvas to draw on
157 * @param padding the horizontal and vertical padding to use when drawing
158 */
159 private void drawWithPadding(Canvas destCanvas, int padding) {
160 final Rect clipRect = mTempRect;
161 getDrawingRect(clipRect);
162
163 // adjust the clip rect so that we don't include the text label
164 clipRect.bottom =
165 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
166
167 // Draw the View into the bitmap.
168 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
169 // they set scrollX and scrollY to large values to achieve centered text
170 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800171 destCanvas.scale(getScaleX(), getScaleY(),
172 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800173 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
174 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800175 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800176 destCanvas.restore();
177 }
178
179 /**
180 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
181 * Responsibility for the bitmap is transferred to the caller.
182 */
183 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
184 final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
185 final Bitmap b = Bitmap.createBitmap(
186 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
187
188 canvas.setBitmap(b);
189 drawWithPadding(canvas, padding);
190 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700191 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800192
193 return b;
194 }
195
196 @Override
197 public boolean onTouchEvent(MotionEvent event) {
198 // Call the superclass onTouchEvent first, because sometimes it changes the state to
199 // isPressed() on an ACTION_UP
200 boolean result = super.onTouchEvent(event);
201
202 switch (event.getAction()) {
203 case MotionEvent.ACTION_DOWN:
204 // So that the pressed outline is visible immediately when isPressed() is true,
205 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
206 // to create it)
207 if (mPressedOrFocusedBackground == null) {
208 mPressedOrFocusedBackground = createGlowingOutline(
209 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
210 }
211 // Invalidate so the pressed state is visible, or set a flag so we know that we
212 // have to call invalidate as soon as the state is "pressed"
213 if (isPressed()) {
214 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700215 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800216 } else {
217 mDidInvalidateForPressedState = false;
218 }
Winson Chung88f33452012-02-23 15:23:44 -0800219
220 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800221 break;
222 case MotionEvent.ACTION_CANCEL:
223 case MotionEvent.ACTION_UP:
224 // If we've touched down and up on an item, and it's still not "pressed", then
225 // destroy the pressed outline
226 if (!isPressed()) {
227 mPressedOrFocusedBackground = null;
228 }
Winson Chung88f33452012-02-23 15:23:44 -0800229
230 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800231 break;
232 }
233 return result;
234 }
235
Michael Jurkaddd62e92011-02-16 17:49:14 -0800236 void setStayPressed(boolean stayPressed) {
237 mStayPressed = stayPressed;
238 if (!stayPressed) {
239 mPressedOrFocusedBackground = null;
240 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800241 setCellLayoutPressedOrFocusedIcon();
242 }
243
244 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700245 if (getParent() instanceof ShortcutAndWidgetContainer) {
246 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700247 if (parent != null) {
248 CellLayout layout = (CellLayout) parent.getParent();
249 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
250 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700251 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800252 }
253
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700254 void clearPressedOrFocusedBackground() {
255 mPressedOrFocusedBackground = null;
256 setCellLayoutPressedOrFocusedIcon();
257 }
258
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800259 Bitmap getPressedOrFocusedBackground() {
260 return mPressedOrFocusedBackground;
261 }
262
263 int getPressedOrFocusedBackgroundPadding() {
264 return HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800265 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 @Override
268 public void draw(Canvas canvas) {
Michael Jurkabdb5c532011-02-01 15:05:06 -0800269 final Drawable background = mBackground;
270 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700271 final int scrollX = getScrollX();
272 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800273
Michael Jurkabdb5c532011-02-01 15:05:06 -0800274 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700275 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800276 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800278
279 if ((scrollX | scrollY) == 0) {
280 background.draw(canvas);
281 } else {
282 canvas.translate(scrollX, scrollY);
283 background.draw(canvas);
284 canvas.translate(-scrollX, -scrollY);
285 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800287
288 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800289 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800290 getPaint().clearShadowLayer();
291 super.draw(canvas);
292 return;
293 }
294
Michael Jurkabdb5c532011-02-01 15:05:06 -0800295 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800296 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800297 super.draw(canvas);
298 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700299 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
300 getScrollX() + getWidth(),
301 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800302 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800303 super.draw(canvas);
304 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400306
307 @Override
308 protected void onAttachedToWindow() {
309 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800310 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400311 }
312
313 @Override
314 protected void onDetachedFromWindow() {
315 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800316 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400317 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700318
319 @Override
320 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800321 if (mPrevAlpha != alpha) {
322 mPrevAlpha = alpha;
Winson Chunge22a8e92010-11-12 13:40:58 -0800323 super.onSetAlpha(alpha);
324 }
325 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700326 }
Winson Chung88f33452012-02-23 15:23:44 -0800327
328 @Override
329 public void cancelLongPress() {
330 super.cancelLongPress();
331
332 mLongPressHelper.cancelLongPress();
333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334}