blob: 46caf4ab99818815531e07ccfc7e7bd0f226bac2 [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
Sunny Goyaldfaccf62015-05-11 16:30:44 -070055 private static final int DISPLAY_WORKSPACE = 0;
56 private static final int DISPLAY_ALL_APPS = 1;
57
Winson Chungb745afb2015-03-02 11:51:23 -080058 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080059 private final Drawable mBackground;
60 private final CheckLongPressHelper mLongPressHelper;
61 private final HolographicOutlineHelper mOutlineHelper;
62
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080063 private boolean mBackgroundSizeChanged;
64
Sunny Goyal508da152014-08-14 10:53:27 -070065 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080066
Jason Monk02dd7ae2014-04-15 15:23:31 -040067 private float mSlop;
68
Winson Chung93f98ea2015-03-10 16:28:47 -070069 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070070 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080071 private final boolean mLayoutHorizontal;
72 private final int mIconSize;
Winson Chungb745afb2015-03-02 11:51:23 -080073 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070074
Michael Jurkaddd62e92011-02-16 17:49:14 -080075 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070076 private boolean mIgnorePressedStateChange;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050077
Sunny Goyal34b65272015-03-11 16:56:52 -070078 private IconLoadRequest mIconLoadRequest;
79
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);
Winson Chungb745afb2015-03-02 11:51:23 -080090 LauncherAppState app = LauncherAppState.getInstance();
91 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070092
Adam Cohen96bb7982014-07-07 11:58:56 -070093 TypedArray a = context.obtainStyledAttributes(attrs,
94 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -070095 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -080096 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -070097 mDeferShadowGenerationOnTouch =
98 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -070099
100 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
101 int defaultIconSize = grid.iconSizePx;
102 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700103 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700104 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700105 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700106 defaultIconSize = grid.allAppsIconSizePx;
107 }
108
109 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
110 defaultIconSize);
111
Adam Cohen96bb7982014-07-07 11:58:56 -0700112 a.recycle();
113
Sunny Goyal15872da2014-07-08 15:43:54 -0700114 if (mCustomShadowsEnabled) {
115 // Draw the background itself as the parent is drawn twice.
116 mBackground = getBackground();
117 setBackground(null);
118 } else {
119 mBackground = null;
120 }
Winson Chungb745afb2015-03-02 11:51:23 -0800121
122 // If we are laying out horizontal, then center the text vertically
123 if (mLayoutHorizontal) {
124 setGravity(Gravity.CENTER_VERTICAL);
125 }
126
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800127 mLongPressHelper = new CheckLongPressHelper(this);
128
129 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
130 if (mCustomShadowsEnabled) {
131 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
132 }
133
134 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 }
136
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700137 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
138 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700139 }
140
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700141 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700142 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800143 Bitmap b = info.getIcon(iconCache);
144
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700145 FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700146 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700147
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700148 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100149 if (info.contentDescription != null) {
150 setContentDescription(info.contentDescription);
151 }
Sunny Goyal508da152014-08-14 10:53:27 -0700152 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800153 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700154
Sunny Goyal34942622014-08-29 17:20:55 -0700155 if (promiseStateChanged || info.isPromise()) {
156 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500157 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800158 }
159
Sunny Goyal508da152014-08-14 10:53:27 -0700160 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700161 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700162 setText(info.title);
163 if (info.contentDescription != null) {
164 setContentDescription(info.contentDescription);
165 }
Winson Chung888b3a12015-03-13 11:14:16 -0700166 // We don't need to check the info since it's not a ShortcutInfo
167 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700168
169 // Verify high res immediately
170 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700171 }
172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 @Override
174 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700175 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 mBackgroundSizeChanged = true;
177 }
178 return super.setFrame(left, top, right, bottom);
179 }
180
181 @Override
182 protected boolean verifyDrawable(Drawable who) {
183 return who == mBackground || super.verifyDrawable(who);
184 }
185
186 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700187 public void setTag(Object tag) {
188 if (tag != null) {
189 LauncherModel.checkItemInfo((ItemInfo) tag);
190 }
191 super.setTag(tag);
192 }
193
194 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700195 public void setPressed(boolean pressed) {
196 super.setPressed(pressed);
197
198 if (!mIgnorePressedStateChange) {
199 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800200 }
Sunny Goyal508da152014-08-14 10:53:27 -0700201 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800202
Winson Chungb745afb2015-03-02 11:51:23 -0800203 /** Returns the icon for this view. */
204 public Drawable getIcon() {
205 return mIcon;
206 }
207
208 /** Returns whether the layout is horizontal. */
209 public boolean isLayoutHorizontal() {
210 return mLayoutHorizontal;
211 }
212
Sunny Goyal508da152014-08-14 10:53:27 -0700213 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800214 if (mIcon instanceof FastBitmapDrawable) {
215 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800217 }
218
219 @Override
220 public boolean onTouchEvent(MotionEvent event) {
221 // Call the superclass onTouchEvent first, because sometimes it changes the state to
222 // isPressed() on an ACTION_UP
223 boolean result = super.onTouchEvent(event);
224
225 switch (event.getAction()) {
226 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700227 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800228 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
229 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700230 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700231 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800232 }
Winson Chung88f33452012-02-23 15:23:44 -0800233
234 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800235 break;
236 case MotionEvent.ACTION_CANCEL:
237 case MotionEvent.ACTION_UP:
238 // If we've touched down and up on an item, and it's still not "pressed", then
239 // destroy the pressed outline
240 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700241 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800242 }
Winson Chung88f33452012-02-23 15:23:44 -0800243
244 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800245 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400246 case MotionEvent.ACTION_MOVE:
247 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
248 mLongPressHelper.cancelLongPress();
249 }
250 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800251 }
252 return result;
253 }
254
Michael Jurkaddd62e92011-02-16 17:49:14 -0800255 void setStayPressed(boolean stayPressed) {
256 mStayPressed = stayPressed;
257 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700258 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700259 } else {
260 if (mPressedBackground == null) {
261 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
262 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800263 }
Sunny Goyal508da152014-08-14 10:53:27 -0700264
265 // Only show the shadow effect when persistent pressed state is set.
266 if (getParent() instanceof ShortcutAndWidgetContainer) {
267 CellLayout layout = (CellLayout) getParent().getParent();
268 layout.setPressedIcon(this, mPressedBackground, mOutlineHelper.shadowBitmapPadding);
269 }
270
271 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800272 }
273
Sunny Goyal508da152014-08-14 10:53:27 -0700274 void clearPressedBackground() {
275 setPressed(false);
276 setStayPressed(false);
277 }
278
279 @Override
280 public boolean onKeyDown(int keyCode, KeyEvent event) {
281 if (super.onKeyDown(keyCode, event)) {
282 // Pre-create shadow so show immediately on click.
283 if (mPressedBackground == null) {
284 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700285 }
Sunny Goyal508da152014-08-14 10:53:27 -0700286 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700287 }
Sunny Goyal508da152014-08-14 10:53:27 -0700288 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800289 }
290
Sunny Goyal508da152014-08-14 10:53:27 -0700291 @Override
292 public boolean onKeyUp(int keyCode, KeyEvent event) {
293 // Unlike touch events, keypress event propagate pressed state change immediately,
294 // without waiting for onClickHandler to execute. Disable pressed state changes here
295 // to avoid flickering.
296 mIgnorePressedStateChange = true;
297 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700298
Sunny Goyal508da152014-08-14 10:53:27 -0700299 mPressedBackground = null;
300 mIgnorePressedStateChange = false;
301 updateIconState();
302 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800303 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 @Override
306 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700307 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700308 super.draw(canvas);
309 return;
310 }
311
Michael Jurkabdb5c532011-02-01 15:05:06 -0800312 final Drawable background = mBackground;
313 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700314 final int scrollX = getScrollX();
315 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800316
Michael Jurkabdb5c532011-02-01 15:05:06 -0800317 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700318 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800319 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800321
322 if ((scrollX | scrollY) == 0) {
323 background.draw(canvas);
324 } else {
325 canvas.translate(scrollX, scrollY);
326 background.draw(canvas);
327 canvas.translate(-scrollX, -scrollY);
328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800330
331 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800332 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800333 getPaint().clearShadowLayer();
334 super.draw(canvas);
335 return;
336 }
337
Michael Jurkabdb5c532011-02-01 15:05:06 -0800338 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800339 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800340 super.draw(canvas);
341 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700342 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
343 getScrollX() + getWidth(),
344 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800345 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800346 super.draw(canvas);
347 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400349
350 @Override
351 protected void onAttachedToWindow() {
352 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700353
Winson Chung656d11c2010-11-29 17:15:47 -0800354 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700355
Winson Chungb745afb2015-03-02 11:51:23 -0800356 if (mIcon instanceof PreloadIconDrawable) {
357 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700358 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400359 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400360 }
361
362 @Override
363 protected void onDetachedFromWindow() {
364 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800365 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700367
Adam Cohen477828c2013-09-20 12:05:49 -0700368 @Override
369 public void setTextColor(int color) {
370 mTextColor = color;
371 super.setTextColor(color);
372 }
373
Adam Cohen96bb7982014-07-07 11:58:56 -0700374 @Override
375 public void setTextColor(ColorStateList colors) {
376 mTextColor = colors.getDefaultColor();
377 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700378 }
379
Winson Chung5f8afe62013-08-12 16:19:28 -0700380 public void setTextVisibility(boolean visible) {
381 Resources res = getResources();
382 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700383 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700384 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700385 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700386 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700387 }
388
Winson Chungaffd7b42010-08-20 15:11:56 -0700389 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800390 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());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700410 setIcon(preloadDrawable, mIconSize);
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 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700438 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800439 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 }
Winson Chungb745afb2015-03-02 11:51:23 -0800448 return icon;
449 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700450
451 /**
452 * Applies the item info if it is same as what the view is pointing to currently.
453 */
454 public void reapplyItemInfo(final ItemInfo info) {
455 if (getTag() == info) {
456 mIconLoadRequest = null;
457 if (info instanceof AppInfo) {
458 applyFromApplicationInfo((AppInfo) info);
459 } else if (info instanceof ShortcutInfo) {
460 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700461 LauncherAppState.getInstance().getIconCache());
Sunny Goyal34b65272015-03-11 16:56:52 -0700462 }
463 }
464 }
465
466 /**
467 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
468 */
469 public void verifyHighRes() {
470 if (mIconLoadRequest != null) {
471 mIconLoadRequest.cancel();
472 mIconLoadRequest = null;
473 }
474 if (getTag() instanceof AppInfo) {
475 AppInfo info = (AppInfo) getTag();
476 if (info.usingLowResIcon) {
477 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
478 .updateIconInBackground(BubbleTextView.this, info);
479 }
480 } else if (getTag() instanceof ShortcutInfo) {
481 ShortcutInfo info = (ShortcutInfo) getTag();
482 if (info.usingLowResIcon) {
483 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
484 .updateIconInBackground(BubbleTextView.this, info);
485 }
486 }
487 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488}