blob: 95300c133e4a8617e11225007d665718e233ff7f [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
77 private int mState;
78
79 private CharSequence mDefaultText = "";
Michael Jurkaddd62e92011-02-16 17:49:14 -080080
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 public BubbleTextView(Context context) {
82 super(context);
83 init();
84 }
85
86 public BubbleTextView(Context context, AttributeSet attrs) {
87 super(context, attrs);
88 init();
89 }
90
91 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
92 super(context, attrs, defStyle);
93 init();
94 }
95
Winson Chung5f8afe62013-08-12 16:19:28 -070096 public void onFinishInflate() {
97 super.onFinishInflate();
98
99 // Ensure we are using the right text size
100 LauncherAppState app = LauncherAppState.getInstance();
101 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700102 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Adam Cohen477828c2013-09-20 12:05:49 -0700103 setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
Winson Chung5f8afe62013-08-12 16:19:28 -0700104 }
105
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -0800107 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 mBackground = getBackground();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Daniel Sandlere4f98912013-06-25 15:13:26 -0400110 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
111
Winson Chung656d11c2010-11-29 17:15:47 -0800112 final Resources res = getContext().getResources();
Winson Chung7d3810d2011-10-07 13:11:56 -0700113 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor =
Adam Cohen410f3cd2013-09-22 12:09:32 -0700114 res.getColor(R.color.outline_color);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800115
116 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 }
118
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800119 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
120 Bitmap b = info.getIcon(iconCache);
Winson Chungcdef0442013-09-19 17:32:58 -0700121 LauncherAppState app = LauncherAppState.getInstance();
122 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800123
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500124 Drawable iconDrawable = Utilities.createIconDrawable(b);
125 setCompoundDrawables(null, iconDrawable, null, null);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700126 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800127 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800128 setTag(info);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500129 if (info.isPromise()) {
130 setState(ShortcutInfo.PACKAGE_STATE_UNKNOWN); // TODO: persist this state somewhere
131 }
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);
153 }
154
155 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800157 if (isPressed()) {
158 // In this case, we have already created the pressed outline on ACTION_DOWN,
159 // so we just need to do an invalidate to trigger draw
160 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800161 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800162 }
163 } else {
164 // Otherwise, either clear the pressed/focused background, or create a background
165 // for the focused state
166 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800167 if (!mStayPressed) {
168 mPressedOrFocusedBackground = null;
169 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800170 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800171 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800172 // In some cases, we get focus before we have been layed out. Set the
173 // background to null so that it will get created when the view is drawn.
174 mPressedOrFocusedBackground = null;
175 } else {
176 mPressedOrFocusedBackground = createGlowingOutline(
177 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
178 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800179 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800180 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800181 }
182 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
183 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800184 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800185 }
186 }
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 Drawable d = mBackground;
189 if (d != null && d.isStateful()) {
190 d.setState(getDrawableState());
191 }
192 super.drawableStateChanged();
193 }
194
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800195 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800196 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800197 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800198 * @param destCanvas the canvas to draw on
199 * @param padding the horizontal and vertical padding to use when drawing
200 */
201 private void drawWithPadding(Canvas destCanvas, int padding) {
202 final Rect clipRect = mTempRect;
203 getDrawingRect(clipRect);
204
205 // adjust the clip rect so that we don't include the text label
206 clipRect.bottom =
207 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
208
209 // Draw the View into the bitmap.
210 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
211 // they set scrollX and scrollY to large values to achieve centered text
212 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800213 destCanvas.scale(getScaleX(), getScaleY(),
214 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800215 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
216 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800217 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800218 destCanvas.restore();
219 }
220
Adam Cohen7397e622013-10-24 12:11:34 -0700221 public void setGlowColor(int color) {
222 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color;
223 }
224
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800225 /**
226 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
227 * Responsibility for the bitmap is transferred to the caller.
228 */
229 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400230 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800231 final Bitmap b = Bitmap.createBitmap(
232 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
233
234 canvas.setBitmap(b);
235 drawWithPadding(canvas, padding);
236 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700237 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800238
239 return b;
240 }
241
242 @Override
243 public boolean onTouchEvent(MotionEvent event) {
244 // Call the superclass onTouchEvent first, because sometimes it changes the state to
245 // isPressed() on an ACTION_UP
246 boolean result = super.onTouchEvent(event);
247
248 switch (event.getAction()) {
249 case MotionEvent.ACTION_DOWN:
250 // So that the pressed outline is visible immediately when isPressed() is true,
251 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
252 // to create it)
253 if (mPressedOrFocusedBackground == null) {
254 mPressedOrFocusedBackground = createGlowingOutline(
255 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
256 }
257 // Invalidate so the pressed state is visible, or set a flag so we know that we
258 // have to call invalidate as soon as the state is "pressed"
259 if (isPressed()) {
260 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700261 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800262 } else {
263 mDidInvalidateForPressedState = false;
264 }
Winson Chung88f33452012-02-23 15:23:44 -0800265
266 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800267 break;
268 case MotionEvent.ACTION_CANCEL:
269 case MotionEvent.ACTION_UP:
270 // If we've touched down and up on an item, and it's still not "pressed", then
271 // destroy the pressed outline
272 if (!isPressed()) {
273 mPressedOrFocusedBackground = null;
274 }
Winson Chung88f33452012-02-23 15:23:44 -0800275
276 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800277 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400278 case MotionEvent.ACTION_MOVE:
279 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
280 mLongPressHelper.cancelLongPress();
281 }
282 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800283 }
284 return result;
285 }
286
Michael Jurkaddd62e92011-02-16 17:49:14 -0800287 void setStayPressed(boolean stayPressed) {
288 mStayPressed = stayPressed;
289 if (!stayPressed) {
290 mPressedOrFocusedBackground = null;
291 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800292 setCellLayoutPressedOrFocusedIcon();
293 }
294
295 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700296 if (getParent() instanceof ShortcutAndWidgetContainer) {
297 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700298 if (parent != null) {
299 CellLayout layout = (CellLayout) parent.getParent();
300 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
301 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700302 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800303 }
304
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700305 void clearPressedOrFocusedBackground() {
306 mPressedOrFocusedBackground = null;
307 setCellLayoutPressedOrFocusedIcon();
308 }
309
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800310 Bitmap getPressedOrFocusedBackground() {
311 return mPressedOrFocusedBackground;
312 }
313
314 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400315 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800316 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800317
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 @Override
319 public void draw(Canvas canvas) {
Adam Cohen477828c2013-09-20 12:05:49 -0700320 if (!mShadowsEnabled) {
321 super.draw(canvas);
322 return;
323 }
324
Michael Jurkabdb5c532011-02-01 15:05:06 -0800325 final Drawable background = mBackground;
326 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700327 final int scrollX = getScrollX();
328 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800329
Michael Jurkabdb5c532011-02-01 15:05:06 -0800330 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700331 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800332 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800334
335 if ((scrollX | scrollY) == 0) {
336 background.draw(canvas);
337 } else {
338 canvas.translate(scrollX, scrollY);
339 background.draw(canvas);
340 canvas.translate(-scrollX, -scrollY);
341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800343
344 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800345 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800346 getPaint().clearShadowLayer();
347 super.draw(canvas);
348 return;
349 }
350
Michael Jurkabdb5c532011-02-01 15:05:06 -0800351 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800352 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800353 super.draw(canvas);
354 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700355 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
356 getScrollX() + getWidth(),
357 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800358 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800359 super.draw(canvas);
360 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400362
363 @Override
364 protected void onAttachedToWindow() {
365 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800366 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400367 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 }
369
370 @Override
371 protected void onDetachedFromWindow() {
372 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800373 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700375
Adam Cohen477828c2013-09-20 12:05:49 -0700376 @Override
377 public void setTextColor(int color) {
378 mTextColor = color;
379 super.setTextColor(color);
380 }
381
382 public void setShadowsEnabled(boolean enabled) {
383 mShadowsEnabled = enabled;
384 getPaint().clearShadowLayer();
385 invalidate();
386 }
387
Winson Chung5f8afe62013-08-12 16:19:28 -0700388 public void setTextVisibility(boolean visible) {
389 Resources res = getResources();
390 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700391 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700392 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700393 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700394 }
395 mIsTextVisible = visible;
396 }
397
398 public boolean isTextVisible() {
399 return mIsTextVisible;
400 }
401
Winson Chungaffd7b42010-08-20 15:11:56 -0700402 @Override
403 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800404 if (mPrevAlpha != alpha) {
405 mPrevAlpha = alpha;
Winson Chunge22a8e92010-11-12 13:40:58 -0800406 super.onSetAlpha(alpha);
407 }
408 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700409 }
Winson Chung88f33452012-02-23 15:23:44 -0800410
411 @Override
412 public void cancelLongPress() {
413 super.cancelLongPress();
414
415 mLongPressHelper.cancelLongPress();
416 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500417
418 public void setState(int state) {
419 if (mState == ShortcutInfo.PACKAGE_STATE_DEFAULT && mState != state) {
420 mDefaultText = getText();
421 }
422 mState = state;
423 applyState();
424 }
425
426 private void applyState() {
427 int alpha = getResources().getInteger(R.integer.promise_icon_alpha);
428 if (DEBUG) Log.d(TAG, "applying icon state: " + mState);
429
430 switch(mState) {
431 case ShortcutInfo.PACKAGE_STATE_DEFAULT:
432 super.setText(mDefaultText);
433 alpha = 255;
434 break;
435
436 case ShortcutInfo.PACKAGE_STATE_ENQUEUED:
437 setText(R.string.package_state_enqueued);
438 break;
439
440 case ShortcutInfo.PACKAGE_STATE_DOWNLOADING:
441 setText(R.string.package_state_downloading);
442 break;
443
444 case ShortcutInfo.PACKAGE_STATE_INSTALLING:
445 setText(R.string.package_state_installing);
446 break;
447
448 case ShortcutInfo.PACKAGE_STATE_ERROR:
449 setText(R.string.package_state_error);
450 break;
451
452 case ShortcutInfo.PACKAGE_STATE_UNKNOWN:
453 default:
454 setText(R.string.package_state_unknown);
455 break;
456 }
457 if (DEBUG) Log.d(TAG, "setting icon alpha to: " + alpha);
458 Drawable[] drawables = getCompoundDrawables();
459 for (int i = 0; i < drawables.length; i++) {
460 if (drawables[i] != null) {
461 drawables[i].setAlpha(alpha);
462 }
463 }
464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465}