blob: ae6ebba34fa0b437acbee6f98425940beff3015a [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
Sunny Goyal34b65272015-03-11 16:56:52 -070037import com.android.launcher3.IconCache.IconLoadRequest;
38
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039/**
40 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
41 * because we want to make the bubble taller than the text and TextView's clip is
42 * too aggressive.
43 */
Michael Jurka08ee7702011-08-11 16:53:35 -070044public class BubbleTextView extends TextView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070045
Sameer Padala8fd74832014-09-08 16:00:29 -070046 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070047
Sunny Goyal508da152014-08-14 10:53:27 -070048 private static final float SHADOW_LARGE_RADIUS = 4.0f;
49 private static final float SHADOW_SMALL_RADIUS = 1.75f;
50 private static final float SHADOW_Y_OFFSET = 2.0f;
51 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
52 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080053 static final float PADDING_V = 3.0f;
54
Winson Chungb745afb2015-03-02 11:51:23 -080055 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080056 private final Drawable mBackground;
57 private final CheckLongPressHelper mLongPressHelper;
58 private final HolographicOutlineHelper mOutlineHelper;
59
60 // TODO: Remove custom background handling code, as no instance of BubbleTextView use any
61 // background.
62 private boolean mBackgroundSizeChanged;
63
Sunny Goyal508da152014-08-14 10:53:27 -070064 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080065
Jason Monk02dd7ae2014-04-15 15:23:31 -040066 private float mSlop;
67
Winson Chung93f98ea2015-03-10 16:28:47 -070068 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070069 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080070 private final boolean mLayoutHorizontal;
71 private final int mIconSize;
72 private final int mIconPaddingSize;
73 private final int mTextSize;
74 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070075
Michael Jurkaddd62e92011-02-16 17:49:14 -080076 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070077 private boolean mIgnorePressedStateChange;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050078
Sunny Goyal34b65272015-03-11 16:56:52 -070079 private IconLoadRequest mIconLoadRequest;
80
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070082 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 }
84
85 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070086 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 }
88
89 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
90 super(context, attrs, defStyle);
Winson Chungb745afb2015-03-02 11:51:23 -080091 LauncherAppState app = LauncherAppState.getInstance();
92 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070093
Adam Cohen96bb7982014-07-07 11:58:56 -070094 TypedArray a = context.obtainStyledAttributes(attrs,
95 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -070096 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -080097 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
98 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
99 grid.allAppsIconSizePx);
100 mIconPaddingSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconPaddingOverride,
101 grid.iconDrawablePaddingPx);
102 mTextSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_textSizeOverride,
103 grid.allAppsIconTextSizePx);
Winson Chung93f98ea2015-03-10 16:28:47 -0700104 mDeferShadowGenerationOnTouch =
105 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Adam Cohen96bb7982014-07-07 11:58:56 -0700106 a.recycle();
107
Sunny Goyal15872da2014-07-08 15:43:54 -0700108 if (mCustomShadowsEnabled) {
109 // Draw the background itself as the parent is drawn twice.
110 mBackground = getBackground();
111 setBackground(null);
112 } else {
113 mBackground = null;
114 }
Winson Chungb745afb2015-03-02 11:51:23 -0800115
116 // If we are laying out horizontal, then center the text vertically
117 if (mLayoutHorizontal) {
118 setGravity(Gravity.CENTER_VERTICAL);
119 }
120
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800121 mLongPressHelper = new CheckLongPressHelper(this);
122
123 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
124 if (mCustomShadowsEnabled) {
125 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
126 }
127
128 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 }
130
Winson Chung5f8afe62013-08-12 16:19:28 -0700131 public void onFinishInflate() {
132 super.onFinishInflate();
133
134 // Ensure we are using the right text size
Winson Chungb745afb2015-03-02 11:51:23 -0800135 setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
Winson Chung5f8afe62013-08-12 16:19:28 -0700136 }
137
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700138 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
139 boolean setDefaultPadding) {
Sunny Goyal34942622014-08-29 17:20:55 -0700140 applyFromShortcutInfo(info, iconCache, setDefaultPadding, false);
141 }
142
143 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
144 boolean setDefaultPadding, boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800145 Bitmap b = info.getIcon(iconCache);
146
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700147 FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700148 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700149
Winson Chungb745afb2015-03-02 11:51:23 -0800150 setIcon(iconDrawable, mIconSize, setDefaultPadding ? mIconPaddingSize : -1);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100151 if (info.contentDescription != null) {
152 setContentDescription(info.contentDescription);
153 }
Sunny Goyal508da152014-08-14 10:53:27 -0700154 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800155 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700156
Sunny Goyal34942622014-08-29 17:20:55 -0700157 if (promiseStateChanged || info.isPromise()) {
158 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500159 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800160 }
161
Sunny Goyal508da152014-08-14 10:53:27 -0700162 public void applyFromApplicationInfo(AppInfo info) {
Winson Chungb745afb2015-03-02 11:51:23 -0800163 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize, mIconPaddingSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700164 setText(info.title);
165 if (info.contentDescription != null) {
166 setContentDescription(info.contentDescription);
167 }
Winson Chung888b3a12015-03-13 11:14:16 -0700168 // We don't need to check the info since it's not a ShortcutInfo
169 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700170
171 // Verify high res immediately
172 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700173 }
174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 @Override
176 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700177 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 mBackgroundSizeChanged = true;
179 }
180 return super.setFrame(left, top, right, bottom);
181 }
182
183 @Override
184 protected boolean verifyDrawable(Drawable who) {
185 return who == mBackground || super.verifyDrawable(who);
186 }
187
188 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700189 public void setTag(Object tag) {
190 if (tag != null) {
191 LauncherModel.checkItemInfo((ItemInfo) tag);
192 }
193 super.setTag(tag);
194 }
195
196 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700197 public void setPressed(boolean pressed) {
198 super.setPressed(pressed);
199
200 if (!mIgnorePressedStateChange) {
201 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800202 }
Sunny Goyal508da152014-08-14 10:53:27 -0700203 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800204
Winson Chungb745afb2015-03-02 11:51:23 -0800205 /** Returns the icon for this view. */
206 public Drawable getIcon() {
207 return mIcon;
208 }
209
210 /** Returns whether the layout is horizontal. */
211 public boolean isLayoutHorizontal() {
212 return mLayoutHorizontal;
213 }
214
Sunny Goyal508da152014-08-14 10:53:27 -0700215 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800216 if (mIcon instanceof FastBitmapDrawable) {
217 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800219 }
220
221 @Override
222 public boolean onTouchEvent(MotionEvent event) {
223 // Call the superclass onTouchEvent first, because sometimes it changes the state to
224 // isPressed() on an ACTION_UP
225 boolean result = super.onTouchEvent(event);
226
227 switch (event.getAction()) {
228 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700229 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800230 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
231 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700232 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700233 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800234 }
Winson Chung88f33452012-02-23 15:23:44 -0800235
236 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800237 break;
238 case MotionEvent.ACTION_CANCEL:
239 case MotionEvent.ACTION_UP:
240 // If we've touched down and up on an item, and it's still not "pressed", then
241 // destroy the pressed outline
242 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700243 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800244 }
Winson Chung88f33452012-02-23 15:23:44 -0800245
246 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400248 case MotionEvent.ACTION_MOVE:
249 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
250 mLongPressHelper.cancelLongPress();
251 }
252 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800253 }
254 return result;
255 }
256
Michael Jurkaddd62e92011-02-16 17:49:14 -0800257 void setStayPressed(boolean stayPressed) {
258 mStayPressed = stayPressed;
259 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700260 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700261 } else {
262 if (mPressedBackground == null) {
263 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
264 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800265 }
Sunny Goyal508da152014-08-14 10:53:27 -0700266
267 // Only show the shadow effect when persistent pressed state is set.
268 if (getParent() instanceof ShortcutAndWidgetContainer) {
269 CellLayout layout = (CellLayout) getParent().getParent();
270 layout.setPressedIcon(this, mPressedBackground, mOutlineHelper.shadowBitmapPadding);
271 }
272
273 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800274 }
275
Sunny Goyal508da152014-08-14 10:53:27 -0700276 void clearPressedBackground() {
277 setPressed(false);
278 setStayPressed(false);
279 }
280
281 @Override
282 public boolean onKeyDown(int keyCode, KeyEvent event) {
283 if (super.onKeyDown(keyCode, event)) {
284 // Pre-create shadow so show immediately on click.
285 if (mPressedBackground == null) {
286 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700287 }
Sunny Goyal508da152014-08-14 10:53:27 -0700288 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700289 }
Sunny Goyal508da152014-08-14 10:53:27 -0700290 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800291 }
292
Sunny Goyal508da152014-08-14 10:53:27 -0700293 @Override
294 public boolean onKeyUp(int keyCode, KeyEvent event) {
295 // Unlike touch events, keypress event propagate pressed state change immediately,
296 // without waiting for onClickHandler to execute. Disable pressed state changes here
297 // to avoid flickering.
298 mIgnorePressedStateChange = true;
299 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700300
Sunny Goyal508da152014-08-14 10:53:27 -0700301 mPressedBackground = null;
302 mIgnorePressedStateChange = false;
303 updateIconState();
304 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800305 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 @Override
308 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700309 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700310 super.draw(canvas);
311 return;
312 }
313
Michael Jurkabdb5c532011-02-01 15:05:06 -0800314 final Drawable background = mBackground;
315 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700316 final int scrollX = getScrollX();
317 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800318
Michael Jurkabdb5c532011-02-01 15:05:06 -0800319 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700320 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800321 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800323
324 if ((scrollX | scrollY) == 0) {
325 background.draw(canvas);
326 } else {
327 canvas.translate(scrollX, scrollY);
328 background.draw(canvas);
329 canvas.translate(-scrollX, -scrollY);
330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800332
333 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800334 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800335 getPaint().clearShadowLayer();
336 super.draw(canvas);
337 return;
338 }
339
Michael Jurkabdb5c532011-02-01 15:05:06 -0800340 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800341 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800342 super.draw(canvas);
343 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700344 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
345 getScrollX() + getWidth(),
346 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800347 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800348 super.draw(canvas);
349 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400351
352 @Override
353 protected void onAttachedToWindow() {
354 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700355
Winson Chung656d11c2010-11-29 17:15:47 -0800356 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700357
Winson Chungb745afb2015-03-02 11:51:23 -0800358 if (mIcon instanceof PreloadIconDrawable) {
359 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700360 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400361 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400362 }
363
364 @Override
365 protected void onDetachedFromWindow() {
366 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800367 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700369
Adam Cohen477828c2013-09-20 12:05:49 -0700370 @Override
371 public void setTextColor(int color) {
372 mTextColor = color;
373 super.setTextColor(color);
374 }
375
Adam Cohen96bb7982014-07-07 11:58:56 -0700376 @Override
377 public void setTextColor(ColorStateList colors) {
378 mTextColor = colors.getDefaultColor();
379 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700380 }
381
Winson Chung5f8afe62013-08-12 16:19:28 -0700382 public void setTextVisibility(boolean visible) {
383 Resources res = getResources();
384 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700385 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700386 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700387 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700388 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700389 }
390
Winson Chungaffd7b42010-08-20 15:11:56 -0700391 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800392 public void cancelLongPress() {
393 super.cancelLongPress();
394
395 mLongPressHelper.cancelLongPress();
396 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500397
Sunny Goyal34942622014-08-29 17:20:55 -0700398 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700399 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400400 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700401 final boolean isPromise = info.isPromise();
402 final int progressLevel = isPromise ?
403 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
404 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700405
Winson Chungb745afb2015-03-02 11:51:23 -0800406 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700407 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800408 if (mIcon instanceof PreloadIconDrawable) {
409 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700410 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800411 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
412 setIcon(preloadDrawable, mIconSize, -1);
Sunny Goyale755d462014-07-22 13:48:29 -0700413 }
414
415 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700416 if (promiseStateChanged) {
417 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700418 }
Sunny Goyale755d462014-07-22 13:48:29 -0700419 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400420 }
421 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700422
423 private Theme getPreloaderTheme() {
424 Object tag = getTag();
425 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
426 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
427 : R.style.PreloadIcon;
428 Theme theme = sPreloaderThemes.get(style);
429 if (theme == null) {
430 theme = getResources().newTheme();
431 theme.applyStyle(style, true);
432 sPreloaderThemes.put(style, theme);
433 }
434 return theme;
435 }
Winson Chungb745afb2015-03-02 11:51:23 -0800436
437 /**
438 * Sets the icon for this view based on the layout direction.
439 */
440 private Drawable setIcon(Drawable icon, int iconSize, int drawablePadding) {
441 mIcon = icon;
442 if (iconSize != -1) {
443 mIcon.setBounds(0, 0, iconSize, iconSize);
444 }
445 if (mLayoutHorizontal) {
446 setCompoundDrawablesRelative(mIcon, null, null, null);
447 } else {
448 setCompoundDrawablesRelative(null, mIcon, null, null);
449 }
450 if (drawablePadding != -1) {
451 setCompoundDrawablePadding(drawablePadding);
452 }
453 return icon;
454 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700455
456 /**
457 * Applies the item info if it is same as what the view is pointing to currently.
458 */
459 public void reapplyItemInfo(final ItemInfo info) {
460 if (getTag() == info) {
461 mIconLoadRequest = null;
462 if (info instanceof AppInfo) {
463 applyFromApplicationInfo((AppInfo) info);
464 } else if (info instanceof ShortcutInfo) {
465 applyFromShortcutInfo((ShortcutInfo) info,
466 LauncherAppState.getInstance().getIconCache(), false);
467 }
468 }
469 }
470
471 /**
472 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
473 */
474 public void verifyHighRes() {
475 if (mIconLoadRequest != null) {
476 mIconLoadRequest.cancel();
477 mIconLoadRequest = null;
478 }
479 if (getTag() instanceof AppInfo) {
480 AppInfo info = (AppInfo) getTag();
481 if (info.usingLowResIcon) {
482 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
483 .updateIconInBackground(BubbleTextView.this, info);
484 }
485 } else if (getTag() instanceof ShortcutInfo) {
486 ShortcutInfo info = (ShortcutInfo) getTag();
487 if (info.usingLowResIcon) {
488 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
489 .updateIconInBackground(BubbleTextView.this, info);
490 }
491 }
492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493}