blob: 8ef234bb01b16b749423c784bde489e3c4909233 [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 Jurka137142e2011-01-05 20:57:04 -080026import android.graphics.Region;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080028import android.util.AttributeSet;
Sunny Goyal95abbb32014-08-04 10:53:22 -070029import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070030import android.util.TypedValue;
Winson Chungb745afb2015-03-02 11:51:23 -080031import android.view.Gravity;
Sunny Goyal508da152014-08-14 10:53:27 -070032import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080033import android.view.MotionEvent;
Jason Monk02dd7ae2014-04-15 15:23:31 -040034import android.view.ViewConfiguration;
Michael Jurkabdb5c532011-02-01 15:05:06 -080035import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080036
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037/**
38 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
39 * because we want to make the bubble taller than the text and TextView's clip is
40 * too aggressive.
41 */
Michael Jurka08ee7702011-08-11 16:53:35 -070042public class BubbleTextView extends TextView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070043
Sameer Padala8fd74832014-09-08 16:00:29 -070044 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070045
Sunny Goyal508da152014-08-14 10:53:27 -070046 private static final float SHADOW_LARGE_RADIUS = 4.0f;
47 private static final float SHADOW_SMALL_RADIUS = 1.75f;
48 private static final float SHADOW_Y_OFFSET = 2.0f;
49 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
50 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080051 static final float PADDING_V = 3.0f;
52
Winson Chungb745afb2015-03-02 11:51:23 -080053 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080054 private final Drawable mBackground;
55 private final CheckLongPressHelper mLongPressHelper;
56 private final HolographicOutlineHelper mOutlineHelper;
57
58 // TODO: Remove custom background handling code, as no instance of BubbleTextView use any
59 // background.
60 private boolean mBackgroundSizeChanged;
61
Sunny Goyal508da152014-08-14 10:53:27 -070062 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080063
Jason Monk02dd7ae2014-04-15 15:23:31 -040064 private float mSlop;
65
Winson Chung93f98ea2015-03-10 16:28:47 -070066 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070067 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080068 private final boolean mLayoutHorizontal;
69 private final int mIconSize;
70 private final int mIconPaddingSize;
71 private final int mTextSize;
72 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070073
Michael Jurkaddd62e92011-02-16 17:49:14 -080074 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070075 private boolean mIgnorePressedStateChange;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050076
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070078 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 }
80
81 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070082 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 }
84
85 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
86 super(context, attrs, defStyle);
Winson Chungb745afb2015-03-02 11:51:23 -080087 LauncherAppState app = LauncherAppState.getInstance();
88 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070089
Adam Cohen96bb7982014-07-07 11:58:56 -070090 TypedArray a = context.obtainStyledAttributes(attrs,
91 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -070092 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -080093 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
94 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
95 grid.allAppsIconSizePx);
96 mIconPaddingSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconPaddingOverride,
97 grid.iconDrawablePaddingPx);
98 mTextSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_textSizeOverride,
99 grid.allAppsIconTextSizePx);
Winson Chung93f98ea2015-03-10 16:28:47 -0700100 mDeferShadowGenerationOnTouch =
101 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Adam Cohen96bb7982014-07-07 11:58:56 -0700102 a.recycle();
103
Sunny Goyal15872da2014-07-08 15:43:54 -0700104 if (mCustomShadowsEnabled) {
105 // Draw the background itself as the parent is drawn twice.
106 mBackground = getBackground();
107 setBackground(null);
108 } else {
109 mBackground = null;
110 }
Winson Chungb745afb2015-03-02 11:51:23 -0800111
112 // If we are laying out horizontal, then center the text vertically
113 if (mLayoutHorizontal) {
114 setGravity(Gravity.CENTER_VERTICAL);
115 }
116
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800117 mLongPressHelper = new CheckLongPressHelper(this);
118
119 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
120 if (mCustomShadowsEnabled) {
121 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
122 }
123
124 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 }
126
Winson Chung5f8afe62013-08-12 16:19:28 -0700127 public void onFinishInflate() {
128 super.onFinishInflate();
129
130 // Ensure we are using the right text size
Winson Chungb745afb2015-03-02 11:51:23 -0800131 setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
Winson Chung5f8afe62013-08-12 16:19:28 -0700132 }
133
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700134 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
135 boolean setDefaultPadding) {
Sunny Goyal34942622014-08-29 17:20:55 -0700136 applyFromShortcutInfo(info, iconCache, setDefaultPadding, false);
137 }
138
139 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
140 boolean setDefaultPadding, boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800141 Bitmap b = info.getIcon(iconCache);
142
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700143 FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700144 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700145
Winson Chungb745afb2015-03-02 11:51:23 -0800146 setIcon(iconDrawable, mIconSize, setDefaultPadding ? mIconPaddingSize : -1);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100147 if (info.contentDescription != null) {
148 setContentDescription(info.contentDescription);
149 }
Sunny Goyal508da152014-08-14 10:53:27 -0700150 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800151 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700152
Sunny Goyal34942622014-08-29 17:20:55 -0700153 if (promiseStateChanged || info.isPromise()) {
154 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500155 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800156 }
157
Sunny Goyal508da152014-08-14 10:53:27 -0700158 public void applyFromApplicationInfo(AppInfo info) {
Winson Chungb745afb2015-03-02 11:51:23 -0800159 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize, mIconPaddingSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700160 setText(info.title);
161 if (info.contentDescription != null) {
162 setContentDescription(info.contentDescription);
163 }
Winson Chung888b3a12015-03-13 11:14:16 -0700164 // We don't need to check the info since it's not a ShortcutInfo
165 super.setTag(info);
Sunny Goyal508da152014-08-14 10:53:27 -0700166 }
167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 @Override
169 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700170 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 mBackgroundSizeChanged = true;
172 }
173 return super.setFrame(left, top, right, bottom);
174 }
175
176 @Override
177 protected boolean verifyDrawable(Drawable who) {
178 return who == mBackground || super.verifyDrawable(who);
179 }
180
181 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700182 public void setTag(Object tag) {
183 if (tag != null) {
184 LauncherModel.checkItemInfo((ItemInfo) tag);
185 }
186 super.setTag(tag);
187 }
188
189 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700190 public void setPressed(boolean pressed) {
191 super.setPressed(pressed);
192
193 if (!mIgnorePressedStateChange) {
194 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800195 }
Sunny Goyal508da152014-08-14 10:53:27 -0700196 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800197
Winson Chungb745afb2015-03-02 11:51:23 -0800198 /** Returns the icon for this view. */
199 public Drawable getIcon() {
200 return mIcon;
201 }
202
203 /** Returns whether the layout is horizontal. */
204 public boolean isLayoutHorizontal() {
205 return mLayoutHorizontal;
206 }
207
Sunny Goyal508da152014-08-14 10:53:27 -0700208 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800209 if (mIcon instanceof FastBitmapDrawable) {
210 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800212 }
213
214 @Override
215 public boolean onTouchEvent(MotionEvent event) {
216 // Call the superclass onTouchEvent first, because sometimes it changes the state to
217 // isPressed() on an ACTION_UP
218 boolean result = super.onTouchEvent(event);
219
220 switch (event.getAction()) {
221 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700222 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800223 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
224 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700225 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700226 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800227 }
Winson Chung88f33452012-02-23 15:23:44 -0800228
229 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800230 break;
231 case MotionEvent.ACTION_CANCEL:
232 case MotionEvent.ACTION_UP:
233 // If we've touched down and up on an item, and it's still not "pressed", then
234 // destroy the pressed outline
235 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700236 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800237 }
Winson Chung88f33452012-02-23 15:23:44 -0800238
239 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800240 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400241 case MotionEvent.ACTION_MOVE:
242 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
243 mLongPressHelper.cancelLongPress();
244 }
245 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800246 }
247 return result;
248 }
249
Michael Jurkaddd62e92011-02-16 17:49:14 -0800250 void setStayPressed(boolean stayPressed) {
251 mStayPressed = stayPressed;
252 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700253 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700254 } else {
255 if (mPressedBackground == null) {
256 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
257 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800258 }
Sunny Goyal508da152014-08-14 10:53:27 -0700259
260 // Only show the shadow effect when persistent pressed state is set.
261 if (getParent() instanceof ShortcutAndWidgetContainer) {
262 CellLayout layout = (CellLayout) getParent().getParent();
263 layout.setPressedIcon(this, mPressedBackground, mOutlineHelper.shadowBitmapPadding);
264 }
265
266 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800267 }
268
Sunny Goyal508da152014-08-14 10:53:27 -0700269 void clearPressedBackground() {
270 setPressed(false);
271 setStayPressed(false);
272 }
273
274 @Override
275 public boolean onKeyDown(int keyCode, KeyEvent event) {
276 if (super.onKeyDown(keyCode, event)) {
277 // Pre-create shadow so show immediately on click.
278 if (mPressedBackground == null) {
279 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700280 }
Sunny Goyal508da152014-08-14 10:53:27 -0700281 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700282 }
Sunny Goyal508da152014-08-14 10:53:27 -0700283 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800284 }
285
Sunny Goyal508da152014-08-14 10:53:27 -0700286 @Override
287 public boolean onKeyUp(int keyCode, KeyEvent event) {
288 // Unlike touch events, keypress event propagate pressed state change immediately,
289 // without waiting for onClickHandler to execute. Disable pressed state changes here
290 // to avoid flickering.
291 mIgnorePressedStateChange = true;
292 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700293
Sunny Goyal508da152014-08-14 10:53:27 -0700294 mPressedBackground = null;
295 mIgnorePressedStateChange = false;
296 updateIconState();
297 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800298 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 @Override
301 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700302 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700303 super.draw(canvas);
304 return;
305 }
306
Michael Jurkabdb5c532011-02-01 15:05:06 -0800307 final Drawable background = mBackground;
308 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700309 final int scrollX = getScrollX();
310 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800311
Michael Jurkabdb5c532011-02-01 15:05:06 -0800312 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700313 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800314 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800316
317 if ((scrollX | scrollY) == 0) {
318 background.draw(canvas);
319 } else {
320 canvas.translate(scrollX, scrollY);
321 background.draw(canvas);
322 canvas.translate(-scrollX, -scrollY);
323 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800325
326 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800327 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800328 getPaint().clearShadowLayer();
329 super.draw(canvas);
330 return;
331 }
332
Michael Jurkabdb5c532011-02-01 15:05:06 -0800333 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800334 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800335 super.draw(canvas);
336 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700337 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
338 getScrollX() + getWidth(),
339 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800340 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800341 super.draw(canvas);
342 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400344
345 @Override
346 protected void onAttachedToWindow() {
347 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700348
Winson Chung656d11c2010-11-29 17:15:47 -0800349 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700350
Winson Chungb745afb2015-03-02 11:51:23 -0800351 if (mIcon instanceof PreloadIconDrawable) {
352 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700353 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400354 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400355 }
356
357 @Override
358 protected void onDetachedFromWindow() {
359 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800360 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700362
Adam Cohen477828c2013-09-20 12:05:49 -0700363 @Override
364 public void setTextColor(int color) {
365 mTextColor = color;
366 super.setTextColor(color);
367 }
368
Adam Cohen96bb7982014-07-07 11:58:56 -0700369 @Override
370 public void setTextColor(ColorStateList colors) {
371 mTextColor = colors.getDefaultColor();
372 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700373 }
374
Winson Chung5f8afe62013-08-12 16:19:28 -0700375 public void setTextVisibility(boolean visible) {
376 Resources res = getResources();
377 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700378 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700379 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700380 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700381 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700382 }
383
Winson Chungaffd7b42010-08-20 15:11:56 -0700384 @Override
385 protected boolean onSetAlpha(int alpha) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800386 return true;
Winson Chungaffd7b42010-08-20 15:11:56 -0700387 }
Winson Chung88f33452012-02-23 15:23:44 -0800388
389 @Override
390 public void cancelLongPress() {
391 super.cancelLongPress();
392
393 mLongPressHelper.cancelLongPress();
394 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500395
Sunny Goyal34942622014-08-29 17:20:55 -0700396 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700397 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400398 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700399 final boolean isPromise = info.isPromise();
400 final int progressLevel = isPromise ?
401 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
402 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700403
Winson Chungb745afb2015-03-02 11:51:23 -0800404 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700405 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800406 if (mIcon instanceof PreloadIconDrawable) {
407 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700408 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800409 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
410 setIcon(preloadDrawable, mIconSize, -1);
Sunny Goyale755d462014-07-22 13:48:29 -0700411 }
412
413 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700414 if (promiseStateChanged) {
415 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700416 }
Sunny Goyale755d462014-07-22 13:48:29 -0700417 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400418 }
419 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700420
421 private Theme getPreloaderTheme() {
422 Object tag = getTag();
423 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
424 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
425 : R.style.PreloadIcon;
426 Theme theme = sPreloaderThemes.get(style);
427 if (theme == null) {
428 theme = getResources().newTheme();
429 theme.applyStyle(style, true);
430 sPreloaderThemes.put(style, theme);
431 }
432 return theme;
433 }
Winson Chungb745afb2015-03-02 11:51:23 -0800434
435 /**
436 * Sets the icon for this view based on the layout direction.
437 */
438 private Drawable setIcon(Drawable icon, int iconSize, int drawablePadding) {
439 mIcon = icon;
440 if (iconSize != -1) {
441 mIcon.setBounds(0, 0, iconSize, iconSize);
442 }
443 if (mLayoutHorizontal) {
444 setCompoundDrawablesRelative(mIcon, null, null, null);
445 } else {
446 setCompoundDrawablesRelative(null, mIcon, null, null);
447 }
448 if (drawablePadding != -1) {
449 setCompoundDrawablePadding(drawablePadding);
450 }
451 return icon;
452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453}