blob: 287bb5096dd7ec65ca9e15ca59ec3e2945ea0c06 [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;
Adam Cohen477828c2013-09-20 12:05:49 -070023import android.graphics.Color;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080024import android.graphics.Rect;
Michael Jurka137142e2011-01-05 20:57:04 -080025import android.graphics.Region;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080026import android.graphics.Region.Op;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080028import android.util.AttributeSet;
Winson Chung5f8afe62013-08-12 16:19:28 -070029import android.util.TypedValue;
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
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 Jurka08ee7702011-08-11 16:53:35 -070038public class BubbleTextView extends TextView {
Winson Chung88127032010-12-13 12:11:33 -080039 static final float SHADOW_LARGE_RADIUS = 4.0f;
40 static final float SHADOW_SMALL_RADIUS = 1.75f;
41 static final float SHADOW_Y_OFFSET = 2.0f;
Winson Chungc7aef8c2011-09-15 18:53:04 -070042 static final int SHADOW_LARGE_COLOUR = 0xDD000000;
43 static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080044 static final float PADDING_H = 8.0f;
45 static final float PADDING_V = 3.0f;
46
Winson Chunge22a8e92010-11-12 13:40:58 -080047 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Daniel Sandlere4f98912013-06-25 15:13:26 -040049 private HolographicOutlineHelper mOutlineHelper;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080050 private final Canvas mTempCanvas = new Canvas();
51 private final Rect mTempRect = new Rect();
Michael Jurka38b4f7c2010-12-14 16:46:39 -080052 private boolean mDidInvalidateForPressedState;
53 private Bitmap mPressedOrFocusedBackground;
54 private int mFocusedOutlineColor;
55 private int mFocusedGlowColor;
56 private int mPressedOutlineColor;
57 private int mPressedGlowColor;
58
Adam Cohen477828c2013-09-20 12:05:49 -070059 private int mTextColor;
60 private boolean mShadowsEnabled = true;
Winson Chung5f8afe62013-08-12 16:19:28 -070061 private boolean mIsTextVisible;
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
Michael Jurkaddd62e92011-02-16 17:49:14 -080066 private boolean mStayPressed;
Winson Chung88f33452012-02-23 15:23:44 -080067 private CheckLongPressHelper mLongPressHelper;
Michael Jurkaddd62e92011-02-16 17:49:14 -080068
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 public BubbleTextView(Context context) {
70 super(context);
71 init();
72 }
73
74 public BubbleTextView(Context context, AttributeSet attrs) {
75 super(context, attrs);
76 init();
77 }
78
79 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
80 super(context, attrs, defStyle);
81 init();
82 }
83
Winson Chung5f8afe62013-08-12 16:19:28 -070084 public void onFinishInflate() {
85 super.onFinishInflate();
86
87 // Ensure we are using the right text size
88 LauncherAppState app = LauncherAppState.getInstance();
89 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
90 setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.iconTextSize);
Adam Cohen477828c2013-09-20 12:05:49 -070091 setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
Winson Chung5f8afe62013-08-12 16:19:28 -070092 }
93
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -080095 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 mBackground = getBackground();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Daniel Sandlere4f98912013-06-25 15:13:26 -040098 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
99
Winson Chung656d11c2010-11-29 17:15:47 -0800100 final Resources res = getContext().getResources();
Winson Chung7d3810d2011-10-07 13:11:56 -0700101 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor =
Adam Cohen410f3cd2013-09-22 12:09:32 -0700102 res.getColor(R.color.outline_color);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800103
104 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 }
106
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800107 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
108 Bitmap b = info.getIcon(iconCache);
Winson Chungcdef0442013-09-19 17:32:58 -0700109 LauncherAppState app = LauncherAppState.getInstance();
110 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800111
112 setCompoundDrawablesWithIntrinsicBounds(null,
113 new FastBitmapDrawable(b),
114 null, null);
Winson Chungcdef0442013-09-19 17:32:58 -0700115 setCompoundDrawablePadding((int) ((grid.folderIconSizePx - grid.iconSizePx) / 2f));
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800116 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800117 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800118 }
119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 @Override
121 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700122 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 mBackgroundSizeChanged = true;
124 }
125 return super.setFrame(left, top, right, bottom);
126 }
127
128 @Override
129 protected boolean verifyDrawable(Drawable who) {
130 return who == mBackground || super.verifyDrawable(who);
131 }
132
133 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700134 public void setTag(Object tag) {
135 if (tag != null) {
136 LauncherModel.checkItemInfo((ItemInfo) tag);
137 }
138 super.setTag(tag);
139 }
140
141 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800143 if (isPressed()) {
144 // In this case, we have already created the pressed outline on ACTION_DOWN,
145 // so we just need to do an invalidate to trigger draw
146 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800147 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800148 }
149 } else {
150 // Otherwise, either clear the pressed/focused background, or create a background
151 // for the focused state
152 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800153 if (!mStayPressed) {
154 mPressedOrFocusedBackground = null;
155 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800156 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800157 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800158 // In some cases, we get focus before we have been layed out. Set the
159 // background to null so that it will get created when the view is drawn.
160 mPressedOrFocusedBackground = null;
161 } else {
162 mPressedOrFocusedBackground = createGlowingOutline(
163 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
164 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800165 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800166 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800167 }
168 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
169 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800170 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800171 }
172 }
173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 Drawable d = mBackground;
175 if (d != null && d.isStateful()) {
176 d.setState(getDrawableState());
177 }
178 super.drawableStateChanged();
179 }
180
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800181 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800182 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800183 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800184 * @param destCanvas the canvas to draw on
185 * @param padding the horizontal and vertical padding to use when drawing
186 */
187 private void drawWithPadding(Canvas destCanvas, int padding) {
188 final Rect clipRect = mTempRect;
189 getDrawingRect(clipRect);
190
191 // adjust the clip rect so that we don't include the text label
192 clipRect.bottom =
193 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
194
195 // Draw the View into the bitmap.
196 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
197 // they set scrollX and scrollY to large values to achieve centered text
198 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800199 destCanvas.scale(getScaleX(), getScaleY(),
200 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800201 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
202 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800203 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800204 destCanvas.restore();
205 }
206
207 /**
208 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
209 * Responsibility for the bitmap is transferred to the caller.
210 */
211 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400212 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800213 final Bitmap b = Bitmap.createBitmap(
214 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
215
216 canvas.setBitmap(b);
217 drawWithPadding(canvas, padding);
218 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700219 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800220
221 return b;
222 }
223
224 @Override
225 public boolean onTouchEvent(MotionEvent event) {
226 // Call the superclass onTouchEvent first, because sometimes it changes the state to
227 // isPressed() on an ACTION_UP
228 boolean result = super.onTouchEvent(event);
229
230 switch (event.getAction()) {
231 case MotionEvent.ACTION_DOWN:
232 // So that the pressed outline is visible immediately when isPressed() is true,
233 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
234 // to create it)
235 if (mPressedOrFocusedBackground == null) {
236 mPressedOrFocusedBackground = createGlowingOutline(
237 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
238 }
239 // Invalidate so the pressed state is visible, or set a flag so we know that we
240 // have to call invalidate as soon as the state is "pressed"
241 if (isPressed()) {
242 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700243 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800244 } else {
245 mDidInvalidateForPressedState = false;
246 }
Winson Chung88f33452012-02-23 15:23:44 -0800247
248 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800249 break;
250 case MotionEvent.ACTION_CANCEL:
251 case MotionEvent.ACTION_UP:
252 // If we've touched down and up on an item, and it's still not "pressed", then
253 // destroy the pressed outline
254 if (!isPressed()) {
255 mPressedOrFocusedBackground = null;
256 }
Winson Chung88f33452012-02-23 15:23:44 -0800257
258 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800259 break;
260 }
261 return result;
262 }
263
Michael Jurkaddd62e92011-02-16 17:49:14 -0800264 void setStayPressed(boolean stayPressed) {
265 mStayPressed = stayPressed;
266 if (!stayPressed) {
267 mPressedOrFocusedBackground = null;
268 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800269 setCellLayoutPressedOrFocusedIcon();
270 }
271
272 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700273 if (getParent() instanceof ShortcutAndWidgetContainer) {
274 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700275 if (parent != null) {
276 CellLayout layout = (CellLayout) parent.getParent();
277 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
278 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700279 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800280 }
281
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700282 void clearPressedOrFocusedBackground() {
283 mPressedOrFocusedBackground = null;
284 setCellLayoutPressedOrFocusedIcon();
285 }
286
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800287 Bitmap getPressedOrFocusedBackground() {
288 return mPressedOrFocusedBackground;
289 }
290
291 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400292 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800293 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 @Override
296 public void draw(Canvas canvas) {
Adam Cohen477828c2013-09-20 12:05:49 -0700297 if (!mShadowsEnabled) {
298 super.draw(canvas);
299 return;
300 }
301
Michael Jurkabdb5c532011-02-01 15:05:06 -0800302 final Drawable background = mBackground;
303 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700304 final int scrollX = getScrollX();
305 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800306
Michael Jurkabdb5c532011-02-01 15:05:06 -0800307 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700308 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800309 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800311
312 if ((scrollX | scrollY) == 0) {
313 background.draw(canvas);
314 } else {
315 canvas.translate(scrollX, scrollY);
316 background.draw(canvas);
317 canvas.translate(-scrollX, -scrollY);
318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800320
321 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800322 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800323 getPaint().clearShadowLayer();
324 super.draw(canvas);
325 return;
326 }
327
Michael Jurkabdb5c532011-02-01 15:05:06 -0800328 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800329 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800330 super.draw(canvas);
331 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700332 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
333 getScrollX() + getWidth(),
334 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800335 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800336 super.draw(canvas);
337 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400339
340 @Override
341 protected void onAttachedToWindow() {
342 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800343 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400344 }
345
346 @Override
347 protected void onDetachedFromWindow() {
348 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800349 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400350 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700351
Adam Cohen477828c2013-09-20 12:05:49 -0700352 @Override
353 public void setTextColor(int color) {
354 mTextColor = color;
355 super.setTextColor(color);
356 }
357
358 public void setShadowsEnabled(boolean enabled) {
359 mShadowsEnabled = enabled;
360 getPaint().clearShadowLayer();
361 invalidate();
362 }
363
Winson Chung5f8afe62013-08-12 16:19:28 -0700364 public void setTextVisibility(boolean visible) {
365 Resources res = getResources();
366 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700367 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700368 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700369 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700370 }
371 mIsTextVisible = visible;
372 }
373
374 public boolean isTextVisible() {
375 return mIsTextVisible;
376 }
377
Winson Chungaffd7b42010-08-20 15:11:56 -0700378 @Override
379 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800380 if (mPrevAlpha != alpha) {
381 mPrevAlpha = alpha;
Winson Chunge22a8e92010-11-12 13:40:58 -0800382 super.onSetAlpha(alpha);
383 }
384 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700385 }
Winson Chung88f33452012-02-23 15:23:44 -0800386
387 @Override
388 public void cancelLongPress() {
389 super.cancelLongPress();
390
391 mLongPressHelper.cancelLongPress();
392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393}