blob: 7a7a3b61f4f80fc8d8aad2b57901f31d84385b7c [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
Winson Chung5f8afe62013-08-12 16:19:28 -070058 private boolean mIsTextVisible;
59
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060 private boolean mBackgroundSizeChanged;
61 private Drawable mBackground;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
Michael Jurkaddd62e92011-02-16 17:49:14 -080063 private boolean mStayPressed;
Winson Chung88f33452012-02-23 15:23:44 -080064 private CheckLongPressHelper mLongPressHelper;
Michael Jurkaddd62e92011-02-16 17:49:14 -080065
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 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
Winson Chung5f8afe62013-08-12 16:19:28 -070081 public void onFinishInflate() {
82 super.onFinishInflate();
83
84 // Ensure we are using the right text size
85 LauncherAppState app = LauncherAppState.getInstance();
86 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
87 setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.iconTextSize);
88 }
89
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -080091 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 mBackground = getBackground();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093
Daniel Sandlere4f98912013-06-25 15:13:26 -040094 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
95
Winson Chung656d11c2010-11-29 17:15:47 -080096 final Resources res = getContext().getResources();
Winson Chung7d3810d2011-10-07 13:11:56 -070097 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor =
98 res.getColor(android.R.color.holo_blue_light);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -080099
100 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
102
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800103 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
104 Bitmap b = info.getIcon(iconCache);
Winson Chungcdef0442013-09-19 17:32:58 -0700105 LauncherAppState app = LauncherAppState.getInstance();
106 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800107
108 setCompoundDrawablesWithIntrinsicBounds(null,
109 new FastBitmapDrawable(b),
110 null, null);
Winson Chungcdef0442013-09-19 17:32:58 -0700111 setCompoundDrawablePadding((int) ((grid.folderIconSizePx - grid.iconSizePx) / 2f));
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800112 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800113 setTag(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800114 }
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 @Override
117 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700118 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 mBackgroundSizeChanged = true;
120 }
121 return super.setFrame(left, top, right, bottom);
122 }
123
124 @Override
125 protected boolean verifyDrawable(Drawable who) {
126 return who == mBackground || super.verifyDrawable(who);
127 }
128
129 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700130 public void setTag(Object tag) {
131 if (tag != null) {
132 LauncherModel.checkItemInfo((ItemInfo) tag);
133 }
134 super.setTag(tag);
135 }
136
137 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800139 if (isPressed()) {
140 // In this case, we have already created the pressed outline on ACTION_DOWN,
141 // so we just need to do an invalidate to trigger draw
142 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800143 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800144 }
145 } else {
146 // Otherwise, either clear the pressed/focused background, or create a background
147 // for the focused state
148 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800149 if (!mStayPressed) {
150 mPressedOrFocusedBackground = null;
151 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800152 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800153 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800154 // In some cases, we get focus before we have been layed out. Set the
155 // background to null so that it will get created when the view is drawn.
156 mPressedOrFocusedBackground = null;
157 } else {
158 mPressedOrFocusedBackground = createGlowingOutline(
159 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
160 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800161 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800162 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800163 }
164 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
165 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800166 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800167 }
168 }
169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 Drawable d = mBackground;
171 if (d != null && d.isStateful()) {
172 d.setState(getDrawableState());
173 }
174 super.drawableStateChanged();
175 }
176
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800177 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800178 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800179 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800180 * @param destCanvas the canvas to draw on
181 * @param padding the horizontal and vertical padding to use when drawing
182 */
183 private void drawWithPadding(Canvas destCanvas, int padding) {
184 final Rect clipRect = mTempRect;
185 getDrawingRect(clipRect);
186
187 // adjust the clip rect so that we don't include the text label
188 clipRect.bottom =
189 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
190
191 // Draw the View into the bitmap.
192 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
193 // they set scrollX and scrollY to large values to achieve centered text
194 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800195 destCanvas.scale(getScaleX(), getScaleY(),
196 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800197 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
198 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800199 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800200 destCanvas.restore();
201 }
202
203 /**
204 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
205 * Responsibility for the bitmap is transferred to the caller.
206 */
207 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400208 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800209 final Bitmap b = Bitmap.createBitmap(
210 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
211
212 canvas.setBitmap(b);
213 drawWithPadding(canvas, padding);
214 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700215 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800216
217 return b;
218 }
219
220 @Override
221 public boolean onTouchEvent(MotionEvent event) {
222 // Call the superclass onTouchEvent first, because sometimes it changes the state to
223 // isPressed() on an ACTION_UP
224 boolean result = super.onTouchEvent(event);
225
226 switch (event.getAction()) {
227 case MotionEvent.ACTION_DOWN:
228 // So that the pressed outline is visible immediately when isPressed() is true,
229 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
230 // to create it)
231 if (mPressedOrFocusedBackground == null) {
232 mPressedOrFocusedBackground = createGlowingOutline(
233 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
234 }
235 // Invalidate so the pressed state is visible, or set a flag so we know that we
236 // have to call invalidate as soon as the state is "pressed"
237 if (isPressed()) {
238 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700239 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800240 } else {
241 mDidInvalidateForPressedState = false;
242 }
Winson Chung88f33452012-02-23 15:23:44 -0800243
244 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800245 break;
246 case MotionEvent.ACTION_CANCEL:
247 case MotionEvent.ACTION_UP:
248 // If we've touched down and up on an item, and it's still not "pressed", then
249 // destroy the pressed outline
250 if (!isPressed()) {
251 mPressedOrFocusedBackground = null;
252 }
Winson Chung88f33452012-02-23 15:23:44 -0800253
254 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800255 break;
256 }
257 return result;
258 }
259
Michael Jurkaddd62e92011-02-16 17:49:14 -0800260 void setStayPressed(boolean stayPressed) {
261 mStayPressed = stayPressed;
262 if (!stayPressed) {
263 mPressedOrFocusedBackground = null;
264 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800265 setCellLayoutPressedOrFocusedIcon();
266 }
267
268 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700269 if (getParent() instanceof ShortcutAndWidgetContainer) {
270 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700271 if (parent != null) {
272 CellLayout layout = (CellLayout) parent.getParent();
273 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
274 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700275 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800276 }
277
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700278 void clearPressedOrFocusedBackground() {
279 mPressedOrFocusedBackground = null;
280 setCellLayoutPressedOrFocusedIcon();
281 }
282
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800283 Bitmap getPressedOrFocusedBackground() {
284 return mPressedOrFocusedBackground;
285 }
286
287 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400288 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800289 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800290
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 @Override
292 public void draw(Canvas canvas) {
Michael Jurkabdb5c532011-02-01 15:05:06 -0800293 final Drawable background = mBackground;
294 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700295 final int scrollX = getScrollX();
296 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800297
Michael Jurkabdb5c532011-02-01 15:05:06 -0800298 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700299 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800300 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800302
303 if ((scrollX | scrollY) == 0) {
304 background.draw(canvas);
305 } else {
306 canvas.translate(scrollX, scrollY);
307 background.draw(canvas);
308 canvas.translate(-scrollX, -scrollY);
309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800311
312 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800313 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800314 getPaint().clearShadowLayer();
315 super.draw(canvas);
316 return;
317 }
318
Michael Jurkabdb5c532011-02-01 15:05:06 -0800319 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800320 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800321 super.draw(canvas);
322 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700323 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
324 getScrollX() + getWidth(),
325 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800326 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800327 super.draw(canvas);
328 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400330
331 @Override
332 protected void onAttachedToWindow() {
333 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800334 if (mBackground != null) mBackground.setCallback(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400335 }
336
337 @Override
338 protected void onDetachedFromWindow() {
339 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800340 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400341 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700342
Winson Chung5f8afe62013-08-12 16:19:28 -0700343 public void setTextVisibility(boolean visible) {
344 Resources res = getResources();
345 if (visible) {
346 setTextColor(res.getColor(R.color.workspace_icon_text_color));
347 } else {
348 setTextColor(res.getColor(android.R.color.transparent));
349 }
350 mIsTextVisible = visible;
351 }
352
353 public boolean isTextVisible() {
354 return mIsTextVisible;
355 }
356
Winson Chungaffd7b42010-08-20 15:11:56 -0700357 @Override
358 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800359 if (mPrevAlpha != alpha) {
360 mPrevAlpha = alpha;
Winson Chunge22a8e92010-11-12 13:40:58 -0800361 super.onSetAlpha(alpha);
362 }
363 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700364 }
Winson Chung88f33452012-02-23 15:23:44 -0800365
366 @Override
367 public void cancelLongPress() {
368 super.cancelLongPress();
369
370 mLongPressHelper.cancelLongPress();
371 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372}