blob: 54d7e506ea1ffa4d19929e6993308f5e420f3547 [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;
Adam Cohen96bb7982014-07-07 11:58:56 -070069 private boolean mCustomShadowsEnabled = true;
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;
73 private 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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 init();
100 }
101
Winson Chung5f8afe62013-08-12 16:19:28 -0700102 public void onFinishInflate() {
103 super.onFinishInflate();
104
105 // Ensure we are using the right text size
106 LauncherAppState app = LauncherAppState.getInstance();
107 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700108 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700109 }
110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -0800112 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 mBackground = getBackground();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Daniel Sandlere4f98912013-06-25 15:13:26 -0400115 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
Adam Cohen96bb7982014-07-07 11:58:56 -0700116 if (mCustomShadowsEnabled) {
117 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 }
120
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800121 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
122 Bitmap b = info.getIcon(iconCache);
Winson Chungcdef0442013-09-19 17:32:58 -0700123 LauncherAppState app = LauncherAppState.getInstance();
124 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800125
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500126 Drawable iconDrawable = Utilities.createIconDrawable(b);
127 setCompoundDrawables(null, iconDrawable, null, null);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700128 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100129 if (info.contentDescription != null) {
130 setContentDescription(info.contentDescription);
131 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800132 setTag(info);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500133 if (info.isPromise()) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400134 applyState();
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500135 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800136 }
137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 @Override
139 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700140 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 mBackgroundSizeChanged = true;
142 }
143 return super.setFrame(left, top, right, bottom);
144 }
145
146 @Override
147 protected boolean verifyDrawable(Drawable who) {
148 return who == mBackground || super.verifyDrawable(who);
149 }
150
151 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700152 public void setTag(Object tag) {
153 if (tag != null) {
154 LauncherModel.checkItemInfo((ItemInfo) tag);
155 }
156 super.setTag(tag);
Chris Wren40c5ed32014-06-24 18:24:23 -0400157 if (tag instanceof ShortcutInfo) {
158 final ShortcutInfo info = (ShortcutInfo) tag;
159 mDefaultText = info.title;
160 setText(mDefaultText);
161 }
Michael Jurka816474f2012-06-25 14:49:02 -0700162 }
163
164 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800166 if (isPressed()) {
167 // In this case, we have already created the pressed outline on ACTION_DOWN,
168 // so we just need to do an invalidate to trigger draw
169 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800170 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800171 }
172 } else {
173 // Otherwise, either clear the pressed/focused background, or create a background
174 // for the focused state
175 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800176 if (!mStayPressed) {
177 mPressedOrFocusedBackground = null;
178 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800179 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800180 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800181 // In some cases, we get focus before we have been layed out. Set the
182 // background to null so that it will get created when the view is drawn.
183 mPressedOrFocusedBackground = null;
184 } else {
185 mPressedOrFocusedBackground = createGlowingOutline(
186 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
187 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800188 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800189 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800190 }
191 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
192 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800193 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800194 }
195 }
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 Drawable d = mBackground;
198 if (d != null && d.isStateful()) {
199 d.setState(getDrawableState());
200 }
201 super.drawableStateChanged();
202 }
203
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800204 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800205 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800206 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800207 * @param destCanvas the canvas to draw on
208 * @param padding the horizontal and vertical padding to use when drawing
209 */
210 private void drawWithPadding(Canvas destCanvas, int padding) {
211 final Rect clipRect = mTempRect;
212 getDrawingRect(clipRect);
213
214 // adjust the clip rect so that we don't include the text label
215 clipRect.bottom =
216 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
217
218 // Draw the View into the bitmap.
219 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
220 // they set scrollX and scrollY to large values to achieve centered text
221 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800222 destCanvas.scale(getScaleX(), getScaleY(),
223 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800224 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
225 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800226 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800227 destCanvas.restore();
228 }
229
Adam Cohen7397e622013-10-24 12:11:34 -0700230 public void setGlowColor(int color) {
231 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color;
232 }
233
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800234 /**
235 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
236 * Responsibility for the bitmap is transferred to the caller.
237 */
238 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400239 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800240 final Bitmap b = Bitmap.createBitmap(
241 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
242
243 canvas.setBitmap(b);
244 drawWithPadding(canvas, padding);
245 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700246 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247
248 return b;
249 }
250
251 @Override
252 public boolean onTouchEvent(MotionEvent event) {
253 // Call the superclass onTouchEvent first, because sometimes it changes the state to
254 // isPressed() on an ACTION_UP
255 boolean result = super.onTouchEvent(event);
256
257 switch (event.getAction()) {
258 case MotionEvent.ACTION_DOWN:
259 // So that the pressed outline is visible immediately when isPressed() is true,
260 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
261 // to create it)
262 if (mPressedOrFocusedBackground == null) {
263 mPressedOrFocusedBackground = createGlowingOutline(
264 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
265 }
266 // Invalidate so the pressed state is visible, or set a flag so we know that we
267 // have to call invalidate as soon as the state is "pressed"
268 if (isPressed()) {
269 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700270 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800271 } else {
272 mDidInvalidateForPressedState = false;
273 }
Winson Chung88f33452012-02-23 15:23:44 -0800274
275 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800276 break;
277 case MotionEvent.ACTION_CANCEL:
278 case MotionEvent.ACTION_UP:
279 // If we've touched down and up on an item, and it's still not "pressed", then
280 // destroy the pressed outline
281 if (!isPressed()) {
282 mPressedOrFocusedBackground = null;
283 }
Winson Chung88f33452012-02-23 15:23:44 -0800284
285 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800286 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400287 case MotionEvent.ACTION_MOVE:
288 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
289 mLongPressHelper.cancelLongPress();
290 }
291 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800292 }
293 return result;
294 }
295
Michael Jurkaddd62e92011-02-16 17:49:14 -0800296 void setStayPressed(boolean stayPressed) {
297 mStayPressed = stayPressed;
298 if (!stayPressed) {
299 mPressedOrFocusedBackground = null;
300 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800301 setCellLayoutPressedOrFocusedIcon();
302 }
303
304 void setCellLayoutPressedOrFocusedIcon() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700305 if (getParent() instanceof ShortcutAndWidgetContainer) {
306 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700307 if (parent != null) {
308 CellLayout layout = (CellLayout) parent.getParent();
309 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
310 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700311 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800312 }
313
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700314 void clearPressedOrFocusedBackground() {
315 mPressedOrFocusedBackground = null;
316 setCellLayoutPressedOrFocusedIcon();
317 }
318
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800319 Bitmap getPressedOrFocusedBackground() {
320 return mPressedOrFocusedBackground;
321 }
322
323 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400324 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800325 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800326
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 @Override
328 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700329 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700330 super.draw(canvas);
331 return;
332 }
333
Michael Jurkabdb5c532011-02-01 15:05:06 -0800334 final Drawable background = mBackground;
335 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700336 final int scrollX = getScrollX();
337 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800338
Michael Jurkabdb5c532011-02-01 15:05:06 -0800339 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700340 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800341 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800343
344 if ((scrollX | scrollY) == 0) {
345 background.draw(canvas);
346 } else {
347 canvas.translate(scrollX, scrollY);
348 background.draw(canvas);
349 canvas.translate(-scrollX, -scrollY);
350 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800352
353 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800354 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800355 getPaint().clearShadowLayer();
356 super.draw(canvas);
357 return;
358 }
359
Michael Jurkabdb5c532011-02-01 15:05:06 -0800360 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800361 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800362 super.draw(canvas);
363 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700364 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
365 getScrollX() + getWidth(),
366 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800367 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800368 super.draw(canvas);
369 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400371
372 @Override
373 protected void onAttachedToWindow() {
374 super.onAttachedToWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800375 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400376 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377 }
378
379 @Override
380 protected void onDetachedFromWindow() {
381 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800382 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400383 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700384
Adam Cohen477828c2013-09-20 12:05:49 -0700385 @Override
386 public void setTextColor(int color) {
387 mTextColor = color;
388 super.setTextColor(color);
389 }
390
Adam Cohen96bb7982014-07-07 11:58:56 -0700391 @Override
392 public void setTextColor(ColorStateList colors) {
393 mTextColor = colors.getDefaultColor();
394 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700395 }
396
Winson Chung5f8afe62013-08-12 16:19:28 -0700397 public void setTextVisibility(boolean visible) {
398 Resources res = getResources();
399 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700400 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700401 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700402 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700403 }
404 mIsTextVisible = visible;
405 }
406
407 public boolean isTextVisible() {
408 return mIsTextVisible;
409 }
410
Winson Chungaffd7b42010-08-20 15:11:56 -0700411 @Override
412 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800413 if (mPrevAlpha != alpha) {
414 mPrevAlpha = alpha;
Winson Chunge22a8e92010-11-12 13:40:58 -0800415 super.onSetAlpha(alpha);
416 }
417 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700418 }
Winson Chung88f33452012-02-23 15:23:44 -0800419
420 @Override
421 public void cancelLongPress() {
422 super.cancelLongPress();
423
424 mLongPressHelper.cancelLongPress();
425 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500426
Chris Wren40c5ed32014-06-24 18:24:23 -0400427 public void applyState() {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500428 int alpha = getResources().getInteger(R.integer.promise_icon_alpha);
Chris Wren40c5ed32014-06-24 18:24:23 -0400429 final int state = getState();
430 if (DEBUG) Log.d(TAG, "applying icon state: " + state);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500431
Chris Wren40c5ed32014-06-24 18:24:23 -0400432 switch(state) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500433 case ShortcutInfo.PACKAGE_STATE_DEFAULT:
434 super.setText(mDefaultText);
435 alpha = 255;
436 break;
437
438 case ShortcutInfo.PACKAGE_STATE_ENQUEUED:
439 setText(R.string.package_state_enqueued);
440 break;
441
442 case ShortcutInfo.PACKAGE_STATE_DOWNLOADING:
443 setText(R.string.package_state_downloading);
444 break;
445
446 case ShortcutInfo.PACKAGE_STATE_INSTALLING:
447 setText(R.string.package_state_installing);
448 break;
449
450 case ShortcutInfo.PACKAGE_STATE_ERROR:
451 setText(R.string.package_state_error);
452 break;
453
454 case ShortcutInfo.PACKAGE_STATE_UNKNOWN:
455 default:
456 setText(R.string.package_state_unknown);
457 break;
458 }
459 if (DEBUG) Log.d(TAG, "setting icon alpha to: " + alpha);
460 Drawable[] drawables = getCompoundDrawables();
461 for (int i = 0; i < drawables.length; i++) {
462 if (drawables[i] != null) {
463 drawables[i].setAlpha(alpha);
464 }
465 }
466 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400467
468 private int getState() {
469 if (! (getTag() instanceof ShortcutInfo)) {
470 return ShortcutInfo.PACKAGE_STATE_DEFAULT;
471 } else {
472 ShortcutInfo info = (ShortcutInfo) getTag();
473 return info.getState();
474 }
475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476}