blob: d39e139761c03396334b7d78c9cc659a97bf274c [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
Sunny Goyal70660032015-05-14 00:07:08 -070019import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070021import android.content.res.ColorStateList;
Winson Chung656d11c2010-11-29 17:15:47 -080022import android.content.res.Resources;
Sunny Goyal95abbb32014-08-04 10:53:22 -070023import android.content.res.Resources.Theme;
Adam Cohen96bb7982014-07-07 11:58:56 -070024import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080025import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.Canvas;
Michael Jurka137142e2011-01-05 20:57:04 -080027import android.graphics.Region;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.graphics.drawable.Drawable;
Sunny Goyal70660032015-05-14 00:07:08 -070029import android.os.Build;
Winson Chung656d11c2010-11-29 17:15:47 -080030import android.util.AttributeSet;
Sunny Goyal69b75642015-05-15 17:00:24 -070031import android.util.Log;
Sunny Goyal95abbb32014-08-04 10:53:22 -070032import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070033import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070034import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080035import android.view.MotionEvent;
Jason Monk02dd7ae2014-04-15 15:23:31 -040036import android.view.ViewConfiguration;
Michael Jurkabdb5c532011-02-01 15:05:06 -080037import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080038
Sunny Goyal34b65272015-03-11 16:56:52 -070039import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal0e08f162015-05-12 11:32:39 -070040import com.android.launcher3.widget.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070041
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042/**
43 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
44 * because we want to make the bubble taller than the text and TextView's clip is
45 * too aggressive.
46 */
Michael Jurka08ee7702011-08-11 16:53:35 -070047public class BubbleTextView extends TextView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070048
Sameer Padala8fd74832014-09-08 16:00:29 -070049 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070050
Sunny Goyal508da152014-08-14 10:53:27 -070051 private static final float SHADOW_LARGE_RADIUS = 4.0f;
52 private static final float SHADOW_SMALL_RADIUS = 1.75f;
53 private static final float SHADOW_Y_OFFSET = 2.0f;
54 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
55 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080056 static final float PADDING_V = 3.0f;
57
Sunny Goyaldfaccf62015-05-11 16:30:44 -070058 private static final int DISPLAY_WORKSPACE = 0;
59 private static final int DISPLAY_ALL_APPS = 1;
60
Winson Chungb745afb2015-03-02 11:51:23 -080061 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080062 private final Drawable mBackground;
63 private final CheckLongPressHelper mLongPressHelper;
64 private final HolographicOutlineHelper mOutlineHelper;
65
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080066 private boolean mBackgroundSizeChanged;
67
Sunny Goyal508da152014-08-14 10:53:27 -070068 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080069
Jason Monk02dd7ae2014-04-15 15:23:31 -040070 private float mSlop;
71
Winson Chung93f98ea2015-03-10 16:28:47 -070072 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070073 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080074 private final boolean mLayoutHorizontal;
75 private final int mIconSize;
Winson Chungb745afb2015-03-02 11:51:23 -080076 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070077
Michael Jurkaddd62e92011-02-16 17:49:14 -080078 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070079 private boolean mIgnorePressedStateChange;
Sunny Goyal69b75642015-05-15 17:00:24 -070080 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050081
Sunny Goyal34b65272015-03-11 16:56:52 -070082 private IconLoadRequest mIconLoadRequest;
83
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070085 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 }
87
88 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070089 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 }
91
92 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
93 super(context, attrs, defStyle);
Winson Chungb745afb2015-03-02 11:51:23 -080094 LauncherAppState app = LauncherAppState.getInstance();
95 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070096
Adam Cohen96bb7982014-07-07 11:58:56 -070097 TypedArray a = context.obtainStyledAttributes(attrs,
98 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -070099 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800100 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700101 mDeferShadowGenerationOnTouch =
102 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700103
104 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
105 int defaultIconSize = grid.iconSizePx;
106 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700107 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700108 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700109 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700110 defaultIconSize = grid.allAppsIconSizePx;
111 }
112
113 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
114 defaultIconSize);
115
Adam Cohen96bb7982014-07-07 11:58:56 -0700116 a.recycle();
117
Sunny Goyal15872da2014-07-08 15:43:54 -0700118 if (mCustomShadowsEnabled) {
119 // Draw the background itself as the parent is drawn twice.
120 mBackground = getBackground();
121 setBackground(null);
122 } else {
123 mBackground = null;
124 }
Winson Chungb745afb2015-03-02 11:51:23 -0800125
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800126 mLongPressHelper = new CheckLongPressHelper(this);
127
128 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
129 if (mCustomShadowsEnabled) {
130 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
131 }
132
133 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 }
135
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700136 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
137 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700138 }
139
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700140 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700141 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800142 Bitmap b = info.getIcon(iconCache);
143
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700144 FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700145 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700146
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700147 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100148 if (info.contentDescription != null) {
149 setContentDescription(info.contentDescription);
150 }
Sunny Goyal508da152014-08-14 10:53:27 -0700151 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800152 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700153
Sunny Goyal34942622014-08-29 17:20:55 -0700154 if (promiseStateChanged || info.isPromise()) {
155 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500156 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800157 }
158
Sunny Goyal508da152014-08-14 10:53:27 -0700159 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700160 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700161 setText(info.title);
162 if (info.contentDescription != null) {
163 setContentDescription(info.contentDescription);
164 }
Winson Chung888b3a12015-03-13 11:14:16 -0700165 // We don't need to check the info since it's not a ShortcutInfo
166 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700167
168 // Verify high res immediately
169 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700170 }
171
Sunny Goyal0e08f162015-05-12 11:32:39 -0700172 public void applyFromPackageItemInfo(PackageItemInfo info) {
173 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
174 setText(info.title);
175 if (info.contentDescription != null) {
176 setContentDescription(info.contentDescription);
177 }
178 // We don't need to check the info since it's not a ShortcutInfo
179 super.setTag(info);
180
181 // Verify high res immediately
182 verifyHighRes();
183 }
184
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 @Override
187 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700188 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 mBackgroundSizeChanged = true;
190 }
191 return super.setFrame(left, top, right, bottom);
192 }
193
194 @Override
195 protected boolean verifyDrawable(Drawable who) {
196 return who == mBackground || super.verifyDrawable(who);
197 }
198
199 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700200 public void setTag(Object tag) {
201 if (tag != null) {
202 LauncherModel.checkItemInfo((ItemInfo) tag);
203 }
204 super.setTag(tag);
205 }
206
207 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700208 public void setPressed(boolean pressed) {
209 super.setPressed(pressed);
210
211 if (!mIgnorePressedStateChange) {
212 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800213 }
Sunny Goyal508da152014-08-14 10:53:27 -0700214 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800215
Winson Chungb745afb2015-03-02 11:51:23 -0800216 /** Returns the icon for this view. */
217 public Drawable getIcon() {
218 return mIcon;
219 }
220
221 /** Returns whether the layout is horizontal. */
222 public boolean isLayoutHorizontal() {
223 return mLayoutHorizontal;
224 }
225
Sunny Goyal508da152014-08-14 10:53:27 -0700226 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800227 if (mIcon instanceof FastBitmapDrawable) {
228 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800230 }
231
232 @Override
233 public boolean onTouchEvent(MotionEvent event) {
234 // Call the superclass onTouchEvent first, because sometimes it changes the state to
235 // isPressed() on an ACTION_UP
236 boolean result = super.onTouchEvent(event);
237
238 switch (event.getAction()) {
239 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700240 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800241 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
242 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700243 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700244 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800245 }
Winson Chung88f33452012-02-23 15:23:44 -0800246
247 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800248 break;
249 case MotionEvent.ACTION_CANCEL:
250 case MotionEvent.ACTION_UP:
251 // If we've touched down and up on an item, and it's still not "pressed", then
252 // destroy the pressed outline
253 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700254 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800255 }
Winson Chung88f33452012-02-23 15:23:44 -0800256
257 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800258 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400259 case MotionEvent.ACTION_MOVE:
260 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
261 mLongPressHelper.cancelLongPress();
262 }
263 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800264 }
265 return result;
266 }
267
Michael Jurkaddd62e92011-02-16 17:49:14 -0800268 void setStayPressed(boolean stayPressed) {
269 mStayPressed = stayPressed;
270 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700271 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700272 } else {
273 if (mPressedBackground == null) {
274 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
275 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800276 }
Sunny Goyal508da152014-08-14 10:53:27 -0700277
278 // Only show the shadow effect when persistent pressed state is set.
279 if (getParent() instanceof ShortcutAndWidgetContainer) {
280 CellLayout layout = (CellLayout) getParent().getParent();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700281 layout.setPressedIcon(this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700282 }
283
284 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800285 }
286
Sunny Goyal508da152014-08-14 10:53:27 -0700287 void clearPressedBackground() {
288 setPressed(false);
289 setStayPressed(false);
290 }
291
292 @Override
293 public boolean onKeyDown(int keyCode, KeyEvent event) {
294 if (super.onKeyDown(keyCode, event)) {
295 // Pre-create shadow so show immediately on click.
296 if (mPressedBackground == null) {
297 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700298 }
Sunny Goyal508da152014-08-14 10:53:27 -0700299 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700300 }
Sunny Goyal508da152014-08-14 10:53:27 -0700301 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800302 }
303
Sunny Goyal508da152014-08-14 10:53:27 -0700304 @Override
305 public boolean onKeyUp(int keyCode, KeyEvent event) {
306 // Unlike touch events, keypress event propagate pressed state change immediately,
307 // without waiting for onClickHandler to execute. Disable pressed state changes here
308 // to avoid flickering.
309 mIgnorePressedStateChange = true;
310 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700311
Sunny Goyal508da152014-08-14 10:53:27 -0700312 mPressedBackground = null;
313 mIgnorePressedStateChange = false;
314 updateIconState();
315 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800316 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800317
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 @Override
319 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700320 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700321 super.draw(canvas);
322 return;
323 }
324
Michael Jurkabdb5c532011-02-01 15:05:06 -0800325 final Drawable background = mBackground;
326 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700327 final int scrollX = getScrollX();
328 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800329
Michael Jurkabdb5c532011-02-01 15:05:06 -0800330 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700331 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800332 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800334
335 if ((scrollX | scrollY) == 0) {
336 background.draw(canvas);
337 } else {
338 canvas.translate(scrollX, scrollY);
339 background.draw(canvas);
340 canvas.translate(-scrollX, -scrollY);
341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800343
344 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800345 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800346 getPaint().clearShadowLayer();
347 super.draw(canvas);
348 return;
349 }
350
Michael Jurkabdb5c532011-02-01 15:05:06 -0800351 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800352 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800353 super.draw(canvas);
354 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700355 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
356 getScrollX() + getWidth(),
357 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800358 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800359 super.draw(canvas);
360 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400362
363 @Override
364 protected void onAttachedToWindow() {
365 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700366
Winson Chung656d11c2010-11-29 17:15:47 -0800367 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700368
Winson Chungb745afb2015-03-02 11:51:23 -0800369 if (mIcon instanceof PreloadIconDrawable) {
370 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700371 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400372 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 }
374
375 @Override
376 protected void onDetachedFromWindow() {
377 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800378 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700380
Adam Cohen477828c2013-09-20 12:05:49 -0700381 @Override
382 public void setTextColor(int color) {
383 mTextColor = color;
384 super.setTextColor(color);
385 }
386
Adam Cohen96bb7982014-07-07 11:58:56 -0700387 @Override
388 public void setTextColor(ColorStateList colors) {
389 mTextColor = colors.getDefaultColor();
390 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700391 }
392
Winson Chung5f8afe62013-08-12 16:19:28 -0700393 public void setTextVisibility(boolean visible) {
394 Resources res = getResources();
395 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700396 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700397 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700398 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700399 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700400 }
401
Winson Chungaffd7b42010-08-20 15:11:56 -0700402 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800403 public void cancelLongPress() {
404 super.cancelLongPress();
405
406 mLongPressHelper.cancelLongPress();
407 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500408
Sunny Goyal34942622014-08-29 17:20:55 -0700409 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700410 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400411 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700412 final boolean isPromise = info.isPromise();
413 final int progressLevel = isPromise ?
414 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
415 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700416
Winson Chungb745afb2015-03-02 11:51:23 -0800417 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700418 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800419 if (mIcon instanceof PreloadIconDrawable) {
420 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700421 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800422 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700423 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700424 }
425
426 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700427 if (promiseStateChanged) {
428 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700429 }
Sunny Goyale755d462014-07-22 13:48:29 -0700430 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400431 }
432 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700433
434 private Theme getPreloaderTheme() {
435 Object tag = getTag();
436 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
437 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
438 : R.style.PreloadIcon;
439 Theme theme = sPreloaderThemes.get(style);
440 if (theme == null) {
441 theme = getResources().newTheme();
442 theme.applyStyle(style, true);
443 sPreloaderThemes.put(style, theme);
444 }
445 return theme;
446 }
Winson Chungb745afb2015-03-02 11:51:23 -0800447
448 /**
449 * Sets the icon for this view based on the layout direction.
450 */
Sunny Goyal70660032015-05-14 00:07:08 -0700451 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700452 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800453 mIcon = icon;
454 if (iconSize != -1) {
455 mIcon.setBounds(0, 0, iconSize, iconSize);
456 }
457 if (mLayoutHorizontal) {
Sunny Goyal70660032015-05-14 00:07:08 -0700458 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
459 setCompoundDrawablesRelative(mIcon, null, null, null);
460 } else {
461 setCompoundDrawables(mIcon, null, null, null);
462 }
Winson Chungb745afb2015-03-02 11:51:23 -0800463 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700464 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800465 }
Winson Chungb745afb2015-03-02 11:51:23 -0800466 return icon;
467 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700468
Sunny Goyal69b75642015-05-15 17:00:24 -0700469 @Override
470 public void requestLayout() {
471 if (!mDisableRelayout) {
472 super.requestLayout();
473 }
474 }
475
Sunny Goyal34b65272015-03-11 16:56:52 -0700476 /**
477 * Applies the item info if it is same as what the view is pointing to currently.
478 */
479 public void reapplyItemInfo(final ItemInfo info) {
480 if (getTag() == info) {
481 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700482 mDisableRelayout = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700483 if (info instanceof AppInfo) {
484 applyFromApplicationInfo((AppInfo) info);
485 } else if (info instanceof ShortcutInfo) {
486 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700487 LauncherAppState.getInstance().getIconCache());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700488 } else if (info instanceof PackageItemInfo) {
489 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700490 }
Sunny Goyal69b75642015-05-15 17:00:24 -0700491 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700492 }
493 }
494
495 /**
496 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
497 */
498 public void verifyHighRes() {
499 if (mIconLoadRequest != null) {
500 mIconLoadRequest.cancel();
501 mIconLoadRequest = null;
502 }
503 if (getTag() instanceof AppInfo) {
504 AppInfo info = (AppInfo) getTag();
505 if (info.usingLowResIcon) {
506 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
507 .updateIconInBackground(BubbleTextView.this, info);
508 }
509 } else if (getTag() instanceof ShortcutInfo) {
510 ShortcutInfo info = (ShortcutInfo) getTag();
511 if (info.usingLowResIcon) {
512 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
513 .updateIconInBackground(BubbleTextView.this, info);
514 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700515 } else if (getTag() instanceof PackageItemInfo) {
516 PackageItemInfo info = (PackageItemInfo) getTag();
517 if (info.usingLowResIcon) {
518 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
519 .updateIconInBackground(BubbleTextView.this, info);
520 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700521 }
522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800523}