blob: 3f619a81248ba0b4e144ec67883348be8f4ee482 [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
Winson Chunge22a8e92010-11-12 13:40:58 -080054 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
Daniel Sandlere4f98912013-06-25 15:13:26 -040056 private HolographicOutlineHelper mOutlineHelper;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080057 private final Canvas mTempCanvas = new Canvas();
58 private final Rect mTempRect = new Rect();
Michael Jurka38b4f7c2010-12-14 16:46:39 -080059 private boolean mDidInvalidateForPressedState;
60 private Bitmap mPressedOrFocusedBackground;
61 private int mFocusedOutlineColor;
62 private int mFocusedGlowColor;
63 private int mPressedOutlineColor;
64 private int mPressedGlowColor;
65
Jason Monk02dd7ae2014-04-15 15:23:31 -040066 private float mSlop;
67
Adam Cohen477828c2013-09-20 12:05:49 -070068 private int mTextColor;
Sunny Goyal15872da2014-07-08 15:43:54 -070069 private final boolean mCustomShadowsEnabled;
Winson Chung5f8afe62013-08-12 16:19:28 -070070 private boolean mIsTextVisible;
71
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 private boolean mBackgroundSizeChanged;
Sunny Goyal15872da2014-07-08 15:43:54 -070073 private final Drawable mBackground;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
Michael Jurkaddd62e92011-02-16 17:49:14 -080075 private boolean mStayPressed;
Winson Chung88f33452012-02-23 15:23:44 -080076 private CheckLongPressHelper mLongPressHelper;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050077
Chris Wrenaeff7ea2014-02-14 16:59:24 -050078 private CharSequence mDefaultText = "";
Michael Jurkaddd62e92011-02-16 17:49:14 -080079
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070081 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 }
83
84 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070085 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 }
87
88 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
89 super(context, attrs, defStyle);
Adam Cohen96bb7982014-07-07 11:58:56 -070090
91 Resources res = context.getResources();
92 TypedArray a = context.obtainStyledAttributes(attrs,
93 R.styleable.BubbleTextView, defStyle, 0);
94 setGlowColor(a.getColor(R.styleable.BubbleTextView_glowColor,
95 res.getColor(R.color.outline_color)));
96 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
97 a.recycle();
98
Sunny Goyal15872da2014-07-08 15:43:54 -070099 if (mCustomShadowsEnabled) {
100 // Draw the background itself as the parent is drawn twice.
101 mBackground = getBackground();
102 setBackground(null);
103 } else {
104 mBackground = null;
105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 init();
107 }
108
Winson Chung5f8afe62013-08-12 16:19:28 -0700109 public void onFinishInflate() {
110 super.onFinishInflate();
111
112 // Ensure we are using the right text size
113 LauncherAppState app = LauncherAppState.getInstance();
114 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700115 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700116 }
117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -0800119 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Daniel Sandlere4f98912013-06-25 15:13:26 -0400121 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
Adam Cohen96bb7982014-07-07 11:58:56 -0700122 if (mCustomShadowsEnabled) {
123 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 }
126
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800127 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
128 Bitmap b = info.getIcon(iconCache);
Winson Chungcdef0442013-09-19 17:32:58 -0700129 LauncherAppState app = LauncherAppState.getInstance();
130 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800131
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500132 Drawable iconDrawable = Utilities.createIconDrawable(b);
133 setCompoundDrawables(null, iconDrawable, null, null);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700134 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100135 if (info.contentDescription != null) {
136 setContentDescription(info.contentDescription);
137 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800138 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700139
140 if (info.wasPromise) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400141 applyState();
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500142 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800143 }
144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 @Override
146 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700147 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 mBackgroundSizeChanged = true;
149 }
150 return super.setFrame(left, top, right, bottom);
151 }
152
153 @Override
154 protected boolean verifyDrawable(Drawable who) {
155 return who == mBackground || super.verifyDrawable(who);
156 }
157
158 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700159 public void setTag(Object tag) {
160 if (tag != null) {
161 LauncherModel.checkItemInfo((ItemInfo) tag);
162 }
163 super.setTag(tag);
Chris Wren40c5ed32014-06-24 18:24:23 -0400164 if (tag instanceof ShortcutInfo) {
165 final ShortcutInfo info = (ShortcutInfo) tag;
166 mDefaultText = info.title;
167 setText(mDefaultText);
168 }
Michael Jurka816474f2012-06-25 14:49:02 -0700169 }
170
171 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800173 if (isPressed()) {
174 // In this case, we have already created the pressed outline on ACTION_DOWN,
175 // so we just need to do an invalidate to trigger draw
176 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800177 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800178 }
179 } else {
180 // Otherwise, either clear the pressed/focused background, or create a background
181 // for the focused state
182 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800183 if (!mStayPressed) {
184 mPressedOrFocusedBackground = null;
185 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800186 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800187 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800188 // In some cases, we get focus before we have been layed out. Set the
189 // background to null so that it will get created when the view is drawn.
190 mPressedOrFocusedBackground = null;
191 } else {
192 mPressedOrFocusedBackground = createGlowingOutline(
193 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
194 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800195 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800196 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800197 }
198 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
199 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800200 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800201 }
202 }
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 Drawable d = mBackground;
205 if (d != null && d.isStateful()) {
206 d.setState(getDrawableState());
207 }
208 super.drawableStateChanged();
209 }
210
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800211 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800212 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800213 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800214 * @param destCanvas the canvas to draw on
215 * @param padding the horizontal and vertical padding to use when drawing
216 */
217 private void drawWithPadding(Canvas destCanvas, int padding) {
218 final Rect clipRect = mTempRect;
219 getDrawingRect(clipRect);
220
221 // adjust the clip rect so that we don't include the text label
222 clipRect.bottom =
223 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
224
225 // Draw the View into the bitmap.
226 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
227 // they set scrollX and scrollY to large values to achieve centered text
228 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800229 destCanvas.scale(getScaleX(), getScaleY(),
230 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800231 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
232 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800233 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800234 destCanvas.restore();
235 }
236
Adam Cohen7397e622013-10-24 12:11:34 -0700237 public void setGlowColor(int color) {
238 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color;
239 }
240
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800241 /**
242 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
243 * Responsibility for the bitmap is transferred to the caller.
244 */
245 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400246 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247 final Bitmap b = Bitmap.createBitmap(
248 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
249
250 canvas.setBitmap(b);
251 drawWithPadding(canvas, padding);
252 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700253 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800254
255 return b;
256 }
257
258 @Override
259 public boolean onTouchEvent(MotionEvent event) {
260 // Call the superclass onTouchEvent first, because sometimes it changes the state to
261 // isPressed() on an ACTION_UP
262 boolean result = super.onTouchEvent(event);
263
264 switch (event.getAction()) {
265 case MotionEvent.ACTION_DOWN:
266 // So that the pressed outline is visible immediately when isPressed() is true,
267 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
268 // to create it)
269 if (mPressedOrFocusedBackground == null) {
270 mPressedOrFocusedBackground = createGlowingOutline(
271 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
272 }
273 // Invalidate so the pressed state is visible, or set a flag so we know that we
274 // have to call invalidate as soon as the state is "pressed"
275 if (isPressed()) {
276 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700277 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800278 } else {
279 mDidInvalidateForPressedState = false;
280 }
Winson Chung88f33452012-02-23 15:23:44 -0800281
282 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800283 break;
284 case MotionEvent.ACTION_CANCEL:
285 case MotionEvent.ACTION_UP:
286 // If we've touched down and up on an item, and it's still not "pressed", then
287 // destroy the pressed outline
288 if (!isPressed()) {
289 mPressedOrFocusedBackground = null;
290 }
Winson Chung88f33452012-02-23 15:23:44 -0800291
292 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800293 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400294 case MotionEvent.ACTION_MOVE:
295 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
296 mLongPressHelper.cancelLongPress();
297 }
298 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800299 }
300 return result;
301 }
302
Michael Jurkaddd62e92011-02-16 17:49:14 -0800303 void setStayPressed(boolean stayPressed) {
304 mStayPressed = stayPressed;
305 if (!stayPressed) {
306 mPressedOrFocusedBackground = null;
307 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800308 setCellLayoutPressedOrFocusedIcon();
309 }
310
311 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700312 if (getParent() instanceof ShortcutAndWidgetContainer) {
313 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700314 if (parent != null) {
315 CellLayout layout = (CellLayout) parent.getParent();
316 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
317 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700318 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800319 }
320
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700321 void clearPressedOrFocusedBackground() {
322 mPressedOrFocusedBackground = null;
323 setCellLayoutPressedOrFocusedIcon();
324 }
325
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800326 Bitmap getPressedOrFocusedBackground() {
327 return mPressedOrFocusedBackground;
328 }
329
330 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400331 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800332 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 @Override
335 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700336 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700337 super.draw(canvas);
338 return;
339 }
340
Michael Jurkabdb5c532011-02-01 15:05:06 -0800341 final Drawable background = mBackground;
342 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700343 final int scrollX = getScrollX();
344 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800345
Michael Jurkabdb5c532011-02-01 15:05:06 -0800346 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700347 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800348 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800350
351 if ((scrollX | scrollY) == 0) {
352 background.draw(canvas);
353 } else {
354 canvas.translate(scrollX, scrollY);
355 background.draw(canvas);
356 canvas.translate(-scrollX, -scrollY);
357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800359
360 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800361 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800362 getPaint().clearShadowLayer();
363 super.draw(canvas);
364 return;
365 }
366
Michael Jurkabdb5c532011-02-01 15:05:06 -0800367 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800368 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800369 super.draw(canvas);
370 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700371 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
372 getScrollX() + getWidth(),
373 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800374 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800375 super.draw(canvas);
376 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378
379 @Override
380 protected void onAttachedToWindow() {
381 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800382 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400383 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384 }
385
386 @Override
387 protected void onDetachedFromWindow() {
388 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800389 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700391
Adam Cohen477828c2013-09-20 12:05:49 -0700392 @Override
393 public void setTextColor(int color) {
394 mTextColor = color;
395 super.setTextColor(color);
396 }
397
Adam Cohen96bb7982014-07-07 11:58:56 -0700398 @Override
399 public void setTextColor(ColorStateList colors) {
400 mTextColor = colors.getDefaultColor();
401 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700402 }
403
Winson Chung5f8afe62013-08-12 16:19:28 -0700404 public void setTextVisibility(boolean visible) {
405 Resources res = getResources();
406 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700407 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700408 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700409 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700410 }
411 mIsTextVisible = visible;
412 }
413
414 public boolean isTextVisible() {
415 return mIsTextVisible;
416 }
417
Winson Chungaffd7b42010-08-20 15:11:56 -0700418 @Override
419 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800420 if (mPrevAlpha != alpha) {
421 mPrevAlpha = alpha;
Winson Chunge22a8e92010-11-12 13:40:58 -0800422 super.onSetAlpha(alpha);
423 }
424 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700425 }
Winson Chung88f33452012-02-23 15:23:44 -0800426
427 @Override
428 public void cancelLongPress() {
429 super.cancelLongPress();
430
431 mLongPressHelper.cancelLongPress();
432 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500433
Chris Wren40c5ed32014-06-24 18:24:23 -0400434 public void applyState() {
Sunny Goyal34846382014-07-09 00:09:28 -0700435 final int progressLevel;
Chris Wren40c5ed32014-06-24 18:24:23 -0400436 final int state = getState();
437 if (DEBUG) Log.d(TAG, "applying icon state: " + state);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500438
Chris Wren40c5ed32014-06-24 18:24:23 -0400439 switch(state) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500440 case ShortcutInfo.PACKAGE_STATE_DEFAULT:
441 super.setText(mDefaultText);
Sunny Goyal34846382014-07-09 00:09:28 -0700442 progressLevel = 100;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500443 break;
444
445 case ShortcutInfo.PACKAGE_STATE_ENQUEUED:
446 setText(R.string.package_state_enqueued);
Sunny Goyal34846382014-07-09 00:09:28 -0700447 progressLevel = 0;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500448 break;
449
450 case ShortcutInfo.PACKAGE_STATE_DOWNLOADING:
451 setText(R.string.package_state_downloading);
Sunny Goyal34846382014-07-09 00:09:28 -0700452 // TODO(sunnygoyal): fix progress
453 progressLevel = 30;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500454 break;
455
456 case ShortcutInfo.PACKAGE_STATE_INSTALLING:
457 setText(R.string.package_state_installing);
Sunny Goyal34846382014-07-09 00:09:28 -0700458 progressLevel = 100;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500459 break;
460
461 case ShortcutInfo.PACKAGE_STATE_ERROR:
462 setText(R.string.package_state_error);
Sunny Goyal34846382014-07-09 00:09:28 -0700463 progressLevel = 0;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500464 break;
465
466 case ShortcutInfo.PACKAGE_STATE_UNKNOWN:
467 default:
Sunny Goyal34846382014-07-09 00:09:28 -0700468 progressLevel = 0;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500469 setText(R.string.package_state_unknown);
470 break;
471 }
Sunny Goyal34846382014-07-09 00:09:28 -0700472
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500473 Drawable[] drawables = getCompoundDrawables();
Sunny Goyal34846382014-07-09 00:09:28 -0700474 Drawable top = drawables[1];
475 if ((top != null) && !(top instanceof PreloadIconDrawable)) {
476 top = new PreloadIconDrawable(top, getResources());
477 setCompoundDrawables(drawables[0], top, drawables[2], drawables[3]);
478 }
479 if (top != null) {
480 top.setLevel(progressLevel);
481 if ((top instanceof PreloadIconDrawable)
482 && (state == ShortcutInfo.PACKAGE_STATE_DEFAULT)) {
483 ((PreloadIconDrawable) top).maybePerformFinishedAnimation();
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500484 }
485 }
486 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400487
488 private int getState() {
489 if (! (getTag() instanceof ShortcutInfo)) {
490 return ShortcutInfo.PACKAGE_STATE_DEFAULT;
491 } else {
492 ShortcutInfo info = (ShortcutInfo) getTag();
493 return info.getState();
494 }
495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496}