blob: 6f2c897796b6a6078810db60dce47ebd89d7e0ea [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
Tony Wickham1237df02017-02-24 08:59:36 -080019import android.animation.ObjectAnimator;
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;
Adam Cohen96bb7982014-07-07 11:58:56 -070022import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080023import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070025import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070026import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080027import android.graphics.Point;
28import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070029import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.drawable.Drawable;
Tony8f402802017-06-16 17:24:54 -070031import android.support.v4.graphics.ColorUtils;
Winson Chung656d11c2010-11-29 17:15:47 -080032import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080033import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070034import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070035import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080036import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070037import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040038import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080039import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070040import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080041import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070042
Sunny Goyal34b65272015-03-11 16:56:52 -070043import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080044import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080045import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080046import com.android.launcher3.badge.BadgeRenderer;
Jon Miranda529af302017-02-28 14:18:54 -080047import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080048import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070049import com.android.launcher3.graphics.HolographicOutlineHelper;
Tony Wickham1237df02017-02-24 08:59:36 -080050import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080051import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070052import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070053
Sunny Goyalc469aad2015-10-01 11:24:23 -070054import java.text.NumberFormat;
55
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056/**
57 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
58 * because we want to make the bubble taller than the text and TextView's clip is
59 * too aggressive.
60 */
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080061public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070062
Sunny Goyaldfaccf62015-05-11 16:30:44 -070063 private static final int DISPLAY_WORKSPACE = 0;
64 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070065 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070066
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080067 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
68
Sunny Goyal53d7ee42015-05-22 12:25:45 -070069 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080070 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070071 private final boolean mCenterVertically;
Sunny Goyal4e5a8782017-06-23 09:34:06 -070072
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080073 private final CheckLongPressHelper mLongPressHelper;
74 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070075 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal9314b7c2017-06-23 10:36:27 -070076 private final float mSlop;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080077
Sunny Goyal508da152014-08-14 10:53:27 -070078 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080079
Winson Chung93f98ea2015-03-10 16:28:47 -070080 private final boolean mDeferShadowGenerationOnTouch;
Winson Chungb745afb2015-03-02 11:51:23 -080081 private final boolean mLayoutHorizontal;
82 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080083 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080084 private int mTextColor;
Tony17b7f9b2017-05-23 12:19:09 -070085 private boolean mIsIconVisible = true;
Winson Chung5f8afe62013-08-12 16:19:28 -070086
Tony Wickham1237df02017-02-24 08:59:36 -080087 private BadgeInfo mBadgeInfo;
88 private BadgeRenderer mBadgeRenderer;
Tony Wickham7092db02017-06-07 14:32:23 -070089 private IconPalette mBadgePalette;
Tony Wickham1237df02017-02-24 08:59:36 -080090 private float mBadgeScale;
91 private boolean mForceHideBadge;
92 private Point mTempSpaceForBadgeOffset = new Point();
93 private Rect mTempIconBounds = new Rect();
94
95 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
96 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
97 @Override
98 public Float get(BubbleTextView bubbleTextView) {
99 return bubbleTextView.mBadgeScale;
100 }
101
102 @Override
103 public void set(BubbleTextView bubbleTextView, Float value) {
104 bubbleTextView.mBadgeScale = value;
105 bubbleTextView.invalidate();
106 }
107 };
108
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700109 public static final Property<BubbleTextView, Integer> TEXT_ALPHA_PROPERTY
Tony8f402802017-06-16 17:24:54 -0700110 = new Property<BubbleTextView, Integer>(Integer.class, "textAlpha") {
111 @Override
112 public Integer get(BubbleTextView bubbleTextView) {
113 return bubbleTextView.getTextAlpha();
114 }
115
116 @Override
117 public void set(BubbleTextView bubbleTextView, Integer alpha) {
118 bubbleTextView.setTextAlpha(alpha);
119 }
120 };
121
Sunny Goyal4ffec482016-02-09 11:28:52 -0800122 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800123 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800124 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700125 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800126 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700127 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500128
Sunny Goyal34b65272015-03-11 16:56:52 -0700129 private IconLoadRequest mIconLoadRequest;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700132 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 }
134
135 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700136 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138
139 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
140 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700141 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700142 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700143 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700144
Adam Cohen96bb7982014-07-07 11:58:56 -0700145 TypedArray a = context.obtainStyledAttributes(attrs,
146 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800147 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700148 mDeferShadowGenerationOnTouch =
149 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700150
151 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
152 int defaultIconSize = grid.iconSizePx;
153 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700154 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700155 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700156 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700157 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
158 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700159 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700160 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700161 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700162 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700163 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700164 }
Winson1f064272016-07-18 17:18:02 -0700165 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700166
167 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
168 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700169 a.recycle();
170
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800171 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700172 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800173
Sunny Goyal10629b02016-09-01 12:50:11 -0700174 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700175 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 }
178
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800179 public void applyFromShortcutInfo(ShortcutInfo info) {
180 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700181 }
182
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800183 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
184 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800185 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700186 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800187 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500188 }
Tony Wickham010d2552017-01-20 08:15:28 -0800189
Tony Wickham1e618492017-02-02 12:57:18 -0800190 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800191 }
192
Sunny Goyal508da152014-08-14 10:53:27 -0700193 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700194 applyIconAndLabel(info.iconBitmap, info);
195
Winson Chung888b3a12015-03-13 11:14:16 -0700196 // We don't need to check the info since it's not a ShortcutInfo
197 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700198
199 // Verify high res immediately
200 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800201
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700202 if (info instanceof PromiseAppInfo) {
203 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
204 applyProgressLevel(promiseAppInfo.level);
205 }
Tony Wickham1e618492017-02-02 12:57:18 -0800206 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700207 }
208
Sunny Goyal0e08f162015-05-12 11:32:39 -0700209 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700210 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700211 // We don't need to check the info since it's not a ShortcutInfo
212 super.setTag(info);
213
214 // Verify high res immediately
215 verifyHighRes();
216 }
217
Sunny Goyalf4204382016-07-13 10:46:07 -0700218 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800219 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800220 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700221 setIcon(iconDrawable);
222 setText(info.title);
223 if (info.contentDescription != null) {
224 setContentDescription(info.isDisabled()
225 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
226 : info.contentDescription);
227 }
228 }
229
Winson Chung7501adf2015-06-02 11:24:28 -0700230 /**
231 * Overrides the default long press timeout.
232 */
233 public void setLongPressTimeout(int longPressTimeout) {
234 mLongPressHelper.setLongPressTimeout(longPressTimeout);
235 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700238 public void setTag(Object tag) {
239 if (tag != null) {
240 LauncherModel.checkItemInfo((ItemInfo) tag);
241 }
242 super.setTag(tag);
243 }
244
245 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800246 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700247 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800248 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800249 }
Sunny Goyal508da152014-08-14 10:53:27 -0700250 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800251
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800252 @Override
253 protected int[] onCreateDrawableState(int extraSpace) {
254 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
255 if (mStayPressed) {
256 mergeDrawableStates(drawableState, STATE_PRESSED);
257 }
258 return drawableState;
259 }
260
Winson Chungb745afb2015-03-02 11:51:23 -0800261 /** Returns the icon for this view. */
262 public Drawable getIcon() {
263 return mIcon;
264 }
265
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700266 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800267 public boolean onTouchEvent(MotionEvent event) {
268 // Call the superclass onTouchEvent first, because sometimes it changes the state to
269 // isPressed() on an ACTION_UP
270 boolean result = super.onTouchEvent(event);
271
Mady Melloref044dd2015-06-02 15:35:07 -0700272 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700273 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700274 mLongPressHelper.cancelLongPress();
275 result = true;
276 }
277
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800278 switch (event.getAction()) {
279 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700280 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800281 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
282 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700283 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700284 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800285 }
Winson Chung88f33452012-02-23 15:23:44 -0800286
Mady Melloref044dd2015-06-02 15:35:07 -0700287 // If we're in a stylus button press, don't check for long press.
288 if (!mStylusEventHelper.inStylusButtonPressed()) {
289 mLongPressHelper.postCheckForLongPress();
290 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800291 break;
292 case MotionEvent.ACTION_CANCEL:
293 case MotionEvent.ACTION_UP:
294 // If we've touched down and up on an item, and it's still not "pressed", then
295 // destroy the pressed outline
296 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700297 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800298 }
Winson Chung88f33452012-02-23 15:23:44 -0800299
300 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800301 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400302 case MotionEvent.ACTION_MOVE:
303 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
304 mLongPressHelper.cancelLongPress();
305 }
306 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800307 }
308 return result;
309 }
310
Michael Jurkaddd62e92011-02-16 17:49:14 -0800311 void setStayPressed(boolean stayPressed) {
312 mStayPressed = stayPressed;
313 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700314 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700315 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700316 } else {
317 if (mPressedBackground == null) {
318 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
319 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800320 }
Sunny Goyal508da152014-08-14 10:53:27 -0700321
322 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700323 ViewParent parent = getParent();
324 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
325 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
326 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700327 }
328
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800329 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800330 }
331
Sunny Goyal508da152014-08-14 10:53:27 -0700332 void clearPressedBackground() {
333 setPressed(false);
334 setStayPressed(false);
335 }
336
337 @Override
338 public boolean onKeyDown(int keyCode, KeyEvent event) {
339 if (super.onKeyDown(keyCode, event)) {
340 // Pre-create shadow so show immediately on click.
341 if (mPressedBackground == null) {
342 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700343 }
Sunny Goyal508da152014-08-14 10:53:27 -0700344 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700345 }
Sunny Goyal508da152014-08-14 10:53:27 -0700346 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800347 }
348
Sunny Goyal508da152014-08-14 10:53:27 -0700349 @Override
350 public boolean onKeyUp(int keyCode, KeyEvent event) {
351 // Unlike touch events, keypress event propagate pressed state change immediately,
352 // without waiting for onClickHandler to execute. Disable pressed state changes here
353 // to avoid flickering.
354 mIgnorePressedStateChange = true;
355 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700356
Sunny Goyal508da152014-08-14 10:53:27 -0700357 mPressedBackground = null;
358 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800359 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700360 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800361 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800362
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700363 @SuppressWarnings("wrongcall")
364 protected void drawWithoutBadge(Canvas canvas) {
365 super.onDraw(canvas);
366 }
367
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700369 public void onDraw(Canvas canvas) {
370 super.onDraw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800371 drawBadgeIfNecessary(canvas);
372 }
373
374 /**
375 * Draws the icon badge in the top right corner of the icon bounds.
376 * @param canvas The canvas to draw to.
377 */
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700378 protected void drawBadgeIfNecessary(Canvas canvas) {
Tony Wickham1237df02017-02-24 08:59:36 -0800379 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
380 getIconBounds(mTempIconBounds);
381 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
382 final int scrollX = getScrollX();
383 final int scrollY = getScrollY();
384 canvas.translate(scrollX, scrollY);
Tony Wickham7092db02017-06-07 14:32:23 -0700385 mBadgeRenderer.draw(canvas, mBadgePalette, mBadgeInfo, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800386 mTempSpaceForBadgeOffset);
387 canvas.translate(-scrollX, -scrollY);
388 }
389 }
390
391 public void forceHideBadge(boolean forceHideBadge) {
392 if (mForceHideBadge == forceHideBadge) {
393 return;
394 }
395 mForceHideBadge = forceHideBadge;
396
397 if (forceHideBadge) {
398 invalidate();
399 } else if (hasBadge()) {
400 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
401 }
402 }
403
404 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700405 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800406 }
407
408 public void getIconBounds(Rect outBounds) {
409 int top = getPaddingTop();
410 int left = (getWidth() - mIconSize) / 2;
411 int right = left + mIconSize;
412 int bottom = top + mIconSize;
413 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400415
416 @Override
Winson1f064272016-07-18 17:18:02 -0700417 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
418 if (mCenterVertically) {
419 Paint.FontMetrics fm = getPaint().getFontMetrics();
420 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
421 (int) Math.ceil(fm.bottom - fm.top);
422 int height = MeasureSpec.getSize(heightMeasureSpec);
423 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
424 getPaddingBottom());
425 }
426 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
427 }
428
429 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700430 public void setTextColor(int color) {
431 mTextColor = color;
432 super.setTextColor(color);
433 }
434
Adam Cohen96bb7982014-07-07 11:58:56 -0700435 @Override
436 public void setTextColor(ColorStateList colors) {
437 mTextColor = colors.getDefaultColor();
438 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700439 }
440
Winson Chung5f8afe62013-08-12 16:19:28 -0700441 public void setTextVisibility(boolean visible) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700442 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700443 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700444 } else {
Tony8f402802017-06-16 17:24:54 -0700445 setTextAlpha(0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700446 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700447 }
448
Tony8f402802017-06-16 17:24:54 -0700449 private void setTextAlpha(int alpha) {
450 super.setTextColor(ColorUtils.setAlphaComponent(mTextColor, alpha));
451 }
452
453 private int getTextAlpha() {
454 return Color.alpha(getCurrentTextColor());
455 }
456
457 /**
458 * Creates an animator to fade the text in or out.
459 * @param fadeIn Whether the text should fade in or fade out.
460 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700461 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Tony8f402802017-06-16 17:24:54 -0700462 return ObjectAnimator.ofInt(this, TEXT_ALPHA_PROPERTY, fadeIn ? Color.alpha(mTextColor) : 0);
463 }
464
Winson Chungaffd7b42010-08-20 15:11:56 -0700465 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800466 public void cancelLongPress() {
467 super.cancelLongPress();
468
469 mLongPressHelper.cancelLongPress();
470 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500471
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800472 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700473 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400474 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700475 final boolean isPromise = info.isPromise();
476 final int progressLevel = isPromise ?
477 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
478 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700479
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700480 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
481 if (preloadDrawable != null && promiseStateChanged) {
482 preloadDrawable.maybePerformFinishedAnimation();
483 }
484 }
485 }
486
487 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
488 if (getTag() instanceof ItemInfoWithIcon) {
489 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
490 setContentDescription(progressLevel > 0
491 ? getContext().getString(R.string.app_downloading_title, info.title,
492 NumberFormat.getPercentInstance().format(progressLevel * 0.01))
493 : getContext().getString(R.string.app_waiting_download_title, info.title));
Sunny Goyalc469aad2015-10-01 11:24:23 -0700494
Winson Chungb745afb2015-03-02 11:51:23 -0800495 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700496 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800497 if (mIcon instanceof PreloadIconDrawable) {
498 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700499 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700500 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800501 preloadDrawable = DrawableFactory.get(getContext())
502 .newPendingIcon(info.iconBitmap, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700503 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700504 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700505 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700506 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700507 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400508 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700509 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400510 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700511
Tony Wickham1e618492017-02-02 12:57:18 -0800512 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800513 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800514 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700515 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
516 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800517 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700518 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800519 if (wasBadged || isBadged) {
Tony Wickham7092db02017-06-07 14:32:23 -0700520 mBadgePalette = IconPalette.getBadgePalette(getResources());
521 if (mBadgePalette == null) {
522 mBadgePalette = ((FastBitmapDrawable) mIcon).getIconPalette();
523 }
Tony Wickham1237df02017-02-24 08:59:36 -0800524 // Animate when a badge is first added or when it is removed.
525 if (animate && (wasBadged ^ isBadged) && isShown()) {
526 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
527 } else {
528 mBadgeScale = newBadgeScale;
529 invalidate();
530 }
531 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800532 }
533 }
534
Tony Wickham7092db02017-06-07 14:32:23 -0700535 public IconPalette getBadgePalette() {
536 return mBadgePalette;
537 }
538
Winson Chungb745afb2015-03-02 11:51:23 -0800539 /**
540 * Sets the icon for this view based on the layout direction.
541 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700542 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800543 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800544 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700545 if (mIsIconVisible) {
546 applyCompoundDrawables(mIcon);
547 }
548 }
549
550 public void setIconVisible(boolean visible) {
551 mIsIconVisible = visible;
552 mDisableRelayout = true;
553 Drawable icon = mIcon;
554 if (!visible) {
555 icon = new ColorDrawable(Color.TRANSPARENT);
556 icon.setBounds(0, 0, mIconSize, mIconSize);
557 }
558 applyCompoundDrawables(icon);
559 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700560 }
561
562 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800563 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800564 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800565 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700566 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800567 }
Winson Chungb745afb2015-03-02 11:51:23 -0800568 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700569
Sunny Goyal69b75642015-05-15 17:00:24 -0700570 @Override
571 public void requestLayout() {
572 if (!mDisableRelayout) {
573 super.requestLayout();
574 }
575 }
576
Sunny Goyal34b65272015-03-11 16:56:52 -0700577 /**
578 * Applies the item info if it is same as what the view is pointing to currently.
579 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800580 @Override
581 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700582 if (getTag() == info) {
583 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700584 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700585
Sunny Goyal34b65272015-03-11 16:56:52 -0700586 if (info instanceof AppInfo) {
587 applyFromApplicationInfo((AppInfo) info);
588 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800589 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800590 FolderIconPreviewVerifier verifier =
591 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
592 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700593 View folderIcon =
594 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
595 if (folderIcon != null) {
596 folderIcon.invalidate();
597 }
598 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700599 } else if (info instanceof PackageItemInfo) {
600 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700601 }
Winsonc0880492015-08-21 11:16:27 -0700602
Sunny Goyal69b75642015-05-15 17:00:24 -0700603 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700604 }
605 }
606
607 /**
608 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
609 */
610 public void verifyHighRes() {
611 if (mIconLoadRequest != null) {
612 mIconLoadRequest.cancel();
613 mIconLoadRequest = null;
614 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800615 if (getTag() instanceof ItemInfoWithIcon) {
616 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700617 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800618 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700619 .updateIconInBackground(BubbleTextView.this, info);
620 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700621 }
622 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700623
Jon Miranda47170112017-03-03 14:57:14 -0800624 public int getIconSize() {
625 return mIconSize;
626 }
627
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700628 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700629 * Interface to be implemented by the grand parent to allow click shadow effect.
630 */
Winsonc0880492015-08-21 11:16:27 -0700631 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700632 void setPressedIcon(BubbleTextView icon, Bitmap background);
633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634}