blob: 160a5ebcce6f4b327782a1ad7f2e8f5a8f5d64a2 [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;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050028import android.util.Log;
Winson Chung5f8afe62013-08-12 16:19:28 -070029import android.util.TypedValue;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080030import android.view.MotionEvent;
Jason Monk02dd7ae2014-04-15 15:23:31 -040031import android.view.ViewConfiguration;
Michael Jurkabdb5c532011-02-01 15:05:06 -080032import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080033
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034/**
35 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
36 * because we want to make the bubble taller than the text and TextView's clip is
37 * too aggressive.
38 */
Michael Jurka08ee7702011-08-11 16:53:35 -070039public class BubbleTextView extends TextView {
Winson Chung88127032010-12-13 12:11:33 -080040 static final float SHADOW_LARGE_RADIUS = 4.0f;
41 static final float SHADOW_SMALL_RADIUS = 1.75f;
42 static final float SHADOW_Y_OFFSET = 2.0f;
Winson Chungc7aef8c2011-09-15 18:53:04 -070043 static final int SHADOW_LARGE_COLOUR = 0xDD000000;
44 static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080045 static final float PADDING_H = 8.0f;
46 static final float PADDING_V = 3.0f;
47
Chris Wrenaeff7ea2014-02-14 16:59:24 -050048 private static final String TAG = "BubbleTextView";
49
50 private static final boolean DEBUG = false;
51
Winson Chunge22a8e92010-11-12 13:40:58 -080052 private int mPrevAlpha = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
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;
67 private boolean mShadowsEnabled = true;
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;
71 private 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 private int mInstallState;
76
Chris Wrenaeff7ea2014-02-14 16:59:24 -050077 private CharSequence mDefaultText = "";
Michael Jurkaddd62e92011-02-16 17:49:14 -080078
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 public BubbleTextView(Context context) {
80 super(context);
81 init();
82 }
83
84 public BubbleTextView(Context context, AttributeSet attrs) {
85 super(context, attrs);
86 init();
87 }
88
89 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
90 super(context, attrs, defStyle);
91 init();
92 }
93
Winson Chung5f8afe62013-08-12 16:19:28 -070094 public void onFinishInflate() {
95 super.onFinishInflate();
96
97 // Ensure we are using the right text size
98 LauncherAppState app = LauncherAppState.getInstance();
99 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700100 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Adam Cohen477828c2013-09-20 12:05:49 -0700101 setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
Winson Chung5f8afe62013-08-12 16:19:28 -0700102 }
103
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -0800105 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 mBackground = getBackground();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
Daniel Sandlere4f98912013-06-25 15:13:26 -0400108 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
109
Winson Chung656d11c2010-11-29 17:15:47 -0800110 final Resources res = getContext().getResources();
Winson Chung7d3810d2011-10-07 13:11:56 -0700111 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor =
Adam Cohen410f3cd2013-09-22 12:09:32 -0700112 res.getColor(R.color.outline_color);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800113
114 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 }
116
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800117 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
118 Bitmap b = info.getIcon(iconCache);
Winson Chungcdef0442013-09-19 17:32:58 -0700119 LauncherAppState app = LauncherAppState.getInstance();
120 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800121
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500122 Drawable iconDrawable = Utilities.createIconDrawable(b);
123 setCompoundDrawables(null, iconDrawable, null, null);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700124 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100125 if (info.contentDescription != null) {
126 setContentDescription(info.contentDescription);
127 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800128 setTag(info);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500129 if (info.isPromise()) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400130 applyState();
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500131 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800132 }
133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 @Override
135 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700136 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 mBackgroundSizeChanged = true;
138 }
139 return super.setFrame(left, top, right, bottom);
140 }
141
142 @Override
143 protected boolean verifyDrawable(Drawable who) {
144 return who == mBackground || super.verifyDrawable(who);
145 }
146
147 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700148 public void setTag(Object tag) {
149 if (tag != null) {
150 LauncherModel.checkItemInfo((ItemInfo) tag);
151 }
152 super.setTag(tag);
Chris Wren40c5ed32014-06-24 18:24:23 -0400153 if (tag instanceof ShortcutInfo) {
154 final ShortcutInfo info = (ShortcutInfo) tag;
155 mDefaultText = info.title;
156 setText(mDefaultText);
157 }
Michael Jurka816474f2012-06-25 14:49:02 -0700158 }
159
160 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800162 if (isPressed()) {
163 // In this case, we have already created the pressed outline on ACTION_DOWN,
164 // so we just need to do an invalidate to trigger draw
165 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800166 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800167 }
168 } else {
169 // Otherwise, either clear the pressed/focused background, or create a background
170 // for the focused state
171 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800172 if (!mStayPressed) {
173 mPressedOrFocusedBackground = null;
174 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800175 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800176 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800177 // In some cases, we get focus before we have been layed out. Set the
178 // background to null so that it will get created when the view is drawn.
179 mPressedOrFocusedBackground = null;
180 } else {
181 mPressedOrFocusedBackground = createGlowingOutline(
182 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
183 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800184 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800185 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800186 }
187 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
188 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800189 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800190 }
191 }
192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 Drawable d = mBackground;
194 if (d != null && d.isStateful()) {
195 d.setState(getDrawableState());
196 }
197 super.drawableStateChanged();
198 }
199
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800200 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800201 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800202 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800203 * @param destCanvas the canvas to draw on
204 * @param padding the horizontal and vertical padding to use when drawing
205 */
206 private void drawWithPadding(Canvas destCanvas, int padding) {
207 final Rect clipRect = mTempRect;
208 getDrawingRect(clipRect);
209
210 // adjust the clip rect so that we don't include the text label
211 clipRect.bottom =
212 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
213
214 // Draw the View into the bitmap.
215 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
216 // they set scrollX and scrollY to large values to achieve centered text
217 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800218 destCanvas.scale(getScaleX(), getScaleY(),
219 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800220 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
221 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800222 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800223 destCanvas.restore();
224 }
225
Adam Cohen7397e622013-10-24 12:11:34 -0700226 public void setGlowColor(int color) {
227 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color;
228 }
229
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800230 /**
231 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
232 * Responsibility for the bitmap is transferred to the caller.
233 */
234 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400235 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800236 final Bitmap b = Bitmap.createBitmap(
237 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
238
239 canvas.setBitmap(b);
240 drawWithPadding(canvas, padding);
241 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700242 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800243
244 return b;
245 }
246
247 @Override
248 public boolean onTouchEvent(MotionEvent event) {
249 // Call the superclass onTouchEvent first, because sometimes it changes the state to
250 // isPressed() on an ACTION_UP
251 boolean result = super.onTouchEvent(event);
252
253 switch (event.getAction()) {
254 case MotionEvent.ACTION_DOWN:
255 // So that the pressed outline is visible immediately when isPressed() is true,
256 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
257 // to create it)
258 if (mPressedOrFocusedBackground == null) {
259 mPressedOrFocusedBackground = createGlowingOutline(
260 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
261 }
262 // Invalidate so the pressed state is visible, or set a flag so we know that we
263 // have to call invalidate as soon as the state is "pressed"
264 if (isPressed()) {
265 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700266 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800267 } else {
268 mDidInvalidateForPressedState = false;
269 }
Winson Chung88f33452012-02-23 15:23:44 -0800270
271 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800272 break;
273 case MotionEvent.ACTION_CANCEL:
274 case MotionEvent.ACTION_UP:
275 // If we've touched down and up on an item, and it's still not "pressed", then
276 // destroy the pressed outline
277 if (!isPressed()) {
278 mPressedOrFocusedBackground = null;
279 }
Winson Chung88f33452012-02-23 15:23:44 -0800280
281 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800282 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400283 case MotionEvent.ACTION_MOVE:
284 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
285 mLongPressHelper.cancelLongPress();
286 }
287 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800288 }
289 return result;
290 }
291
Michael Jurkaddd62e92011-02-16 17:49:14 -0800292 void setStayPressed(boolean stayPressed) {
293 mStayPressed = stayPressed;
294 if (!stayPressed) {
295 mPressedOrFocusedBackground = null;
296 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800297 setCellLayoutPressedOrFocusedIcon();
298 }
299
300 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700301 if (getParent() instanceof ShortcutAndWidgetContainer) {
302 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700303 if (parent != null) {
304 CellLayout layout = (CellLayout) parent.getParent();
305 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
306 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700307 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800308 }
309
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700310 void clearPressedOrFocusedBackground() {
311 mPressedOrFocusedBackground = null;
312 setCellLayoutPressedOrFocusedIcon();
313 }
314
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800315 Bitmap getPressedOrFocusedBackground() {
316 return mPressedOrFocusedBackground;
317 }
318
319 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400320 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800321 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800322
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 @Override
324 public void draw(Canvas canvas) {
Adam Cohen477828c2013-09-20 12:05:49 -0700325 if (!mShadowsEnabled) {
326 super.draw(canvas);
327 return;
328 }
329
Michael Jurkabdb5c532011-02-01 15:05:06 -0800330 final Drawable background = mBackground;
331 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700332 final int scrollX = getScrollX();
333 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800334
Michael Jurkabdb5c532011-02-01 15:05:06 -0800335 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700336 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800337 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800339
340 if ((scrollX | scrollY) == 0) {
341 background.draw(canvas);
342 } else {
343 canvas.translate(scrollX, scrollY);
344 background.draw(canvas);
345 canvas.translate(-scrollX, -scrollY);
346 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800348
349 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800350 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800351 getPaint().clearShadowLayer();
352 super.draw(canvas);
353 return;
354 }
355
Michael Jurkabdb5c532011-02-01 15:05:06 -0800356 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800357 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800358 super.draw(canvas);
359 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700360 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
361 getScrollX() + getWidth(),
362 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800363 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800364 super.draw(canvas);
365 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367
368 @Override
369 protected void onAttachedToWindow() {
370 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800371 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400372 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 }
374
375 @Override
376 protected void onDetachedFromWindow() {
377 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800378 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700380
Adam Cohen477828c2013-09-20 12:05:49 -0700381 @Override
382 public void setTextColor(int color) {
383 mTextColor = color;
384 super.setTextColor(color);
385 }
386
387 public void setShadowsEnabled(boolean enabled) {
388 mShadowsEnabled = enabled;
389 getPaint().clearShadowLayer();
390 invalidate();
391 }
392
Winson Chung5f8afe62013-08-12 16:19:28 -0700393 public void setTextVisibility(boolean visible) {
394 Resources res = getResources();
395 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700396 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700397 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700398 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700399 }
400 mIsTextVisible = visible;
401 }
402
403 public boolean isTextVisible() {
404 return mIsTextVisible;
405 }
406
Winson Chungaffd7b42010-08-20 15:11:56 -0700407 @Override
408 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800409 if (mPrevAlpha != alpha) {
410 mPrevAlpha = alpha;
Winson Chunge22a8e92010-11-12 13:40:58 -0800411 super.onSetAlpha(alpha);
412 }
413 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700414 }
Winson Chung88f33452012-02-23 15:23:44 -0800415
416 @Override
417 public void cancelLongPress() {
418 super.cancelLongPress();
419
420 mLongPressHelper.cancelLongPress();
421 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500422
Chris Wren40c5ed32014-06-24 18:24:23 -0400423 public void applyState() {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500424 int alpha = getResources().getInteger(R.integer.promise_icon_alpha);
Chris Wren40c5ed32014-06-24 18:24:23 -0400425 final int state = getState();
426 if (DEBUG) Log.d(TAG, "applying icon state: " + state);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500427
Chris Wren40c5ed32014-06-24 18:24:23 -0400428 switch(state) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500429 case ShortcutInfo.PACKAGE_STATE_DEFAULT:
430 super.setText(mDefaultText);
431 alpha = 255;
432 break;
433
434 case ShortcutInfo.PACKAGE_STATE_ENQUEUED:
435 setText(R.string.package_state_enqueued);
436 break;
437
438 case ShortcutInfo.PACKAGE_STATE_DOWNLOADING:
439 setText(R.string.package_state_downloading);
440 break;
441
442 case ShortcutInfo.PACKAGE_STATE_INSTALLING:
443 setText(R.string.package_state_installing);
444 break;
445
446 case ShortcutInfo.PACKAGE_STATE_ERROR:
447 setText(R.string.package_state_error);
448 break;
449
450 case ShortcutInfo.PACKAGE_STATE_UNKNOWN:
451 default:
452 setText(R.string.package_state_unknown);
453 break;
454 }
455 if (DEBUG) Log.d(TAG, "setting icon alpha to: " + alpha);
456 Drawable[] drawables = getCompoundDrawables();
457 for (int i = 0; i < drawables.length; i++) {
458 if (drawables[i] != null) {
459 drawables[i].setAlpha(alpha);
460 }
461 }
462 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400463
464 private int getState() {
465 if (! (getTag() instanceof ShortcutInfo)) {
466 return ShortcutInfo.PACKAGE_STATE_DEFAULT;
467 } else {
468 ShortcutInfo info = (ShortcutInfo) getTag();
469 return info.getState();
470 }
471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472}