blob: 992ac58971a167bfb69e092fe4847f3aba7fa80b [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);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800125 setTag(info);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500126 if (info.isPromise()) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400127 applyState();
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500128 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800129 }
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 @Override
132 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700133 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 mBackgroundSizeChanged = true;
135 }
136 return super.setFrame(left, top, right, bottom);
137 }
138
139 @Override
140 protected boolean verifyDrawable(Drawable who) {
141 return who == mBackground || super.verifyDrawable(who);
142 }
143
144 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700145 public void setTag(Object tag) {
146 if (tag != null) {
147 LauncherModel.checkItemInfo((ItemInfo) tag);
148 }
149 super.setTag(tag);
Chris Wren40c5ed32014-06-24 18:24:23 -0400150 if (tag instanceof ShortcutInfo) {
151 final ShortcutInfo info = (ShortcutInfo) tag;
152 mDefaultText = info.title;
153 setText(mDefaultText);
154 }
Michael Jurka816474f2012-06-25 14:49:02 -0700155 }
156
157 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800159 if (isPressed()) {
160 // In this case, we have already created the pressed outline on ACTION_DOWN,
161 // so we just need to do an invalidate to trigger draw
162 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800163 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800164 }
165 } else {
166 // Otherwise, either clear the pressed/focused background, or create a background
167 // for the focused state
168 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800169 if (!mStayPressed) {
170 mPressedOrFocusedBackground = null;
171 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800172 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800173 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800174 // In some cases, we get focus before we have been layed out. Set the
175 // background to null so that it will get created when the view is drawn.
176 mPressedOrFocusedBackground = null;
177 } else {
178 mPressedOrFocusedBackground = createGlowingOutline(
179 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
180 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800181 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800182 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800183 }
184 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
185 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800186 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800187 }
188 }
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 Drawable d = mBackground;
191 if (d != null && d.isStateful()) {
192 d.setState(getDrawableState());
193 }
194 super.drawableStateChanged();
195 }
196
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800197 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800198 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800199 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800200 * @param destCanvas the canvas to draw on
201 * @param padding the horizontal and vertical padding to use when drawing
202 */
203 private void drawWithPadding(Canvas destCanvas, int padding) {
204 final Rect clipRect = mTempRect;
205 getDrawingRect(clipRect);
206
207 // adjust the clip rect so that we don't include the text label
208 clipRect.bottom =
209 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
210
211 // Draw the View into the bitmap.
212 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
213 // they set scrollX and scrollY to large values to achieve centered text
214 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800215 destCanvas.scale(getScaleX(), getScaleY(),
216 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800217 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
218 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800219 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800220 destCanvas.restore();
221 }
222
Adam Cohen7397e622013-10-24 12:11:34 -0700223 public void setGlowColor(int color) {
224 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color;
225 }
226
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800227 /**
228 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
229 * Responsibility for the bitmap is transferred to the caller.
230 */
231 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400232 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800233 final Bitmap b = Bitmap.createBitmap(
234 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
235
236 canvas.setBitmap(b);
237 drawWithPadding(canvas, padding);
238 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700239 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800240
241 return b;
242 }
243
244 @Override
245 public boolean onTouchEvent(MotionEvent event) {
246 // Call the superclass onTouchEvent first, because sometimes it changes the state to
247 // isPressed() on an ACTION_UP
248 boolean result = super.onTouchEvent(event);
249
250 switch (event.getAction()) {
251 case MotionEvent.ACTION_DOWN:
252 // So that the pressed outline is visible immediately when isPressed() is true,
253 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
254 // to create it)
255 if (mPressedOrFocusedBackground == null) {
256 mPressedOrFocusedBackground = createGlowingOutline(
257 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
258 }
259 // Invalidate so the pressed state is visible, or set a flag so we know that we
260 // have to call invalidate as soon as the state is "pressed"
261 if (isPressed()) {
262 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700263 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800264 } else {
265 mDidInvalidateForPressedState = false;
266 }
Winson Chung88f33452012-02-23 15:23:44 -0800267
268 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800269 break;
270 case MotionEvent.ACTION_CANCEL:
271 case MotionEvent.ACTION_UP:
272 // If we've touched down and up on an item, and it's still not "pressed", then
273 // destroy the pressed outline
274 if (!isPressed()) {
275 mPressedOrFocusedBackground = null;
276 }
Winson Chung88f33452012-02-23 15:23:44 -0800277
278 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800279 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400280 case MotionEvent.ACTION_MOVE:
281 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
282 mLongPressHelper.cancelLongPress();
283 }
284 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800285 }
286 return result;
287 }
288
Michael Jurkaddd62e92011-02-16 17:49:14 -0800289 void setStayPressed(boolean stayPressed) {
290 mStayPressed = stayPressed;
291 if (!stayPressed) {
292 mPressedOrFocusedBackground = null;
293 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800294 setCellLayoutPressedOrFocusedIcon();
295 }
296
297 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700298 if (getParent() instanceof ShortcutAndWidgetContainer) {
299 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700300 if (parent != null) {
301 CellLayout layout = (CellLayout) parent.getParent();
302 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
303 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700304 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800305 }
306
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700307 void clearPressedOrFocusedBackground() {
308 mPressedOrFocusedBackground = null;
309 setCellLayoutPressedOrFocusedIcon();
310 }
311
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800312 Bitmap getPressedOrFocusedBackground() {
313 return mPressedOrFocusedBackground;
314 }
315
316 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400317 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800318 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800319
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 @Override
321 public void draw(Canvas canvas) {
Adam Cohen477828c2013-09-20 12:05:49 -0700322 if (!mShadowsEnabled) {
323 super.draw(canvas);
324 return;
325 }
326
Michael Jurkabdb5c532011-02-01 15:05:06 -0800327 final Drawable background = mBackground;
328 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700329 final int scrollX = getScrollX();
330 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800331
Michael Jurkabdb5c532011-02-01 15:05:06 -0800332 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700333 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800334 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800336
337 if ((scrollX | scrollY) == 0) {
338 background.draw(canvas);
339 } else {
340 canvas.translate(scrollX, scrollY);
341 background.draw(canvas);
342 canvas.translate(-scrollX, -scrollY);
343 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800345
346 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800347 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800348 getPaint().clearShadowLayer();
349 super.draw(canvas);
350 return;
351 }
352
Michael Jurkabdb5c532011-02-01 15:05:06 -0800353 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800354 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800355 super.draw(canvas);
356 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700357 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
358 getScrollX() + getWidth(),
359 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800360 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800361 super.draw(canvas);
362 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364
365 @Override
366 protected void onAttachedToWindow() {
367 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800368 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400369 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400370 }
371
372 @Override
373 protected void onDetachedFromWindow() {
374 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800375 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400376 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700377
Adam Cohen477828c2013-09-20 12:05:49 -0700378 @Override
379 public void setTextColor(int color) {
380 mTextColor = color;
381 super.setTextColor(color);
382 }
383
384 public void setShadowsEnabled(boolean enabled) {
385 mShadowsEnabled = enabled;
386 getPaint().clearShadowLayer();
387 invalidate();
388 }
389
Winson Chung5f8afe62013-08-12 16:19:28 -0700390 public void setTextVisibility(boolean visible) {
391 Resources res = getResources();
392 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700393 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700394 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700395 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700396 }
397 mIsTextVisible = visible;
398 }
399
400 public boolean isTextVisible() {
401 return mIsTextVisible;
402 }
403
Winson Chungaffd7b42010-08-20 15:11:56 -0700404 @Override
405 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800406 if (mPrevAlpha != alpha) {
407 mPrevAlpha = alpha;
Winson Chunge22a8e92010-11-12 13:40:58 -0800408 super.onSetAlpha(alpha);
409 }
410 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700411 }
Winson Chung88f33452012-02-23 15:23:44 -0800412
413 @Override
414 public void cancelLongPress() {
415 super.cancelLongPress();
416
417 mLongPressHelper.cancelLongPress();
418 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500419
Chris Wren40c5ed32014-06-24 18:24:23 -0400420 public void applyState() {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500421 int alpha = getResources().getInteger(R.integer.promise_icon_alpha);
Chris Wren40c5ed32014-06-24 18:24:23 -0400422 final int state = getState();
423 if (DEBUG) Log.d(TAG, "applying icon state: " + state);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500424
Chris Wren40c5ed32014-06-24 18:24:23 -0400425 switch(state) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500426 case ShortcutInfo.PACKAGE_STATE_DEFAULT:
427 super.setText(mDefaultText);
428 alpha = 255;
429 break;
430
431 case ShortcutInfo.PACKAGE_STATE_ENQUEUED:
432 setText(R.string.package_state_enqueued);
433 break;
434
435 case ShortcutInfo.PACKAGE_STATE_DOWNLOADING:
436 setText(R.string.package_state_downloading);
437 break;
438
439 case ShortcutInfo.PACKAGE_STATE_INSTALLING:
440 setText(R.string.package_state_installing);
441 break;
442
443 case ShortcutInfo.PACKAGE_STATE_ERROR:
444 setText(R.string.package_state_error);
445 break;
446
447 case ShortcutInfo.PACKAGE_STATE_UNKNOWN:
448 default:
449 setText(R.string.package_state_unknown);
450 break;
451 }
452 if (DEBUG) Log.d(TAG, "setting icon alpha to: " + alpha);
453 Drawable[] drawables = getCompoundDrawables();
454 for (int i = 0; i < drawables.length; i++) {
455 if (drawables[i] != null) {
456 drawables[i].setAlpha(alpha);
457 }
458 }
459 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400460
461 private int getState() {
462 if (! (getTag() instanceof ShortcutInfo)) {
463 return ShortcutInfo.PACKAGE_STATE_DEFAULT;
464 } else {
465 ShortcutInfo info = (ShortcutInfo) getTag();
466 return info.getState();
467 }
468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469}