blob: ab94814c365f54498a8080ce42a90402cda7c5be [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;
Adam Cohen96bb7982014-07-07 11:58:56 -070020import android.content.res.ColorStateList;
Winson Chung656d11c2010-11-29 17:15:47 -080021import android.content.res.Resources;
Adam Cohen96bb7982014-07-07 11:58:56 -070022import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080023import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Canvas;
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;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050030import android.util.Log;
Winson Chung5f8afe62013-08-12 16:19:28 -070031import android.util.TypedValue;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080032import android.view.MotionEvent;
Jason Monk02dd7ae2014-04-15 15:23:31 -040033import android.view.ViewConfiguration;
Michael Jurkabdb5c532011-02-01 15:05:06 -080034import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080035
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036/**
37 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
38 * because we want to make the bubble taller than the text and TextView's clip is
39 * too aggressive.
40 */
Michael Jurka08ee7702011-08-11 16:53:35 -070041public class BubbleTextView extends TextView {
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
Chris Wrenaeff7ea2014-02-14 16:59:24 -050050 private static final String TAG = "BubbleTextView";
51
52 private static final boolean DEBUG = false;
53
Daniel Sandlere4f98912013-06-25 15:13:26 -040054 private HolographicOutlineHelper mOutlineHelper;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080055 private final Canvas mTempCanvas = new Canvas();
56 private final Rect mTempRect = new Rect();
Michael Jurka38b4f7c2010-12-14 16:46:39 -080057 private boolean mDidInvalidateForPressedState;
58 private Bitmap mPressedOrFocusedBackground;
59 private int mFocusedOutlineColor;
60 private int mFocusedGlowColor;
61 private int mPressedOutlineColor;
62 private int mPressedGlowColor;
63
Jason Monk02dd7ae2014-04-15 15:23:31 -040064 private float mSlop;
65
Adam Cohen477828c2013-09-20 12:05:49 -070066 private int mTextColor;
Sunny Goyal15872da2014-07-08 15:43:54 -070067 private final boolean mCustomShadowsEnabled;
Winson Chung5f8afe62013-08-12 16:19:28 -070068 private boolean mIsTextVisible;
69
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 private boolean mBackgroundSizeChanged;
Sunny Goyal15872da2014-07-08 15:43:54 -070071 private final Drawable mBackground;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
Michael Jurkaddd62e92011-02-16 17:49:14 -080073 private boolean mStayPressed;
Winson Chung88f33452012-02-23 15:23:44 -080074 private CheckLongPressHelper mLongPressHelper;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050075
Chris Wrenaeff7ea2014-02-14 16:59:24 -050076 private CharSequence mDefaultText = "";
Michael Jurkaddd62e92011-02-16 17:49:14 -080077
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070079 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 }
81
82 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070083 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 }
85
86 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
87 super(context, attrs, defStyle);
Adam Cohen96bb7982014-07-07 11:58:56 -070088
89 Resources res = context.getResources();
90 TypedArray a = context.obtainStyledAttributes(attrs,
91 R.styleable.BubbleTextView, defStyle, 0);
92 setGlowColor(a.getColor(R.styleable.BubbleTextView_glowColor,
93 res.getColor(R.color.outline_color)));
94 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
95 a.recycle();
96
Sunny Goyal15872da2014-07-08 15:43:54 -070097 if (mCustomShadowsEnabled) {
98 // Draw the background itself as the parent is drawn twice.
99 mBackground = getBackground();
100 setBackground(null);
101 } else {
102 mBackground = null;
103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 init();
105 }
106
Winson Chung5f8afe62013-08-12 16:19:28 -0700107 public void onFinishInflate() {
108 super.onFinishInflate();
109
110 // Ensure we are using the right text size
111 LauncherAppState app = LauncherAppState.getInstance();
112 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700113 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700114 }
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -0800117 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Daniel Sandlere4f98912013-06-25 15:13:26 -0400119 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
Adam Cohen96bb7982014-07-07 11:58:56 -0700120 if (mCustomShadowsEnabled) {
121 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 }
124
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700125 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
126 boolean setDefaultPadding) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800127 Bitmap b = info.getIcon(iconCache);
Winson Chungcdef0442013-09-19 17:32:58 -0700128 LauncherAppState app = LauncherAppState.getInstance();
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800129
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700130 FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);
131 if (info.isDisabled) {
132 iconDrawable.setSaturation(0);
133 iconDrawable.setBrightness(20);
134 }
135
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500136 setCompoundDrawables(null, iconDrawable, null, null);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700137 if (setDefaultPadding) {
138 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
139 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
140 }
Kenny Guyc2bd8102014-06-30 12:30:31 +0100141 if (info.contentDescription != null) {
142 setContentDescription(info.contentDescription);
143 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800144 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700145
146 if (info.wasPromise) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400147 applyState();
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500148 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800149 }
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 @Override
152 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700153 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 mBackgroundSizeChanged = true;
155 }
156 return super.setFrame(left, top, right, bottom);
157 }
158
159 @Override
160 protected boolean verifyDrawable(Drawable who) {
161 return who == mBackground || super.verifyDrawable(who);
162 }
163
164 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700165 public void setTag(Object tag) {
166 if (tag != null) {
167 LauncherModel.checkItemInfo((ItemInfo) tag);
168 }
169 super.setTag(tag);
Chris Wren40c5ed32014-06-24 18:24:23 -0400170 if (tag instanceof ShortcutInfo) {
171 final ShortcutInfo info = (ShortcutInfo) tag;
172 mDefaultText = info.title;
173 setText(mDefaultText);
174 }
Michael Jurka816474f2012-06-25 14:49:02 -0700175 }
176
177 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800179 if (isPressed()) {
180 // In this case, we have already created the pressed outline on ACTION_DOWN,
181 // so we just need to do an invalidate to trigger draw
182 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800183 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800184 }
185 } else {
186 // Otherwise, either clear the pressed/focused background, or create a background
187 // for the focused state
188 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800189 if (!mStayPressed) {
190 mPressedOrFocusedBackground = null;
191 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800192 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800193 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800194 // In some cases, we get focus before we have been layed out. Set the
195 // background to null so that it will get created when the view is drawn.
196 mPressedOrFocusedBackground = null;
197 } else {
198 mPressedOrFocusedBackground = createGlowingOutline(
199 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
200 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800201 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800202 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800203 }
204 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
205 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800206 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800207 }
208 }
209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 Drawable d = mBackground;
211 if (d != null && d.isStateful()) {
212 d.setState(getDrawableState());
213 }
214 super.drawableStateChanged();
215 }
216
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800217 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800218 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800219 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800220 * @param destCanvas the canvas to draw on
221 * @param padding the horizontal and vertical padding to use when drawing
222 */
223 private void drawWithPadding(Canvas destCanvas, int padding) {
224 final Rect clipRect = mTempRect;
225 getDrawingRect(clipRect);
226
227 // adjust the clip rect so that we don't include the text label
228 clipRect.bottom =
229 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
230
231 // Draw the View into the bitmap.
232 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
233 // they set scrollX and scrollY to large values to achieve centered text
234 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800235 destCanvas.scale(getScaleX(), getScaleY(),
236 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800237 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
238 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800239 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800240 destCanvas.restore();
241 }
242
Adam Cohen7397e622013-10-24 12:11:34 -0700243 public void setGlowColor(int color) {
244 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color;
245 }
246
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247 /**
248 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
249 * Responsibility for the bitmap is transferred to the caller.
250 */
251 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400252 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800253 final Bitmap b = Bitmap.createBitmap(
254 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
255
256 canvas.setBitmap(b);
257 drawWithPadding(canvas, padding);
258 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700259 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800260
261 return b;
262 }
263
264 @Override
265 public boolean onTouchEvent(MotionEvent event) {
266 // Call the superclass onTouchEvent first, because sometimes it changes the state to
267 // isPressed() on an ACTION_UP
268 boolean result = super.onTouchEvent(event);
269
270 switch (event.getAction()) {
271 case MotionEvent.ACTION_DOWN:
272 // So that the pressed outline is visible immediately when isPressed() is true,
273 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
274 // to create it)
275 if (mPressedOrFocusedBackground == null) {
276 mPressedOrFocusedBackground = createGlowingOutline(
277 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
278 }
279 // Invalidate so the pressed state is visible, or set a flag so we know that we
280 // have to call invalidate as soon as the state is "pressed"
281 if (isPressed()) {
282 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700283 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800284 } else {
285 mDidInvalidateForPressedState = false;
286 }
Winson Chung88f33452012-02-23 15:23:44 -0800287
288 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800289 break;
290 case MotionEvent.ACTION_CANCEL:
291 case MotionEvent.ACTION_UP:
292 // If we've touched down and up on an item, and it's still not "pressed", then
293 // destroy the pressed outline
294 if (!isPressed()) {
295 mPressedOrFocusedBackground = null;
296 }
Winson Chung88f33452012-02-23 15:23:44 -0800297
298 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800299 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400300 case MotionEvent.ACTION_MOVE:
301 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
302 mLongPressHelper.cancelLongPress();
303 }
304 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800305 }
306 return result;
307 }
308
Michael Jurkaddd62e92011-02-16 17:49:14 -0800309 void setStayPressed(boolean stayPressed) {
310 mStayPressed = stayPressed;
311 if (!stayPressed) {
312 mPressedOrFocusedBackground = null;
313 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800314 setCellLayoutPressedOrFocusedIcon();
315 }
316
317 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700318 if (getParent() instanceof ShortcutAndWidgetContainer) {
319 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700320 if (parent != null) {
321 CellLayout layout = (CellLayout) parent.getParent();
322 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
323 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700324 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800325 }
326
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700327 void clearPressedOrFocusedBackground() {
328 mPressedOrFocusedBackground = null;
329 setCellLayoutPressedOrFocusedIcon();
330 }
331
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800332 Bitmap getPressedOrFocusedBackground() {
333 return mPressedOrFocusedBackground;
334 }
335
336 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400337 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800338 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 @Override
341 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700342 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700343 super.draw(canvas);
344 return;
345 }
346
Michael Jurkabdb5c532011-02-01 15:05:06 -0800347 final Drawable background = mBackground;
348 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700349 final int scrollX = getScrollX();
350 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800351
Michael Jurkabdb5c532011-02-01 15:05:06 -0800352 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700353 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800354 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800356
357 if ((scrollX | scrollY) == 0) {
358 background.draw(canvas);
359 } else {
360 canvas.translate(scrollX, scrollY);
361 background.draw(canvas);
362 canvas.translate(-scrollX, -scrollY);
363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800365
366 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800367 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800368 getPaint().clearShadowLayer();
369 super.draw(canvas);
370 return;
371 }
372
Michael Jurkabdb5c532011-02-01 15:05:06 -0800373 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800374 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800375 super.draw(canvas);
376 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700377 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
378 getScrollX() + getWidth(),
379 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800380 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800381 super.draw(canvas);
382 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384
385 @Override
386 protected void onAttachedToWindow() {
387 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800388 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400389 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390 }
391
392 @Override
393 protected void onDetachedFromWindow() {
394 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800395 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400396 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700397
Adam Cohen477828c2013-09-20 12:05:49 -0700398 @Override
399 public void setTextColor(int color) {
400 mTextColor = color;
401 super.setTextColor(color);
402 }
403
Adam Cohen96bb7982014-07-07 11:58:56 -0700404 @Override
405 public void setTextColor(ColorStateList colors) {
406 mTextColor = colors.getDefaultColor();
407 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700408 }
409
Winson Chung5f8afe62013-08-12 16:19:28 -0700410 public void setTextVisibility(boolean visible) {
411 Resources res = getResources();
412 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700413 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700414 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700415 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700416 }
417 mIsTextVisible = visible;
418 }
419
420 public boolean isTextVisible() {
421 return mIsTextVisible;
422 }
423
Winson Chungaffd7b42010-08-20 15:11:56 -0700424 @Override
425 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800426 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700427 }
Winson Chung88f33452012-02-23 15:23:44 -0800428
429 @Override
430 public void cancelLongPress() {
431 super.cancelLongPress();
432
433 mLongPressHelper.cancelLongPress();
434 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500435
Chris Wren40c5ed32014-06-24 18:24:23 -0400436 public void applyState() {
Sunny Goyale755d462014-07-22 13:48:29 -0700437 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400438 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyale755d462014-07-22 13:48:29 -0700439 final int state = info.getState();
440
441 final int progressLevel;
442 if (DEBUG) Log.d(TAG, "applying icon state: " + state);
443
444 switch(state) {
445 case ShortcutInfo.PACKAGE_STATE_DEFAULT:
446 progressLevel = 100;
447 break;
448
449 case ShortcutInfo.PACKAGE_STATE_INSTALLING:
450 setText(R.string.package_state_installing);
451 progressLevel = info.getProgress();
452 break;
453
454 case ShortcutInfo.PACKAGE_STATE_ERROR:
455 case ShortcutInfo.PACKAGE_STATE_UNKNOWN:
456 default:
457 progressLevel = 0;
458 setText(R.string.package_state_unknown);
459 break;
460 }
461
462 Drawable[] drawables = getCompoundDrawables();
463 Drawable top = drawables[1];
464 if (top != null) {
465 final PreloadIconDrawable preloadDrawable;
466 if (top instanceof PreloadIconDrawable) {
467 preloadDrawable = (PreloadIconDrawable) top;
468 } else {
469 preloadDrawable = new PreloadIconDrawable(top, getResources());
470 setCompoundDrawables(drawables[0], preloadDrawable, drawables[2], drawables[3]);
471 }
472
473 preloadDrawable.setLevel(progressLevel);
474 if (state == ShortcutInfo.PACKAGE_STATE_DEFAULT) {
475 preloadDrawable.maybePerformFinishedAnimation();
476 }
477
478 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400479 }
480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481}