blob: 5c2bb99468d4e4c46fc230e22e82fec42554f810 [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;
Sunny Goyal95abbb32014-08-04 10:53:22 -070022import android.content.res.Resources.Theme;
Adam Cohen96bb7982014-07-07 11:58:56 -070023import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080024import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Canvas;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080026import android.graphics.Rect;
Michael Jurka137142e2011-01-05 20:57:04 -080027import android.graphics.Region;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080028import android.graphics.Region.Op;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080030import android.util.AttributeSet;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050031import android.util.Log;
Sunny Goyal95abbb32014-08-04 10:53:22 -070032import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070033import android.util.TypedValue;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080034import android.view.MotionEvent;
Jason Monk02dd7ae2014-04-15 15:23:31 -040035import android.view.ViewConfiguration;
Michael Jurkabdb5c532011-02-01 15:05:06 -080036import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080037
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038/**
39 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
40 * because we want to make the bubble taller than the text and TextView's clip is
41 * too aggressive.
42 */
Michael Jurka08ee7702011-08-11 16:53:35 -070043public class BubbleTextView extends TextView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070044
45 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<>(2);
46
Winson Chung88127032010-12-13 12:11:33 -080047 static final float SHADOW_LARGE_RADIUS = 4.0f;
48 static final float SHADOW_SMALL_RADIUS = 1.75f;
49 static final float SHADOW_Y_OFFSET = 2.0f;
Winson Chungc7aef8c2011-09-15 18:53:04 -070050 static final int SHADOW_LARGE_COLOUR = 0xDD000000;
51 static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080052 static final float PADDING_H = 8.0f;
53 static final float PADDING_V = 3.0f;
54
Chris Wrenaeff7ea2014-02-14 16:59:24 -050055 private static final String TAG = "BubbleTextView";
56
57 private static final boolean DEBUG = false;
58
Daniel Sandlere4f98912013-06-25 15:13:26 -040059 private HolographicOutlineHelper mOutlineHelper;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080060 private final Canvas mTempCanvas = new Canvas();
61 private final Rect mTempRect = new Rect();
Michael Jurka38b4f7c2010-12-14 16:46:39 -080062 private boolean mDidInvalidateForPressedState;
63 private Bitmap mPressedOrFocusedBackground;
64 private int mFocusedOutlineColor;
65 private int mFocusedGlowColor;
66 private int mPressedOutlineColor;
67 private int mPressedGlowColor;
68
Jason Monk02dd7ae2014-04-15 15:23:31 -040069 private float mSlop;
70
Adam Cohen477828c2013-09-20 12:05:49 -070071 private int mTextColor;
Sunny Goyal15872da2014-07-08 15:43:54 -070072 private final boolean mCustomShadowsEnabled;
Winson Chung5f8afe62013-08-12 16:19:28 -070073 private boolean mIsTextVisible;
74
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075 private boolean mBackgroundSizeChanged;
Sunny Goyal15872da2014-07-08 15:43:54 -070076 private final Drawable mBackground;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Michael Jurkaddd62e92011-02-16 17:49:14 -080078 private boolean mStayPressed;
Winson Chung88f33452012-02-23 15:23:44 -080079 private CheckLongPressHelper mLongPressHelper;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050080
Chris Wrenaeff7ea2014-02-14 16:59:24 -050081 private CharSequence mDefaultText = "";
Michael Jurkaddd62e92011-02-16 17:49:14 -080082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070084 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 }
86
87 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070088 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 }
90
91 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
92 super(context, attrs, defStyle);
Adam Cohen96bb7982014-07-07 11:58:56 -070093
94 Resources res = context.getResources();
95 TypedArray a = context.obtainStyledAttributes(attrs,
96 R.styleable.BubbleTextView, defStyle, 0);
97 setGlowColor(a.getColor(R.styleable.BubbleTextView_glowColor,
98 res.getColor(R.color.outline_color)));
99 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
100 a.recycle();
101
Sunny Goyal15872da2014-07-08 15:43:54 -0700102 if (mCustomShadowsEnabled) {
103 // Draw the background itself as the parent is drawn twice.
104 mBackground = getBackground();
105 setBackground(null);
106 } else {
107 mBackground = null;
108 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 init();
110 }
111
Winson Chung5f8afe62013-08-12 16:19:28 -0700112 public void onFinishInflate() {
113 super.onFinishInflate();
114
115 // Ensure we are using the right text size
116 LauncherAppState app = LauncherAppState.getInstance();
117 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700118 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700119 }
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private void init() {
Winson Chung88f33452012-02-23 15:23:44 -0800122 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Daniel Sandlere4f98912013-06-25 15:13:26 -0400124 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
Adam Cohen96bb7982014-07-07 11:58:56 -0700125 if (mCustomShadowsEnabled) {
126 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 }
129
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700130 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
131 boolean setDefaultPadding) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800132 Bitmap b = info.getIcon(iconCache);
Winson Chungcdef0442013-09-19 17:32:58 -0700133 LauncherAppState app = LauncherAppState.getInstance();
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800134
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700135 FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700136 iconDrawable.setGhostModeEnabled(info.isDisabled);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700137
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500138 setCompoundDrawables(null, iconDrawable, null, null);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700139 if (setDefaultPadding) {
140 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
141 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
142 }
Kenny Guyc2bd8102014-06-30 12:30:31 +0100143 if (info.contentDescription != null) {
144 setContentDescription(info.contentDescription);
145 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800146 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700147
148 if (info.wasPromise) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400149 applyState();
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500150 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800151 }
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 @Override
154 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 mBackgroundSizeChanged = true;
157 }
158 return super.setFrame(left, top, right, bottom);
159 }
160
161 @Override
162 protected boolean verifyDrawable(Drawable who) {
163 return who == mBackground || super.verifyDrawable(who);
164 }
165
166 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700167 public void setTag(Object tag) {
168 if (tag != null) {
169 LauncherModel.checkItemInfo((ItemInfo) tag);
170 }
171 super.setTag(tag);
Chris Wren40c5ed32014-06-24 18:24:23 -0400172 if (tag instanceof ShortcutInfo) {
173 final ShortcutInfo info = (ShortcutInfo) tag;
174 mDefaultText = info.title;
175 setText(mDefaultText);
176 }
Michael Jurka816474f2012-06-25 14:49:02 -0700177 }
178
179 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 protected void drawableStateChanged() {
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800181 if (isPressed()) {
182 // In this case, we have already created the pressed outline on ACTION_DOWN,
183 // so we just need to do an invalidate to trigger draw
184 if (!mDidInvalidateForPressedState) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800185 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800186 }
187 } else {
188 // Otherwise, either clear the pressed/focused background, or create a background
189 // for the focused state
190 final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800191 if (!mStayPressed) {
192 mPressedOrFocusedBackground = null;
193 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800194 if (isFocused()) {
Gilles Debunnec7869522011-11-28 18:03:56 -0800195 if (getLayout() == null) {
Patrick Dubroya017c032011-03-09 15:58:32 -0800196 // In some cases, we get focus before we have been layed out. Set the
197 // background to null so that it will get created when the view is drawn.
198 mPressedOrFocusedBackground = null;
199 } else {
200 mPressedOrFocusedBackground = createGlowingOutline(
201 mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
202 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800203 mStayPressed = false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800204 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800205 }
206 final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
207 if (!backgroundEmptyBefore && backgroundEmptyNow) {
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800208 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800209 }
210 }
211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 Drawable d = mBackground;
213 if (d != null && d.isStateful()) {
214 d.setState(getDrawableState());
215 }
216 super.drawableStateChanged();
217 }
218
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800219 /**
Patrick Dubroycd953712011-02-28 15:16:42 -0800220 * Draw this BubbleTextView into the given Canvas.
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800221 *
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800222 * @param destCanvas the canvas to draw on
223 * @param padding the horizontal and vertical padding to use when drawing
224 */
225 private void drawWithPadding(Canvas destCanvas, int padding) {
226 final Rect clipRect = mTempRect;
227 getDrawingRect(clipRect);
228
229 // adjust the clip rect so that we don't include the text label
230 clipRect.bottom =
231 getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
232
233 // Draw the View into the bitmap.
234 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
235 // they set scrollX and scrollY to large values to achieve centered text
236 destCanvas.save();
Winson Chungeecf02d2012-03-02 17:14:58 -0800237 destCanvas.scale(getScaleX(), getScaleY(),
238 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800239 destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
240 destCanvas.clipRect(clipRect, Op.REPLACE);
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800241 draw(destCanvas);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800242 destCanvas.restore();
243 }
244
Adam Cohen7397e622013-10-24 12:11:34 -0700245 public void setGlowColor(int color) {
246 mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color;
247 }
248
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800249 /**
250 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
251 * Responsibility for the bitmap is transferred to the caller.
252 */
253 private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400254 final int padding = mOutlineHelper.mMaxOuterBlurRadius;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800255 final Bitmap b = Bitmap.createBitmap(
256 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
257
258 canvas.setBitmap(b);
259 drawWithPadding(canvas, padding);
260 mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700261 canvas.setBitmap(null);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800262
263 return b;
264 }
265
266 @Override
267 public boolean onTouchEvent(MotionEvent event) {
268 // Call the superclass onTouchEvent first, because sometimes it changes the state to
269 // isPressed() on an ACTION_UP
270 boolean result = super.onTouchEvent(event);
271
272 switch (event.getAction()) {
273 case MotionEvent.ACTION_DOWN:
274 // So that the pressed outline is visible immediately when isPressed() is true,
275 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
276 // to create it)
277 if (mPressedOrFocusedBackground == null) {
278 mPressedOrFocusedBackground = createGlowingOutline(
279 mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
280 }
281 // Invalidate so the pressed state is visible, or set a flag so we know that we
282 // have to call invalidate as soon as the state is "pressed"
283 if (isPressed()) {
284 mDidInvalidateForPressedState = true;
Michael Jurkae6235dd2011-10-04 15:02:05 -0700285 setCellLayoutPressedOrFocusedIcon();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800286 } else {
287 mDidInvalidateForPressedState = false;
288 }
Winson Chung88f33452012-02-23 15:23:44 -0800289
290 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800291 break;
292 case MotionEvent.ACTION_CANCEL:
293 case MotionEvent.ACTION_UP:
294 // If we've touched down and up on an item, and it's still not "pressed", then
295 // destroy the pressed outline
296 if (!isPressed()) {
297 mPressedOrFocusedBackground = null;
298 }
Winson Chung88f33452012-02-23 15:23:44 -0800299
300 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800301 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400302 case MotionEvent.ACTION_MOVE:
303 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
304 mLongPressHelper.cancelLongPress();
305 }
306 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800307 }
308 return result;
309 }
310
Michael Jurkaddd62e92011-02-16 17:49:14 -0800311 void setStayPressed(boolean stayPressed) {
312 mStayPressed = stayPressed;
313 if (!stayPressed) {
314 mPressedOrFocusedBackground = null;
315 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800316 setCellLayoutPressedOrFocusedIcon();
317 }
318
319 void setCellLayoutPressedOrFocusedIcon() {
Sunny Goyal95abbb32014-08-04 10:53:22 -0700320 // Disable pressed state when the icon is in preloader state.
321 if ((getParent() instanceof ShortcutAndWidgetContainer) &&
322 !(getCompoundDrawables()[1] instanceof PreloadIconDrawable)){
Michael Jurkaa52570f2012-03-20 03:18:20 -0700323 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
Adam Cohen76fc0852011-06-17 13:26:23 -0700324 if (parent != null) {
325 CellLayout layout = (CellLayout) parent.getParent();
326 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
327 }
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700328 }
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800329 }
330
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700331 void clearPressedOrFocusedBackground() {
332 mPressedOrFocusedBackground = null;
333 setCellLayoutPressedOrFocusedIcon();
334 }
335
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800336 Bitmap getPressedOrFocusedBackground() {
337 return mPressedOrFocusedBackground;
338 }
339
340 int getPressedOrFocusedBackgroundPadding() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400341 return mOutlineHelper.mMaxOuterBlurRadius / 2;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800342 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 @Override
345 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700346 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700347 super.draw(canvas);
348 return;
349 }
350
Michael Jurkabdb5c532011-02-01 15:05:06 -0800351 final Drawable background = mBackground;
352 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700353 final int scrollX = getScrollX();
354 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800355
Michael Jurkabdb5c532011-02-01 15:05:06 -0800356 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700357 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800358 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800360
361 if ((scrollX | scrollY) == 0) {
362 background.draw(canvas);
363 } else {
364 canvas.translate(scrollX, scrollY);
365 background.draw(canvas);
366 canvas.translate(-scrollX, -scrollY);
367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800369
370 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800371 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800372 getPaint().clearShadowLayer();
373 super.draw(canvas);
374 return;
375 }
376
Michael Jurkabdb5c532011-02-01 15:05:06 -0800377 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800378 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800379 super.draw(canvas);
380 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700381 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
382 getScrollX() + getWidth(),
383 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800384 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800385 super.draw(canvas);
386 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400388
389 @Override
390 protected void onAttachedToWindow() {
391 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700392
Winson Chung656d11c2010-11-29 17:15:47 -0800393 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700394 Drawable top = getCompoundDrawables()[1];
395
396 if (top instanceof PreloadIconDrawable) {
397 ((PreloadIconDrawable) top).applyTheme(getPreloaderTheme());
398 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400399 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400400 }
401
402 @Override
403 protected void onDetachedFromWindow() {
404 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800405 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700407
Adam Cohen477828c2013-09-20 12:05:49 -0700408 @Override
409 public void setTextColor(int color) {
410 mTextColor = color;
411 super.setTextColor(color);
412 }
413
Adam Cohen96bb7982014-07-07 11:58:56 -0700414 @Override
415 public void setTextColor(ColorStateList colors) {
416 mTextColor = colors.getDefaultColor();
417 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700418 }
419
Winson Chung5f8afe62013-08-12 16:19:28 -0700420 public void setTextVisibility(boolean visible) {
421 Resources res = getResources();
422 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700423 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700424 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700425 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700426 }
427 mIsTextVisible = visible;
428 }
429
430 public boolean isTextVisible() {
431 return mIsTextVisible;
432 }
433
Winson Chungaffd7b42010-08-20 15:11:56 -0700434 @Override
435 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800436 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700437 }
Winson Chung88f33452012-02-23 15:23:44 -0800438
439 @Override
440 public void cancelLongPress() {
441 super.cancelLongPress();
442
443 mLongPressHelper.cancelLongPress();
444 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500445
Chris Wren40c5ed32014-06-24 18:24:23 -0400446 public void applyState() {
Sunny Goyale755d462014-07-22 13:48:29 -0700447 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400448 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyale755d462014-07-22 13:48:29 -0700449 final int state = info.getState();
450
451 final int progressLevel;
452 if (DEBUG) Log.d(TAG, "applying icon state: " + state);
453
454 switch(state) {
455 case ShortcutInfo.PACKAGE_STATE_DEFAULT:
456 progressLevel = 100;
457 break;
458
459 case ShortcutInfo.PACKAGE_STATE_INSTALLING:
460 setText(R.string.package_state_installing);
461 progressLevel = info.getProgress();
462 break;
463
464 case ShortcutInfo.PACKAGE_STATE_ERROR:
465 case ShortcutInfo.PACKAGE_STATE_UNKNOWN:
466 default:
467 progressLevel = 0;
468 setText(R.string.package_state_unknown);
469 break;
470 }
471
472 Drawable[] drawables = getCompoundDrawables();
473 Drawable top = drawables[1];
474 if (top != null) {
475 final PreloadIconDrawable preloadDrawable;
476 if (top instanceof PreloadIconDrawable) {
477 preloadDrawable = (PreloadIconDrawable) top;
478 } else {
Sunny Goyal95abbb32014-08-04 10:53:22 -0700479 preloadDrawable = new PreloadIconDrawable(top, getPreloaderTheme());
Sunny Goyale755d462014-07-22 13:48:29 -0700480 setCompoundDrawables(drawables[0], preloadDrawable, drawables[2], drawables[3]);
481 }
482
483 preloadDrawable.setLevel(progressLevel);
484 if (state == ShortcutInfo.PACKAGE_STATE_DEFAULT) {
485 preloadDrawable.maybePerformFinishedAnimation();
486 }
487
488 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400489 }
490 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700491
492 private Theme getPreloaderTheme() {
493 Object tag = getTag();
494 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
495 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
496 : R.style.PreloadIcon;
497 Theme theme = sPreloaderThemes.get(style);
498 if (theme == null) {
499 theme = getResources().newTheme();
500 theme.applyStyle(style, true);
501 sPreloaderThemes.put(style, theme);
502 }
503 return theme;
504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505}