blob: d5300095b5f04dd996d9d060bc0e5064b5a45261 [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;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070037import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080038import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080039
Sunny Goyal34b65272015-03-11 16:56:52 -070040import com.android.launcher3.IconCache.IconLoadRequest;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070041import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070042
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043/**
44 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
45 * because we want to make the bubble taller than the text and TextView's clip is
46 * too aggressive.
47 */
Michael Jurka08ee7702011-08-11 16:53:35 -070048public class BubbleTextView extends TextView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070049
Sameer Padala8fd74832014-09-08 16:00:29 -070050 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070051
Sunny Goyal508da152014-08-14 10:53:27 -070052 private static final float SHADOW_LARGE_RADIUS = 4.0f;
53 private static final float SHADOW_SMALL_RADIUS = 1.75f;
54 private static final float SHADOW_Y_OFFSET = 2.0f;
55 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
56 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080057 static final float PADDING_V = 3.0f;
58
Sunny Goyaldfaccf62015-05-11 16:30:44 -070059 private static final int DISPLAY_WORKSPACE = 0;
60 private static final int DISPLAY_ALL_APPS = 1;
61
Winson Chungb745afb2015-03-02 11:51:23 -080062 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080063 private final Drawable mBackground;
64 private final CheckLongPressHelper mLongPressHelper;
65 private final HolographicOutlineHelper mOutlineHelper;
66
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080067 private boolean mBackgroundSizeChanged;
68
Sunny Goyal508da152014-08-14 10:53:27 -070069 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080070
Jason Monk02dd7ae2014-04-15 15:23:31 -040071 private float mSlop;
72
Winson Chung93f98ea2015-03-10 16:28:47 -070073 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070074 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080075 private final boolean mLayoutHorizontal;
76 private final int mIconSize;
Winson Chungb745afb2015-03-02 11:51:23 -080077 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070078
Michael Jurkaddd62e92011-02-16 17:49:14 -080079 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070080 private boolean mIgnorePressedStateChange;
Sunny Goyal69b75642015-05-15 17:00:24 -070081 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050082
Sunny Goyal34b65272015-03-11 16:56:52 -070083 private IconLoadRequest mIconLoadRequest;
84
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070086 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 }
88
89 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070090 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 }
92
93 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
94 super(context, attrs, defStyle);
Winson Chungb745afb2015-03-02 11:51:23 -080095 LauncherAppState app = LauncherAppState.getInstance();
96 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070097
Adam Cohen96bb7982014-07-07 11:58:56 -070098 TypedArray a = context.obtainStyledAttributes(attrs,
99 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700100 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800101 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700102 mDeferShadowGenerationOnTouch =
103 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700104
105 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
106 int defaultIconSize = grid.iconSizePx;
107 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700108 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700109 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700110 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700111 defaultIconSize = grid.allAppsIconSizePx;
112 }
113
114 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
115 defaultIconSize);
116
Adam Cohen96bb7982014-07-07 11:58:56 -0700117 a.recycle();
118
Sunny Goyal15872da2014-07-08 15:43:54 -0700119 if (mCustomShadowsEnabled) {
120 // Draw the background itself as the parent is drawn twice.
121 mBackground = getBackground();
122 setBackground(null);
123 } else {
124 mBackground = null;
125 }
Winson Chungb745afb2015-03-02 11:51:23 -0800126
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
Sunny Goyal0e08f162015-05-12 11:32:39 -0700173 public void applyFromPackageItemInfo(PackageItemInfo info) {
174 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
175 setText(info.title);
176 if (info.contentDescription != null) {
177 setContentDescription(info.contentDescription);
178 }
179 // We don't need to check the info since it's not a ShortcutInfo
180 super.setTag(info);
181
182 // Verify high res immediately
183 verifyHighRes();
184 }
185
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 @Override
188 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700189 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 mBackgroundSizeChanged = true;
191 }
192 return super.setFrame(left, top, right, bottom);
193 }
194
195 @Override
196 protected boolean verifyDrawable(Drawable who) {
197 return who == mBackground || super.verifyDrawable(who);
198 }
199
200 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700201 public void setTag(Object tag) {
202 if (tag != null) {
203 LauncherModel.checkItemInfo((ItemInfo) tag);
204 }
205 super.setTag(tag);
206 }
207
208 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700209 public void setPressed(boolean pressed) {
210 super.setPressed(pressed);
211
212 if (!mIgnorePressedStateChange) {
213 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800214 }
Sunny Goyal508da152014-08-14 10:53:27 -0700215 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800216
Winson Chungb745afb2015-03-02 11:51:23 -0800217 /** Returns the icon for this view. */
218 public Drawable getIcon() {
219 return mIcon;
220 }
221
222 /** Returns whether the layout is horizontal. */
223 public boolean isLayoutHorizontal() {
224 return mLayoutHorizontal;
225 }
226
Sunny Goyal508da152014-08-14 10:53:27 -0700227 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800228 if (mIcon instanceof FastBitmapDrawable) {
229 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800231 }
232
233 @Override
234 public boolean onTouchEvent(MotionEvent event) {
235 // Call the superclass onTouchEvent first, because sometimes it changes the state to
236 // isPressed() on an ACTION_UP
237 boolean result = super.onTouchEvent(event);
238
239 switch (event.getAction()) {
240 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700241 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800242 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
243 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700244 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700245 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800246 }
Winson Chung88f33452012-02-23 15:23:44 -0800247
248 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800249 break;
250 case MotionEvent.ACTION_CANCEL:
251 case MotionEvent.ACTION_UP:
252 // If we've touched down and up on an item, and it's still not "pressed", then
253 // destroy the pressed outline
254 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700255 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800256 }
Winson Chung88f33452012-02-23 15:23:44 -0800257
258 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800259 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400260 case MotionEvent.ACTION_MOVE:
261 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
262 mLongPressHelper.cancelLongPress();
263 }
264 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800265 }
266 return result;
267 }
268
Michael Jurkaddd62e92011-02-16 17:49:14 -0800269 void setStayPressed(boolean stayPressed) {
270 mStayPressed = stayPressed;
271 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700272 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700273 } else {
274 if (mPressedBackground == null) {
275 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
276 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800277 }
Sunny Goyal508da152014-08-14 10:53:27 -0700278
279 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700280 ViewParent parent = getParent();
281 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
282 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
283 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700284 }
285
286 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800287 }
288
Sunny Goyal508da152014-08-14 10:53:27 -0700289 void clearPressedBackground() {
290 setPressed(false);
291 setStayPressed(false);
292 }
293
294 @Override
295 public boolean onKeyDown(int keyCode, KeyEvent event) {
296 if (super.onKeyDown(keyCode, event)) {
297 // Pre-create shadow so show immediately on click.
298 if (mPressedBackground == null) {
299 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700300 }
Sunny Goyal508da152014-08-14 10:53:27 -0700301 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700302 }
Sunny Goyal508da152014-08-14 10:53:27 -0700303 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800304 }
305
Sunny Goyal508da152014-08-14 10:53:27 -0700306 @Override
307 public boolean onKeyUp(int keyCode, KeyEvent event) {
308 // Unlike touch events, keypress event propagate pressed state change immediately,
309 // without waiting for onClickHandler to execute. Disable pressed state changes here
310 // to avoid flickering.
311 mIgnorePressedStateChange = true;
312 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700313
Sunny Goyal508da152014-08-14 10:53:27 -0700314 mPressedBackground = null;
315 mIgnorePressedStateChange = false;
316 updateIconState();
317 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800318 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800319
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 @Override
321 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700322 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700323 super.draw(canvas);
324 return;
325 }
326
Michael Jurkabdb5c532011-02-01 15:05:06 -0800327 final Drawable background = mBackground;
328 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700329 final int scrollX = getScrollX();
330 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800331
Michael Jurkabdb5c532011-02-01 15:05:06 -0800332 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700333 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800334 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800336
337 if ((scrollX | scrollY) == 0) {
338 background.draw(canvas);
339 } else {
340 canvas.translate(scrollX, scrollY);
341 background.draw(canvas);
342 canvas.translate(-scrollX, -scrollY);
343 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800345
346 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800347 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800348 getPaint().clearShadowLayer();
349 super.draw(canvas);
350 return;
351 }
352
Michael Jurkabdb5c532011-02-01 15:05:06 -0800353 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800354 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800355 super.draw(canvas);
356 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700357 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
358 getScrollX() + getWidth(),
359 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800360 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800361 super.draw(canvas);
362 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364
365 @Override
366 protected void onAttachedToWindow() {
367 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700368
Winson Chung656d11c2010-11-29 17:15:47 -0800369 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700370
Winson Chungb745afb2015-03-02 11:51:23 -0800371 if (mIcon instanceof PreloadIconDrawable) {
372 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700373 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400374 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375 }
376
377 @Override
378 protected void onDetachedFromWindow() {
379 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800380 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700382
Adam Cohen477828c2013-09-20 12:05:49 -0700383 @Override
384 public void setTextColor(int color) {
385 mTextColor = color;
386 super.setTextColor(color);
387 }
388
Adam Cohen96bb7982014-07-07 11:58:56 -0700389 @Override
390 public void setTextColor(ColorStateList colors) {
391 mTextColor = colors.getDefaultColor();
392 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700393 }
394
Winson Chung5f8afe62013-08-12 16:19:28 -0700395 public void setTextVisibility(boolean visible) {
396 Resources res = getResources();
397 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700398 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700399 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700400 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700401 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700402 }
403
Winson Chungaffd7b42010-08-20 15:11:56 -0700404 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800405 public void cancelLongPress() {
406 super.cancelLongPress();
407
408 mLongPressHelper.cancelLongPress();
409 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500410
Sunny Goyal34942622014-08-29 17:20:55 -0700411 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700412 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400413 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700414 final boolean isPromise = info.isPromise();
415 final int progressLevel = isPromise ?
416 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
417 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700418
Winson Chungb745afb2015-03-02 11:51:23 -0800419 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700420 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800421 if (mIcon instanceof PreloadIconDrawable) {
422 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700423 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800424 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700425 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700426 }
427
428 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700429 if (promiseStateChanged) {
430 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700431 }
Sunny Goyale755d462014-07-22 13:48:29 -0700432 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400433 }
434 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700435
436 private Theme getPreloaderTheme() {
437 Object tag = getTag();
438 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
439 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
440 : R.style.PreloadIcon;
441 Theme theme = sPreloaderThemes.get(style);
442 if (theme == null) {
443 theme = getResources().newTheme();
444 theme.applyStyle(style, true);
445 sPreloaderThemes.put(style, theme);
446 }
447 return theme;
448 }
Winson Chungb745afb2015-03-02 11:51:23 -0800449
450 /**
451 * Sets the icon for this view based on the layout direction.
452 */
Sunny Goyal70660032015-05-14 00:07:08 -0700453 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700454 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800455 mIcon = icon;
456 if (iconSize != -1) {
457 mIcon.setBounds(0, 0, iconSize, iconSize);
458 }
459 if (mLayoutHorizontal) {
Sunny Goyal70660032015-05-14 00:07:08 -0700460 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
461 setCompoundDrawablesRelative(mIcon, null, null, null);
462 } else {
463 setCompoundDrawables(mIcon, null, null, null);
464 }
Winson Chungb745afb2015-03-02 11:51:23 -0800465 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700466 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800467 }
Winson Chungb745afb2015-03-02 11:51:23 -0800468 return icon;
469 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700470
Sunny Goyal69b75642015-05-15 17:00:24 -0700471 @Override
472 public void requestLayout() {
473 if (!mDisableRelayout) {
474 super.requestLayout();
475 }
476 }
477
Sunny Goyal34b65272015-03-11 16:56:52 -0700478 /**
479 * Applies the item info if it is same as what the view is pointing to currently.
480 */
481 public void reapplyItemInfo(final ItemInfo info) {
482 if (getTag() == info) {
483 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700484 mDisableRelayout = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700485 if (info instanceof AppInfo) {
486 applyFromApplicationInfo((AppInfo) info);
487 } else if (info instanceof ShortcutInfo) {
488 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700489 LauncherAppState.getInstance().getIconCache());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700490 } else if (info instanceof PackageItemInfo) {
491 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700492 }
Sunny Goyal69b75642015-05-15 17:00:24 -0700493 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700494 }
495 }
496
497 /**
498 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
499 */
500 public void verifyHighRes() {
501 if (mIconLoadRequest != null) {
502 mIconLoadRequest.cancel();
503 mIconLoadRequest = null;
504 }
505 if (getTag() instanceof AppInfo) {
506 AppInfo info = (AppInfo) getTag();
507 if (info.usingLowResIcon) {
508 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
509 .updateIconInBackground(BubbleTextView.this, info);
510 }
511 } else if (getTag() instanceof ShortcutInfo) {
512 ShortcutInfo info = (ShortcutInfo) getTag();
513 if (info.usingLowResIcon) {
514 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
515 .updateIconInBackground(BubbleTextView.this, info);
516 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700517 } else if (getTag() instanceof PackageItemInfo) {
518 PackageItemInfo info = (PackageItemInfo) getTag();
519 if (info.usingLowResIcon) {
520 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
521 .updateIconInBackground(BubbleTextView.this, info);
522 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700523 }
524 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700525
526 /**
527 * Interface to be implemented by the grand parent to allow click shadow effect.
528 */
529 public static interface BubbleTextShadowHandler {
530 void setPressedIcon(BubbleTextView icon, Bitmap background);
531 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532}