blob: 4a3a7a452baf33b06612b1a6dc39342b25cab9c3 [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;
Winson Chung656d11c2010-11-29 17:15:47 -080023import android.graphics.Color;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Paint;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080025import android.graphics.Rect;
Michael Jurka137142e2011-01-05 20:57:04 -080026import android.graphics.Region;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080027import android.graphics.Region.Op;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080029import android.util.AttributeSet;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080030import android.view.MotionEvent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080031import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080032
Michael Jurka92f3d462011-11-22 21:02:29 -080033import com.android.launcher.R;
34
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035/**
36 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
37 * because we want to make the bubble taller than the text and TextView's clip is
38 * too aggressive.
39 */
Michael Jurka08ee7702011-08-11 16:53:35 -070040public class BubbleTextView extends TextView {
Winson Chung656d11c2010-11-29 17:15:47 -080041 static final float CORNER_RADIUS = 4.0f;
Winson Chung88127032010-12-13 12:11:33 -080042 static final float SHADOW_LARGE_RADIUS = 4.0f;
43 static final float SHADOW_SMALL_RADIUS = 1.75f;
44 static final float SHADOW_Y_OFFSET = 2.0f;
Winson Chungc7aef8c2011-09-15 18:53:04 -070045 static final int SHADOW_LARGE_COLOUR = 0xDD000000;
46 static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080047 static final float PADDING_H = 8.0f;
48 static final float PADDING_V = 3.0f;
49
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();
Michael Jurka38b4f7c2010-12-14 16:46:39 -080055 private boolean mDidInvalidateForPressedState;
56 private Bitmap mPressedOrFocusedBackground;
57 private int mFocusedOutlineColor;
58 private int mFocusedGlowColor;
59 private int mPressedOutlineColor;
60 private int mPressedGlowColor;
61
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 private boolean mBackgroundSizeChanged;
63 private Drawable mBackground;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064
Michael Jurkaddd62e92011-02-16 17:49:14 -080065 private boolean mStayPressed;
Winson Chung88f33452012-02-23 15:23:44 -080066 private CheckLongPressHelper mLongPressHelper;
Michael Jurkaddd62e92011-02-16 17:49:14 -080067
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 public BubbleTextView(Context context) {
69 super(context);
70 init();
71 }
72
73 public BubbleTextView(Context context, AttributeSet attrs) {
74 super(context, attrs);
75 init();
76 }
77
78 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
79 super(context, attrs, defStyle);
80 init();
81 }
82
83 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -080084 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 mBackground = getBackground();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Winson Chung656d11c2010-11-29 17:15:47 -080087 final Resources res = getContext().getResources();
88 int bubbleColor = res.getColor(R.color.bubble_dark_background);
Winson Chung7d3810d2011-10-07 13:11:56 -070089 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor =
90 res.getColor(android.R.color.holo_blue_light);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -080091
92 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 }
94
Michael Jurka67b2f6c2010-11-17 12:33:46 -080095 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
96 Bitmap b = info.getIcon(iconCache);
97
98 setCompoundDrawablesWithIntrinsicBounds(null,
99 new FastBitmapDrawable(b),
100 null, null);
101 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800102 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800103 }
104
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 @Override
106 protected boolean setFrame(int left, int top, int right, int bottom) {
107 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
108 mBackgroundSizeChanged = true;
109 }
110 return super.setFrame(left, top, right, bottom);
111 }
112
113 @Override
114 protected boolean verifyDrawable(Drawable who) {
115 return who == mBackground || super.verifyDrawable(who);
116 }
117
118 @Override
119 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800120 if (isPressed()) {
121 // In this case, we have already created the pressed outline on ACTION_DOWN,
122 // so we just need to do an invalidate to trigger draw
123 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800124 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800125 }
126 } else {
127 // Otherwise, either clear the pressed/focused background, or create a background
128 // for the focused state
129 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800130 if (!mStayPressed) {
131 mPressedOrFocusedBackground = null;
132 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800133 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800134 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800135 // In some cases, we get focus before we have been layed out. Set the
136 // background to null so that it will get created when the view is drawn.
137 mPressedOrFocusedBackground = null;
138 } else {
139 mPressedOrFocusedBackground = createGlowingOutline(
140 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
141 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800142 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800143 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800144 }
145 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
146 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800147 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800148 }
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 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800159 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800160 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800161 * @param destCanvas the canvas to draw on
162 * @param padding the horizontal and vertical padding to use when drawing
163 */
164 private void drawWithPadding(Canvas destCanvas, int padding) {
165 final Rect clipRect = mTempRect;
166 getDrawingRect(clipRect);
167
168 // adjust the clip rect so that we don't include the text label
169 clipRect.bottom =
170 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
171
172 // Draw the View into the bitmap.
173 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
174 // they set scrollX and scrollY to large values to achieve centered text
175 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800176 destCanvas.scale(getScaleX(), getScaleY(),
177 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800178 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
179 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800180 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800181 destCanvas.restore();
182 }
183
184 /**
185 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
186 * Responsibility for the bitmap is transferred to the caller.
187 */
188 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
189 final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
190 final Bitmap b = Bitmap.createBitmap(
191 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
192
193 canvas.setBitmap(b);
194 drawWithPadding(canvas, padding);
195 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700196 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800197
198 return b;
199 }
200
201 @Override
202 public boolean onTouchEvent(MotionEvent event) {
203 // Call the superclass onTouchEvent first, because sometimes it changes the state to
204 // isPressed() on an ACTION_UP
205 boolean result = super.onTouchEvent(event);
206
207 switch (event.getAction()) {
208 case MotionEvent.ACTION_DOWN:
209 // So that the pressed outline is visible immediately when isPressed() is true,
210 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
211 // to create it)
212 if (mPressedOrFocusedBackground == null) {
213 mPressedOrFocusedBackground = createGlowingOutline(
214 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
215 }
216 // Invalidate so the pressed state is visible, or set a flag so we know that we
217 // have to call invalidate as soon as the state is "pressed"
218 if (isPressed()) {
219 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700220 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800221 } else {
222 mDidInvalidateForPressedState = false;
223 }
Winson Chung88f33452012-02-23 15:23:44 -0800224
225 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800226 break;
227 case MotionEvent.ACTION_CANCEL:
228 case MotionEvent.ACTION_UP:
229 // If we've touched down and up on an item, and it's still not "pressed", then
230 // destroy the pressed outline
231 if (!isPressed()) {
232 mPressedOrFocusedBackground = null;
233 }
Winson Chung88f33452012-02-23 15:23:44 -0800234
235 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800236 break;
237 }
238 return result;
239 }
240
Michael Jurkaddd62e92011-02-16 17:49:14 -0800241 void setStayPressed(boolean stayPressed) {
242 mStayPressed = stayPressed;
243 if (!stayPressed) {
244 mPressedOrFocusedBackground = null;
245 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800246 setCellLayoutPressedOrFocusedIcon();
247 }
248
249 void setCellLayoutPressedOrFocusedIcon() {
Adam Cohen76fc0852011-06-17 13:26:23 -0700250 if (getParent() instanceof CellLayoutChildren) {
251 CellLayoutChildren parent = (CellLayoutChildren) getParent();
252 if (parent != null) {
253 CellLayout layout = (CellLayout) parent.getParent();
254 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
255 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700256 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800257 }
258
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700259 void clearPressedOrFocusedBackground() {
260 mPressedOrFocusedBackground = null;
261 setCellLayoutPressedOrFocusedIcon();
262 }
263
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800264 Bitmap getPressedOrFocusedBackground() {
265 return mPressedOrFocusedBackground;
266 }
267
268 int getPressedOrFocusedBackgroundPadding() {
269 return HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800270 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 @Override
273 public void draw(Canvas canvas) {
Michael Jurkabdb5c532011-02-01 15:05:06 -0800274 final Drawable background = mBackground;
275 if (background != null) {
276 final int scrollX = mScrollX;
277 final int scrollY = mScrollY;
Winson Chung88127032010-12-13 12:11:33 -0800278
Michael Jurkabdb5c532011-02-01 15:05:06 -0800279 if (mBackgroundSizeChanged) {
280 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
281 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800283
284 if ((scrollX | scrollY) == 0) {
285 background.draw(canvas);
286 } else {
287 canvas.translate(scrollX, scrollY);
288 background.draw(canvas);
289 canvas.translate(-scrollX, -scrollY);
290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800292
293 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800294 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800295 getPaint().clearShadowLayer();
296 super.draw(canvas);
297 return;
298 }
299
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 Chunge22a8e92010-11-12 13:40:58 -0800327 super.onSetAlpha(alpha);
328 }
329 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700330 }
Winson Chung88f33452012-02-23 15:23:44 -0800331
332 @Override
333 public void cancelLongPress() {
334 super.cancelLongPress();
335
336 mLongPressHelper.cancelLongPress();
337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338}