blob: d32c919196dc2c3864b91f2684cb8caf41c5c836 [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;
Sunny Goyal508da152014-08-14 10:53:27 -070031import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080032import android.view.MotionEvent;
Jason Monk02dd7ae2014-04-15 15:23:31 -040033import android.view.ViewConfiguration;
Michael Jurkabdb5c532011-02-01 15:05:06 -080034import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080035
Sunny Goyal34b65272015-03-11 16:56:52 -070036import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal0e08f162015-05-12 11:32:39 -070037import com.android.launcher3.widget.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070038
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
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800122 mLongPressHelper = new CheckLongPressHelper(this);
123
124 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
125 if (mCustomShadowsEnabled) {
126 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
127 }
128
129 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 }
131
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700132 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
133 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700134 }
135
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700136 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700137 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800138 Bitmap b = info.getIcon(iconCache);
139
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700140 FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700141 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700142
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700143 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100144 if (info.contentDescription != null) {
145 setContentDescription(info.contentDescription);
146 }
Sunny Goyal508da152014-08-14 10:53:27 -0700147 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800148 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700149
Sunny Goyal34942622014-08-29 17:20:55 -0700150 if (promiseStateChanged || info.isPromise()) {
151 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500152 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800153 }
154
Sunny Goyal508da152014-08-14 10:53:27 -0700155 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700156 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700157 setText(info.title);
158 if (info.contentDescription != null) {
159 setContentDescription(info.contentDescription);
160 }
Winson Chung888b3a12015-03-13 11:14:16 -0700161 // We don't need to check the info since it's not a ShortcutInfo
162 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700163
164 // Verify high res immediately
165 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700166 }
167
Sunny Goyal0e08f162015-05-12 11:32:39 -0700168 public void applyFromPackageItemInfo(PackageItemInfo info) {
169 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
170 setText(info.title);
171 if (info.contentDescription != null) {
172 setContentDescription(info.contentDescription);
173 }
174 // We don't need to check the info since it's not a ShortcutInfo
175 super.setTag(info);
176
177 // Verify high res immediately
178 verifyHighRes();
179 }
180
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 @Override
183 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700184 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 mBackgroundSizeChanged = true;
186 }
187 return super.setFrame(left, top, right, bottom);
188 }
189
190 @Override
191 protected boolean verifyDrawable(Drawable who) {
192 return who == mBackground || super.verifyDrawable(who);
193 }
194
195 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700196 public void setTag(Object tag) {
197 if (tag != null) {
198 LauncherModel.checkItemInfo((ItemInfo) tag);
199 }
200 super.setTag(tag);
201 }
202
203 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700204 public void setPressed(boolean pressed) {
205 super.setPressed(pressed);
206
207 if (!mIgnorePressedStateChange) {
208 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800209 }
Sunny Goyal508da152014-08-14 10:53:27 -0700210 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800211
Winson Chungb745afb2015-03-02 11:51:23 -0800212 /** Returns the icon for this view. */
213 public Drawable getIcon() {
214 return mIcon;
215 }
216
217 /** Returns whether the layout is horizontal. */
218 public boolean isLayoutHorizontal() {
219 return mLayoutHorizontal;
220 }
221
Sunny Goyal508da152014-08-14 10:53:27 -0700222 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800223 if (mIcon instanceof FastBitmapDrawable) {
224 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800226 }
227
228 @Override
229 public boolean onTouchEvent(MotionEvent event) {
230 // Call the superclass onTouchEvent first, because sometimes it changes the state to
231 // isPressed() on an ACTION_UP
232 boolean result = super.onTouchEvent(event);
233
234 switch (event.getAction()) {
235 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700236 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800237 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
238 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700239 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700240 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800241 }
Winson Chung88f33452012-02-23 15:23:44 -0800242
243 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800244 break;
245 case MotionEvent.ACTION_CANCEL:
246 case MotionEvent.ACTION_UP:
247 // If we've touched down and up on an item, and it's still not "pressed", then
248 // destroy the pressed outline
249 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700250 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800251 }
Winson Chung88f33452012-02-23 15:23:44 -0800252
253 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800254 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400255 case MotionEvent.ACTION_MOVE:
256 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
257 mLongPressHelper.cancelLongPress();
258 }
259 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800260 }
261 return result;
262 }
263
Michael Jurkaddd62e92011-02-16 17:49:14 -0800264 void setStayPressed(boolean stayPressed) {
265 mStayPressed = stayPressed;
266 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700267 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700268 } else {
269 if (mPressedBackground == null) {
270 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
271 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800272 }
Sunny Goyal508da152014-08-14 10:53:27 -0700273
274 // Only show the shadow effect when persistent pressed state is set.
275 if (getParent() instanceof ShortcutAndWidgetContainer) {
276 CellLayout layout = (CellLayout) getParent().getParent();
277 layout.setPressedIcon(this, mPressedBackground, mOutlineHelper.shadowBitmapPadding);
278 }
279
280 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800281 }
282
Sunny Goyal508da152014-08-14 10:53:27 -0700283 void clearPressedBackground() {
284 setPressed(false);
285 setStayPressed(false);
286 }
287
288 @Override
289 public boolean onKeyDown(int keyCode, KeyEvent event) {
290 if (super.onKeyDown(keyCode, event)) {
291 // Pre-create shadow so show immediately on click.
292 if (mPressedBackground == null) {
293 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700294 }
Sunny Goyal508da152014-08-14 10:53:27 -0700295 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700296 }
Sunny Goyal508da152014-08-14 10:53:27 -0700297 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800298 }
299
Sunny Goyal508da152014-08-14 10:53:27 -0700300 @Override
301 public boolean onKeyUp(int keyCode, KeyEvent event) {
302 // Unlike touch events, keypress event propagate pressed state change immediately,
303 // without waiting for onClickHandler to execute. Disable pressed state changes here
304 // to avoid flickering.
305 mIgnorePressedStateChange = true;
306 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700307
Sunny Goyal508da152014-08-14 10:53:27 -0700308 mPressedBackground = null;
309 mIgnorePressedStateChange = false;
310 updateIconState();
311 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800312 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800313
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 @Override
315 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700316 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700317 super.draw(canvas);
318 return;
319 }
320
Michael Jurkabdb5c532011-02-01 15:05:06 -0800321 final Drawable background = mBackground;
322 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700323 final int scrollX = getScrollX();
324 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800325
Michael Jurkabdb5c532011-02-01 15:05:06 -0800326 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700327 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800328 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800330
331 if ((scrollX | scrollY) == 0) {
332 background.draw(canvas);
333 } else {
334 canvas.translate(scrollX, scrollY);
335 background.draw(canvas);
336 canvas.translate(-scrollX, -scrollY);
337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800339
340 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800341 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800342 getPaint().clearShadowLayer();
343 super.draw(canvas);
344 return;
345 }
346
Michael Jurkabdb5c532011-02-01 15:05:06 -0800347 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800348 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800349 super.draw(canvas);
350 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700351 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
352 getScrollX() + getWidth(),
353 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800354 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800355 super.draw(canvas);
356 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400358
359 @Override
360 protected void onAttachedToWindow() {
361 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700362
Winson Chung656d11c2010-11-29 17:15:47 -0800363 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700364
Winson Chungb745afb2015-03-02 11:51:23 -0800365 if (mIcon instanceof PreloadIconDrawable) {
366 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700367 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400368 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400369 }
370
371 @Override
372 protected void onDetachedFromWindow() {
373 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800374 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700376
Adam Cohen477828c2013-09-20 12:05:49 -0700377 @Override
378 public void setTextColor(int color) {
379 mTextColor = color;
380 super.setTextColor(color);
381 }
382
Adam Cohen96bb7982014-07-07 11:58:56 -0700383 @Override
384 public void setTextColor(ColorStateList colors) {
385 mTextColor = colors.getDefaultColor();
386 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700387 }
388
Winson Chung5f8afe62013-08-12 16:19:28 -0700389 public void setTextVisibility(boolean visible) {
390 Resources res = getResources();
391 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700392 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700393 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700394 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700395 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700396 }
397
Winson Chungaffd7b42010-08-20 15:11:56 -0700398 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800399 public void cancelLongPress() {
400 super.cancelLongPress();
401
402 mLongPressHelper.cancelLongPress();
403 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500404
Sunny Goyal34942622014-08-29 17:20:55 -0700405 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700406 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400407 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700408 final boolean isPromise = info.isPromise();
409 final int progressLevel = isPromise ?
410 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
411 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700412
Winson Chungb745afb2015-03-02 11:51:23 -0800413 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700414 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800415 if (mIcon instanceof PreloadIconDrawable) {
416 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700417 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800418 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700419 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700420 }
421
422 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700423 if (promiseStateChanged) {
424 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700425 }
Sunny Goyale755d462014-07-22 13:48:29 -0700426 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400427 }
428 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700429
430 private Theme getPreloaderTheme() {
431 Object tag = getTag();
432 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
433 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
434 : R.style.PreloadIcon;
435 Theme theme = sPreloaderThemes.get(style);
436 if (theme == null) {
437 theme = getResources().newTheme();
438 theme.applyStyle(style, true);
439 sPreloaderThemes.put(style, theme);
440 }
441 return theme;
442 }
Winson Chungb745afb2015-03-02 11:51:23 -0800443
444 /**
445 * Sets the icon for this view based on the layout direction.
446 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700447 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800448 mIcon = icon;
449 if (iconSize != -1) {
450 mIcon.setBounds(0, 0, iconSize, iconSize);
451 }
452 if (mLayoutHorizontal) {
453 setCompoundDrawablesRelative(mIcon, null, null, null);
454 } else {
455 setCompoundDrawablesRelative(null, mIcon, null, null);
456 }
Winson Chungb745afb2015-03-02 11:51:23 -0800457 return icon;
458 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700459
460 /**
461 * Applies the item info if it is same as what the view is pointing to currently.
462 */
463 public void reapplyItemInfo(final ItemInfo info) {
464 if (getTag() == info) {
465 mIconLoadRequest = null;
466 if (info instanceof AppInfo) {
467 applyFromApplicationInfo((AppInfo) info);
468 } else if (info instanceof ShortcutInfo) {
469 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700470 LauncherAppState.getInstance().getIconCache());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700471 } else if (info instanceof PackageItemInfo) {
472 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700473 }
474 }
475 }
476
477 /**
478 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
479 */
480 public void verifyHighRes() {
481 if (mIconLoadRequest != null) {
482 mIconLoadRequest.cancel();
483 mIconLoadRequest = null;
484 }
485 if (getTag() instanceof AppInfo) {
486 AppInfo info = (AppInfo) getTag();
487 if (info.usingLowResIcon) {
488 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
489 .updateIconInBackground(BubbleTextView.this, info);
490 }
491 } else if (getTag() instanceof ShortcutInfo) {
492 ShortcutInfo info = (ShortcutInfo) getTag();
493 if (info.usingLowResIcon) {
494 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
495 .updateIconInBackground(BubbleTextView.this, info);
496 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700497 } else if (getTag() instanceof PackageItemInfo) {
498 PackageItemInfo info = (PackageItemInfo) getTag();
499 if (info.usingLowResIcon) {
500 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
501 .updateIconInBackground(BubbleTextView.this, info);
502 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700503 }
504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505}