blob: 8f28921184df1888ee794c3f2f500df90ebd610b [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 Goyal95abbb32014-08-04 10:53:22 -070031import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070032import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070033import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080034import android.view.MotionEvent;
Jason Monk02dd7ae2014-04-15 15:23:31 -040035import android.view.ViewConfiguration;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070036import android.view.ViewParent;
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;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050080
Sunny Goyal34b65272015-03-11 16:56:52 -070081 private IconLoadRequest mIconLoadRequest;
82
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070084 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 }
86
87 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070088 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 }
90
91 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
92 super(context, attrs, defStyle);
Winson Chungb745afb2015-03-02 11:51:23 -080093 LauncherAppState app = LauncherAppState.getInstance();
94 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070095
Adam Cohen96bb7982014-07-07 11:58:56 -070096 TypedArray a = context.obtainStyledAttributes(attrs,
97 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -070098 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -080099 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700100 mDeferShadowGenerationOnTouch =
101 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700102
103 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
104 int defaultIconSize = grid.iconSizePx;
105 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700106 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700107 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700108 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700109 defaultIconSize = grid.allAppsIconSizePx;
110 }
111
112 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
113 defaultIconSize);
114
Adam Cohen96bb7982014-07-07 11:58:56 -0700115 a.recycle();
116
Sunny Goyal15872da2014-07-08 15:43:54 -0700117 if (mCustomShadowsEnabled) {
118 // Draw the background itself as the parent is drawn twice.
119 mBackground = getBackground();
120 setBackground(null);
121 } else {
122 mBackground = null;
123 }
Winson Chungb745afb2015-03-02 11:51:23 -0800124
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800125 mLongPressHelper = new CheckLongPressHelper(this);
126
127 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
128 if (mCustomShadowsEnabled) {
129 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
130 }
131
132 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 }
134
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700135 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
136 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700137 }
138
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700139 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700140 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800141 Bitmap b = info.getIcon(iconCache);
142
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700143 FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700144 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700145
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700146 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100147 if (info.contentDescription != null) {
148 setContentDescription(info.contentDescription);
149 }
Sunny Goyal508da152014-08-14 10:53:27 -0700150 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800151 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700152
Sunny Goyal34942622014-08-29 17:20:55 -0700153 if (promiseStateChanged || info.isPromise()) {
154 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500155 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800156 }
157
Sunny Goyal508da152014-08-14 10:53:27 -0700158 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700159 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700160 setText(info.title);
161 if (info.contentDescription != null) {
162 setContentDescription(info.contentDescription);
163 }
Winson Chung888b3a12015-03-13 11:14:16 -0700164 // We don't need to check the info since it's not a ShortcutInfo
165 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700166
167 // Verify high res immediately
168 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700169 }
170
Sunny Goyal0e08f162015-05-12 11:32:39 -0700171 public void applyFromPackageItemInfo(PackageItemInfo info) {
172 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
173 setText(info.title);
174 if (info.contentDescription != null) {
175 setContentDescription(info.contentDescription);
176 }
177 // We don't need to check the info since it's not a ShortcutInfo
178 super.setTag(info);
179
180 // Verify high res immediately
181 verifyHighRes();
182 }
183
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 @Override
186 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700187 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 mBackgroundSizeChanged = true;
189 }
190 return super.setFrame(left, top, right, bottom);
191 }
192
193 @Override
194 protected boolean verifyDrawable(Drawable who) {
195 return who == mBackground || super.verifyDrawable(who);
196 }
197
198 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700199 public void setTag(Object tag) {
200 if (tag != null) {
201 LauncherModel.checkItemInfo((ItemInfo) tag);
202 }
203 super.setTag(tag);
204 }
205
206 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700207 public void setPressed(boolean pressed) {
208 super.setPressed(pressed);
209
210 if (!mIgnorePressedStateChange) {
211 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800212 }
Sunny Goyal508da152014-08-14 10:53:27 -0700213 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800214
Winson Chungb745afb2015-03-02 11:51:23 -0800215 /** Returns the icon for this view. */
216 public Drawable getIcon() {
217 return mIcon;
218 }
219
220 /** Returns whether the layout is horizontal. */
221 public boolean isLayoutHorizontal() {
222 return mLayoutHorizontal;
223 }
224
Sunny Goyal508da152014-08-14 10:53:27 -0700225 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800226 if (mIcon instanceof FastBitmapDrawable) {
227 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800229 }
230
231 @Override
232 public boolean onTouchEvent(MotionEvent event) {
233 // Call the superclass onTouchEvent first, because sometimes it changes the state to
234 // isPressed() on an ACTION_UP
235 boolean result = super.onTouchEvent(event);
236
237 switch (event.getAction()) {
238 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700239 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800240 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
241 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700242 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700243 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800244 }
Winson Chung88f33452012-02-23 15:23:44 -0800245
246 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247 break;
248 case MotionEvent.ACTION_CANCEL:
249 case MotionEvent.ACTION_UP:
250 // If we've touched down and up on an item, and it's still not "pressed", then
251 // destroy the pressed outline
252 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700253 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800254 }
Winson Chung88f33452012-02-23 15:23:44 -0800255
256 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800257 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400258 case MotionEvent.ACTION_MOVE:
259 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
260 mLongPressHelper.cancelLongPress();
261 }
262 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800263 }
264 return result;
265 }
266
Michael Jurkaddd62e92011-02-16 17:49:14 -0800267 void setStayPressed(boolean stayPressed) {
268 mStayPressed = stayPressed;
269 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700270 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700271 } else {
272 if (mPressedBackground == null) {
273 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
274 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800275 }
Sunny Goyal508da152014-08-14 10:53:27 -0700276
277 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700278 ViewParent parent = getParent();
279 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
280 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
281 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
469 /**
470 * Applies the item info if it is same as what the view is pointing to currently.
471 */
472 public void reapplyItemInfo(final ItemInfo info) {
473 if (getTag() == info) {
474 mIconLoadRequest = null;
475 if (info instanceof AppInfo) {
476 applyFromApplicationInfo((AppInfo) info);
477 } else if (info instanceof ShortcutInfo) {
478 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700479 LauncherAppState.getInstance().getIconCache());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700480 } else if (info instanceof PackageItemInfo) {
481 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700482 }
483 }
484 }
485
486 /**
487 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
488 */
489 public void verifyHighRes() {
490 if (mIconLoadRequest != null) {
491 mIconLoadRequest.cancel();
492 mIconLoadRequest = null;
493 }
494 if (getTag() instanceof AppInfo) {
495 AppInfo info = (AppInfo) getTag();
496 if (info.usingLowResIcon) {
497 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
498 .updateIconInBackground(BubbleTextView.this, info);
499 }
500 } else if (getTag() instanceof ShortcutInfo) {
501 ShortcutInfo info = (ShortcutInfo) getTag();
502 if (info.usingLowResIcon) {
503 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
504 .updateIconInBackground(BubbleTextView.this, info);
505 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700506 } else if (getTag() instanceof PackageItemInfo) {
507 PackageItemInfo info = (PackageItemInfo) getTag();
508 if (info.usingLowResIcon) {
509 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
510 .updateIconInBackground(BubbleTextView.this, info);
511 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700512 }
513 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700514
515 /**
516 * Interface to be implemented by the grand parent to allow click shadow effect.
517 */
518 public static interface BubbleTextShadowHandler {
519 void setPressedIcon(BubbleTextView icon, Bitmap background);
520 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521}