blob: ee42904dd667607076630b803f3589c4d670368c [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
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;
Winson Chung5f8afe62013-08-12 16:19:28 -070028import android.util.TypedValue;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080029import android.view.MotionEvent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080030import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080031
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032/**
33 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
34 * because we want to make the bubble taller than the text and TextView's clip is
35 * too aggressive.
36 */
Michael Jurka08ee7702011-08-11 16:53:35 -070037public class BubbleTextView extends TextView {
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
Daniel Sandlere4f98912013-06-25 15:13:26 -040048 private HolographicOutlineHelper mOutlineHelper;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080049 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
Adam Cohen477828c2013-09-20 12:05:49 -070058 private int mTextColor;
59 private boolean mShadowsEnabled = true;
Winson Chung5f8afe62013-08-12 16:19:28 -070060 private boolean mIsTextVisible;
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
Winson Chung5f8afe62013-08-12 16:19:28 -070083 public void onFinishInflate() {
84 super.onFinishInflate();
85
86 // Ensure we are using the right text size
87 LauncherAppState app = LauncherAppState.getInstance();
88 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -070089 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Adam Cohen477828c2013-09-20 12:05:49 -070090 setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
Winson Chung5f8afe62013-08-12 16:19:28 -070091 }
92
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -080094 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 mBackground = getBackground();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
Daniel Sandlere4f98912013-06-25 15:13:26 -040097 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
98
Winson Chung656d11c2010-11-29 17:15:47 -080099 final Resources res = getContext().getResources();
Winson Chung7d3810d2011-10-07 13:11:56 -0700100 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor =
Adam Cohen410f3cd2013-09-22 12:09:32 -0700101 res.getColor(R.color.outline_color);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800102
103 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 }
105
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800106 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
107 Bitmap b = info.getIcon(iconCache);
Winson Chungcdef0442013-09-19 17:32:58 -0700108 LauncherAppState app = LauncherAppState.getInstance();
109 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800110
Winson Chung54000492013-10-14 16:29:29 -0700111 setCompoundDrawables(null,
Winson Chung0dbd7342013-10-13 22:46:20 -0700112 Utilities.createIconDrawable(b), null, null);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700113 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800114 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800115 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800116 }
117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 @Override
119 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700120 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 mBackgroundSizeChanged = true;
122 }
123 return super.setFrame(left, top, right, bottom);
124 }
125
126 @Override
127 protected boolean verifyDrawable(Drawable who) {
128 return who == mBackground || super.verifyDrawable(who);
129 }
130
131 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700132 public void setTag(Object tag) {
133 if (tag != null) {
134 LauncherModel.checkItemInfo((ItemInfo) tag);
135 }
136 super.setTag(tag);
137 }
138
139 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800141 if (isPressed()) {
142 // In this case, we have already created the pressed outline on ACTION_DOWN,
143 // so we just need to do an invalidate to trigger draw
144 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800145 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800146 }
147 } else {
148 // Otherwise, either clear the pressed/focused background, or create a background
149 // for the focused state
150 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800151 if (!mStayPressed) {
152 mPressedOrFocusedBackground = null;
153 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800154 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800155 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800156 // In some cases, we get focus before we have been layed out. Set the
157 // background to null so that it will get created when the view is drawn.
158 mPressedOrFocusedBackground = null;
159 } else {
160 mPressedOrFocusedBackground = createGlowingOutline(
161 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
162 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800163 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800164 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800165 }
166 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
167 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800168 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800169 }
170 }
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 Drawable d = mBackground;
173 if (d != null && d.isStateful()) {
174 d.setState(getDrawableState());
175 }
176 super.drawableStateChanged();
177 }
178
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800179 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800180 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800181 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800182 * @param destCanvas the canvas to draw on
183 * @param padding the horizontal and vertical padding to use when drawing
184 */
185 private void drawWithPadding(Canvas destCanvas, int padding) {
186 final Rect clipRect = mTempRect;
187 getDrawingRect(clipRect);
188
189 // adjust the clip rect so that we don't include the text label
190 clipRect.bottom =
191 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
192
193 // Draw the View into the bitmap.
194 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
195 // they set scrollX and scrollY to large values to achieve centered text
196 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800197 destCanvas.scale(getScaleX(), getScaleY(),
198 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800199 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
200 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800201 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800202 destCanvas.restore();
203 }
204
Adam Cohen7397e622013-10-24 12:11:34 -0700205 public void setGlowColor(int color) {
206 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color;
207 }
208
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800209 /**
210 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
211 * Responsibility for the bitmap is transferred to the caller.
212 */
213 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400214 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800215 final Bitmap b = Bitmap.createBitmap(
216 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
217
218 canvas.setBitmap(b);
219 drawWithPadding(canvas, padding);
220 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700221 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800222
223 return b;
224 }
225
226 @Override
227 public boolean onTouchEvent(MotionEvent event) {
228 // Call the superclass onTouchEvent first, because sometimes it changes the state to
229 // isPressed() on an ACTION_UP
230 boolean result = super.onTouchEvent(event);
231
232 switch (event.getAction()) {
233 case MotionEvent.ACTION_DOWN:
234 // So that the pressed outline is visible immediately when isPressed() is true,
235 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
236 // to create it)
237 if (mPressedOrFocusedBackground == null) {
238 mPressedOrFocusedBackground = createGlowingOutline(
239 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
240 }
241 // Invalidate so the pressed state is visible, or set a flag so we know that we
242 // have to call invalidate as soon as the state is "pressed"
243 if (isPressed()) {
244 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700245 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800246 } else {
247 mDidInvalidateForPressedState = false;
248 }
Winson Chung88f33452012-02-23 15:23:44 -0800249
250 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800251 break;
252 case MotionEvent.ACTION_CANCEL:
253 case MotionEvent.ACTION_UP:
254 // If we've touched down and up on an item, and it's still not "pressed", then
255 // destroy the pressed outline
256 if (!isPressed()) {
257 mPressedOrFocusedBackground = null;
258 }
Winson Chung88f33452012-02-23 15:23:44 -0800259
260 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800261 break;
262 }
263 return result;
264 }
265
Michael Jurkaddd62e92011-02-16 17:49:14 -0800266 void setStayPressed(boolean stayPressed) {
267 mStayPressed = stayPressed;
268 if (!stayPressed) {
269 mPressedOrFocusedBackground = null;
270 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800271 setCellLayoutPressedOrFocusedIcon();
272 }
273
274 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700275 if (getParent() instanceof ShortcutAndWidgetContainer) {
276 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700277 if (parent != null) {
278 CellLayout layout = (CellLayout) parent.getParent();
279 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
280 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700281 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800282 }
283
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700284 void clearPressedOrFocusedBackground() {
285 mPressedOrFocusedBackground = null;
286 setCellLayoutPressedOrFocusedIcon();
287 }
288
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800289 Bitmap getPressedOrFocusedBackground() {
290 return mPressedOrFocusedBackground;
291 }
292
293 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400294 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800295 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800296
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 @Override
298 public void draw(Canvas canvas) {
Adam Cohen477828c2013-09-20 12:05:49 -0700299 if (!mShadowsEnabled) {
300 super.draw(canvas);
301 return;
302 }
303
Michael Jurkabdb5c532011-02-01 15:05:06 -0800304 final Drawable background = mBackground;
305 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700306 final int scrollX = getScrollX();
307 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800308
Michael Jurkabdb5c532011-02-01 15:05:06 -0800309 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700310 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800311 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800313
314 if ((scrollX | scrollY) == 0) {
315 background.draw(canvas);
316 } else {
317 canvas.translate(scrollX, scrollY);
318 background.draw(canvas);
319 canvas.translate(-scrollX, -scrollY);
320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800322
323 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800324 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800325 getPaint().clearShadowLayer();
326 super.draw(canvas);
327 return;
328 }
329
Michael Jurkabdb5c532011-02-01 15:05:06 -0800330 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800331 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800332 super.draw(canvas);
333 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700334 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
335 getScrollX() + getWidth(),
336 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800337 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800338 super.draw(canvas);
339 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400341
342 @Override
343 protected void onAttachedToWindow() {
344 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800345 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400346 }
347
348 @Override
349 protected void onDetachedFromWindow() {
350 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800351 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700353
Adam Cohen477828c2013-09-20 12:05:49 -0700354 @Override
355 public void setTextColor(int color) {
356 mTextColor = color;
357 super.setTextColor(color);
358 }
359
360 public void setShadowsEnabled(boolean enabled) {
361 mShadowsEnabled = enabled;
362 getPaint().clearShadowLayer();
363 invalidate();
364 }
365
Winson Chung5f8afe62013-08-12 16:19:28 -0700366 public void setTextVisibility(boolean visible) {
367 Resources res = getResources();
368 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700369 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700370 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700371 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700372 }
373 mIsTextVisible = visible;
374 }
375
376 public boolean isTextVisible() {
377 return mIsTextVisible;
378 }
379
Winson Chungaffd7b42010-08-20 15:11:56 -0700380 @Override
381 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800382 if (mPrevAlpha != alpha) {
383 mPrevAlpha = alpha;
Winson Chunge22a8e92010-11-12 13:40:58 -0800384 super.onSetAlpha(alpha);
385 }
386 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700387 }
Winson Chung88f33452012-02-23 15:23:44 -0800388
389 @Override
390 public void cancelLongPress() {
391 super.cancelLongPress();
392
393 mLongPressHelper.cancelLongPress();
394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395}