blob: 57dcea0444e28a66912b95cfc5844002e256224d [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);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500139 if (info.isPromise()) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400140 applyState();
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500141 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800142 }
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 @Override
145 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700146 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 mBackgroundSizeChanged = true;
148 }
149 return super.setFrame(left, top, right, bottom);
150 }
151
152 @Override
153 protected boolean verifyDrawable(Drawable who) {
154 return who == mBackground || super.verifyDrawable(who);
155 }
156
157 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700158 public void setTag(Object tag) {
159 if (tag != null) {
160 LauncherModel.checkItemInfo((ItemInfo) tag);
161 }
162 super.setTag(tag);
Chris Wren40c5ed32014-06-24 18:24:23 -0400163 if (tag instanceof ShortcutInfo) {
164 final ShortcutInfo info = (ShortcutInfo) tag;
165 mDefaultText = info.title;
166 setText(mDefaultText);
167 }
Michael Jurka816474f2012-06-25 14:49:02 -0700168 }
169
170 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800172 if (isPressed()) {
173 // In this case, we have already created the pressed outline on ACTION_DOWN,
174 // so we just need to do an invalidate to trigger draw
175 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800176 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800177 }
178 } else {
179 // Otherwise, either clear the pressed/focused background, or create a background
180 // for the focused state
181 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800182 if (!mStayPressed) {
183 mPressedOrFocusedBackground = null;
184 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800185 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800186 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800187 // In some cases, we get focus before we have been layed out. Set the
188 // background to null so that it will get created when the view is drawn.
189 mPressedOrFocusedBackground = null;
190 } else {
191 mPressedOrFocusedBackground = createGlowingOutline(
192 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
193 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800194 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800195 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800196 }
197 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
198 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800199 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800200 }
201 }
202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 Drawable d = mBackground;
204 if (d != null && d.isStateful()) {
205 d.setState(getDrawableState());
206 }
207 super.drawableStateChanged();
208 }
209
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800210 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800211 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800212 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800213 * @param destCanvas the canvas to draw on
214 * @param padding the horizontal and vertical padding to use when drawing
215 */
216 private void drawWithPadding(Canvas destCanvas, int padding) {
217 final Rect clipRect = mTempRect;
218 getDrawingRect(clipRect);
219
220 // adjust the clip rect so that we don't include the text label
221 clipRect.bottom =
222 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
223
224 // Draw the View into the bitmap.
225 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
226 // they set scrollX and scrollY to large values to achieve centered text
227 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800228 destCanvas.scale(getScaleX(), getScaleY(),
229 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800230 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
231 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800232 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800233 destCanvas.restore();
234 }
235
Adam Cohen7397e622013-10-24 12:11:34 -0700236 public void setGlowColor(int color) {
237 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color;
238 }
239
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800240 /**
241 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
242 * Responsibility for the bitmap is transferred to the caller.
243 */
244 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400245 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800246 final Bitmap b = Bitmap.createBitmap(
247 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
248
249 canvas.setBitmap(b);
250 drawWithPadding(canvas, padding);
251 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700252 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800253
254 return b;
255 }
256
257 @Override
258 public boolean onTouchEvent(MotionEvent event) {
259 // Call the superclass onTouchEvent first, because sometimes it changes the state to
260 // isPressed() on an ACTION_UP
261 boolean result = super.onTouchEvent(event);
262
263 switch (event.getAction()) {
264 case MotionEvent.ACTION_DOWN:
265 // So that the pressed outline is visible immediately when isPressed() is true,
266 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
267 // to create it)
268 if (mPressedOrFocusedBackground == null) {
269 mPressedOrFocusedBackground = createGlowingOutline(
270 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
271 }
272 // Invalidate so the pressed state is visible, or set a flag so we know that we
273 // have to call invalidate as soon as the state is "pressed"
274 if (isPressed()) {
275 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700276 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800277 } else {
278 mDidInvalidateForPressedState = false;
279 }
Winson Chung88f33452012-02-23 15:23:44 -0800280
281 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800282 break;
283 case MotionEvent.ACTION_CANCEL:
284 case MotionEvent.ACTION_UP:
285 // If we've touched down and up on an item, and it's still not "pressed", then
286 // destroy the pressed outline
287 if (!isPressed()) {
288 mPressedOrFocusedBackground = null;
289 }
Winson Chung88f33452012-02-23 15:23:44 -0800290
291 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800292 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400293 case MotionEvent.ACTION_MOVE:
294 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
295 mLongPressHelper.cancelLongPress();
296 }
297 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800298 }
299 return result;
300 }
301
Michael Jurkaddd62e92011-02-16 17:49:14 -0800302 void setStayPressed(boolean stayPressed) {
303 mStayPressed = stayPressed;
304 if (!stayPressed) {
305 mPressedOrFocusedBackground = null;
306 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800307 setCellLayoutPressedOrFocusedIcon();
308 }
309
310 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700311 if (getParent() instanceof ShortcutAndWidgetContainer) {
312 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700313 if (parent != null) {
314 CellLayout layout = (CellLayout) parent.getParent();
315 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
316 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700317 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800318 }
319
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700320 void clearPressedOrFocusedBackground() {
321 mPressedOrFocusedBackground = null;
322 setCellLayoutPressedOrFocusedIcon();
323 }
324
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800325 Bitmap getPressedOrFocusedBackground() {
326 return mPressedOrFocusedBackground;
327 }
328
329 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400330 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800331 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 @Override
334 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700335 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700336 super.draw(canvas);
337 return;
338 }
339
Michael Jurkabdb5c532011-02-01 15:05:06 -0800340 final Drawable background = mBackground;
341 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700342 final int scrollX = getScrollX();
343 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800344
Michael Jurkabdb5c532011-02-01 15:05:06 -0800345 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700346 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800347 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800349
350 if ((scrollX | scrollY) == 0) {
351 background.draw(canvas);
352 } else {
353 canvas.translate(scrollX, scrollY);
354 background.draw(canvas);
355 canvas.translate(-scrollX, -scrollY);
356 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800358
359 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800360 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800361 getPaint().clearShadowLayer();
362 super.draw(canvas);
363 return;
364 }
365
Michael Jurkabdb5c532011-02-01 15:05:06 -0800366 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800367 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800368 super.draw(canvas);
369 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700370 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
371 getScrollX() + getWidth(),
372 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800373 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800374 super.draw(canvas);
375 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377
378 @Override
379 protected void onAttachedToWindow() {
380 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800381 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400382 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400383 }
384
385 @Override
386 protected void onDetachedFromWindow() {
387 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800388 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400389 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700390
Adam Cohen477828c2013-09-20 12:05:49 -0700391 @Override
392 public void setTextColor(int color) {
393 mTextColor = color;
394 super.setTextColor(color);
395 }
396
Adam Cohen96bb7982014-07-07 11:58:56 -0700397 @Override
398 public void setTextColor(ColorStateList colors) {
399 mTextColor = colors.getDefaultColor();
400 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700401 }
402
Winson Chung5f8afe62013-08-12 16:19:28 -0700403 public void setTextVisibility(boolean visible) {
404 Resources res = getResources();
405 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700406 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700407 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700408 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700409 }
410 mIsTextVisible = visible;
411 }
412
413 public boolean isTextVisible() {
414 return mIsTextVisible;
415 }
416
Winson Chungaffd7b42010-08-20 15:11:56 -0700417 @Override
418 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800419 if (mPrevAlpha != alpha) {
420 mPrevAlpha = alpha;
Winson Chunge22a8e92010-11-12 13:40:58 -0800421 super.onSetAlpha(alpha);
422 }
423 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700424 }
Winson Chung88f33452012-02-23 15:23:44 -0800425
426 @Override
427 public void cancelLongPress() {
428 super.cancelLongPress();
429
430 mLongPressHelper.cancelLongPress();
431 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500432
Chris Wren40c5ed32014-06-24 18:24:23 -0400433 public void applyState() {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500434 int alpha = getResources().getInteger(R.integer.promise_icon_alpha);
Chris Wren40c5ed32014-06-24 18:24:23 -0400435 final int state = getState();
436 if (DEBUG) Log.d(TAG, "applying icon state: " + state);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500437
Chris Wren40c5ed32014-06-24 18:24:23 -0400438 switch(state) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500439 case ShortcutInfo.PACKAGE_STATE_DEFAULT:
440 super.setText(mDefaultText);
441 alpha = 255;
442 break;
443
444 case ShortcutInfo.PACKAGE_STATE_ENQUEUED:
445 setText(R.string.package_state_enqueued);
446 break;
447
448 case ShortcutInfo.PACKAGE_STATE_DOWNLOADING:
449 setText(R.string.package_state_downloading);
450 break;
451
452 case ShortcutInfo.PACKAGE_STATE_INSTALLING:
453 setText(R.string.package_state_installing);
454 break;
455
456 case ShortcutInfo.PACKAGE_STATE_ERROR:
457 setText(R.string.package_state_error);
458 break;
459
460 case ShortcutInfo.PACKAGE_STATE_UNKNOWN:
461 default:
462 setText(R.string.package_state_unknown);
463 break;
464 }
465 if (DEBUG) Log.d(TAG, "setting icon alpha to: " + alpha);
466 Drawable[] drawables = getCompoundDrawables();
467 for (int i = 0; i < drawables.length; i++) {
468 if (drawables[i] != null) {
469 drawables[i].setAlpha(alpha);
470 }
471 }
472 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400473
474 private int getState() {
475 if (! (getTag() instanceof ShortcutInfo)) {
476 return ShortcutInfo.PACKAGE_STATE_DEFAULT;
477 } else {
478 ShortcutInfo info = (ShortcutInfo) getTag();
479 return info.getState();
480 }
481 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482}