blob: e49ead0f7527b1559c01697711269b5191fe6a6b [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;
Winson Chung656d11c2010-11-29 17:15:47 -080022import android.content.res.Resources;
Adam Cohen96bb7982014-07-07 11:58:56 -070023import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080024import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Canvas;
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;
Michael Jurka137142e2011-01-05 20:57:04 -080029import android.graphics.Region;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080031import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080032import android.util.Property;
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;
Sunny Goyal317698b2015-07-29 11:45:41 -070036import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040037import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080038import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070039import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080040import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070041
Sunny Goyal34b65272015-03-11 16:56:52 -070042import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080043import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080044import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080045import com.android.launcher3.badge.BadgeRenderer;
Jon Miranda529af302017-02-28 14:18:54 -080046import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080047import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070048import com.android.launcher3.graphics.HolographicOutlineHelper;
Tony Wickham1237df02017-02-24 08:59:36 -080049import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080050import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070051import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070052
Sunny Goyalc469aad2015-10-01 11:24:23 -070053import java.text.NumberFormat;
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055/**
56 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
57 * because we want to make the bubble taller than the text and TextView's clip is
58 * too aggressive.
59 */
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080060public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070061
Sunny Goyal8f8f3982016-07-25 17:08:38 -070062 // Dimensions in DP
63 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
64 private static final float KEY_SHADOW_RADIUS = 1f;
65 private static final float KEY_SHADOW_OFFSET = 0.5f;
66 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
67 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080068
Sunny Goyaldfaccf62015-05-11 16:30:44 -070069 private static final int DISPLAY_WORKSPACE = 0;
70 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070071 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070072
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080073 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
74
Sunny Goyal53d7ee42015-05-22 12:25:45 -070075 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080076 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070077 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070079 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080 private final CheckLongPressHelper mLongPressHelper;
81 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070082 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080083
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080084 private boolean mBackgroundSizeChanged;
85
Sunny Goyal508da152014-08-14 10:53:27 -070086 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080087
Jason Monk02dd7ae2014-04-15 15:23:31 -040088 private float mSlop;
89
Winson Chung93f98ea2015-03-10 16:28:47 -070090 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070091 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080092 private final boolean mLayoutHorizontal;
93 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080095 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070096
Tony Wickham1237df02017-02-24 08:59:36 -080097 private BadgeInfo mBadgeInfo;
98 private BadgeRenderer mBadgeRenderer;
99 private IconPalette mIconPalette;
100 private float mBadgeScale;
101 private boolean mForceHideBadge;
102 private Point mTempSpaceForBadgeOffset = new Point();
103 private Rect mTempIconBounds = new Rect();
104
105 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
106 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
107 @Override
108 public Float get(BubbleTextView bubbleTextView) {
109 return bubbleTextView.mBadgeScale;
110 }
111
112 @Override
113 public void set(BubbleTextView bubbleTextView, Float value) {
114 bubbleTextView.mBadgeScale = value;
115 bubbleTextView.invalidate();
116 }
117 };
118
Sunny Goyal4ffec482016-02-09 11:28:52 -0800119 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800120 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800121 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700122 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800123 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700124 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500125
Sunny Goyal34b65272015-03-11 16:56:52 -0700126 private IconLoadRequest mIconLoadRequest;
127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700129 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 }
131
132 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700133 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 }
135
136 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
137 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700138 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700139 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700140
Adam Cohen96bb7982014-07-07 11:58:56 -0700141 TypedArray a = context.obtainStyledAttributes(attrs,
142 R.styleable.BubbleTextView, defStyle, 0);
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800143 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, false);
Winson Chungb745afb2015-03-02 11:51:23 -0800144 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700145 mDeferShadowGenerationOnTouch =
146 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700147
148 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
149 int defaultIconSize = grid.iconSizePx;
150 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700151 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700152 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700153 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
154 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700155 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700156 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700157 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700158 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700159 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700160 }
Winson1f064272016-07-18 17:18:02 -0700161 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700162
163 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
164 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700165 a.recycle();
166
Sunny Goyal15872da2014-07-08 15:43:54 -0700167 if (mCustomShadowsEnabled) {
168 // Draw the background itself as the parent is drawn twice.
169 mBackground = getBackground();
170 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700171
172 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
173 float density = getResources().getDisplayMetrics().density;
174 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700175 } else {
176 mBackground = null;
177 }
Winson Chungb745afb2015-03-02 11:51:23 -0800178
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800179 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700180 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800181
Sunny Goyal10629b02016-09-01 12:50:11 -0700182 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700183 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 }
185
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800186 public void applyFromShortcutInfo(ShortcutInfo info) {
187 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700188 }
189
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800190 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
191 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800192 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700193 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800194 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500195 }
Tony Wickham010d2552017-01-20 08:15:28 -0800196
Tony Wickham1e618492017-02-02 12:57:18 -0800197 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800198 }
199
Sunny Goyal508da152014-08-14 10:53:27 -0700200 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700201 applyIconAndLabel(info.iconBitmap, info);
202
Winson Chung888b3a12015-03-13 11:14:16 -0700203 // We don't need to check the info since it's not a ShortcutInfo
204 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700205
206 // Verify high res immediately
207 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800208
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700209 if (info instanceof PromiseAppInfo) {
210 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
211 applyProgressLevel(promiseAppInfo.level);
212 }
Tony Wickham1e618492017-02-02 12:57:18 -0800213 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700214 }
215
Sunny Goyal0e08f162015-05-12 11:32:39 -0700216 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700217 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700218 // We don't need to check the info since it's not a ShortcutInfo
219 super.setTag(info);
220
221 // Verify high res immediately
222 verifyHighRes();
223 }
224
Sunny Goyalf4204382016-07-13 10:46:07 -0700225 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800226 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800227 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700228 setIcon(iconDrawable);
229 setText(info.title);
230 if (info.contentDescription != null) {
231 setContentDescription(info.isDisabled()
232 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
233 : info.contentDescription);
234 }
235 }
236
Winson Chung7501adf2015-06-02 11:24:28 -0700237 /**
238 * Overrides the default long press timeout.
239 */
240 public void setLongPressTimeout(int longPressTimeout) {
241 mLongPressHelper.setLongPressTimeout(longPressTimeout);
242 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 @Override
245 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700246 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 mBackgroundSizeChanged = true;
248 }
249 return super.setFrame(left, top, right, bottom);
250 }
251
252 @Override
253 protected boolean verifyDrawable(Drawable who) {
254 return who == mBackground || super.verifyDrawable(who);
255 }
256
257 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700258 public void setTag(Object tag) {
259 if (tag != null) {
260 LauncherModel.checkItemInfo((ItemInfo) tag);
261 }
262 super.setTag(tag);
263 }
264
265 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800266 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700267 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800268 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800269 }
Sunny Goyal508da152014-08-14 10:53:27 -0700270 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800271
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800272 @Override
273 protected int[] onCreateDrawableState(int extraSpace) {
274 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
275 if (mStayPressed) {
276 mergeDrawableStates(drawableState, STATE_PRESSED);
277 }
278 return drawableState;
279 }
280
Winson Chungb745afb2015-03-02 11:51:23 -0800281 /** Returns the icon for this view. */
282 public Drawable getIcon() {
283 return mIcon;
284 }
285
286 /** Returns whether the layout is horizontal. */
287 public boolean isLayoutHorizontal() {
288 return mLayoutHorizontal;
289 }
290
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800291 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700292 public void setOnLongClickListener(OnLongClickListener l) {
293 super.setOnLongClickListener(l);
294 mOnLongClickListener = l;
295 }
296
297 public OnLongClickListener getOnLongClickListener() {
298 return mOnLongClickListener;
299 }
300
301 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800302 public boolean onTouchEvent(MotionEvent event) {
303 // Call the superclass onTouchEvent first, because sometimes it changes the state to
304 // isPressed() on an ACTION_UP
305 boolean result = super.onTouchEvent(event);
306
Mady Melloref044dd2015-06-02 15:35:07 -0700307 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700308 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700309 mLongPressHelper.cancelLongPress();
310 result = true;
311 }
312
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800313 switch (event.getAction()) {
314 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700315 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800316 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
317 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700318 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700319 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800320 }
Winson Chung88f33452012-02-23 15:23:44 -0800321
Mady Melloref044dd2015-06-02 15:35:07 -0700322 // If we're in a stylus button press, don't check for long press.
323 if (!mStylusEventHelper.inStylusButtonPressed()) {
324 mLongPressHelper.postCheckForLongPress();
325 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800326 break;
327 case MotionEvent.ACTION_CANCEL:
328 case MotionEvent.ACTION_UP:
329 // If we've touched down and up on an item, and it's still not "pressed", then
330 // destroy the pressed outline
331 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700332 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800333 }
Winson Chung88f33452012-02-23 15:23:44 -0800334
335 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800336 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400337 case MotionEvent.ACTION_MOVE:
338 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
339 mLongPressHelper.cancelLongPress();
340 }
341 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800342 }
343 return result;
344 }
345
Michael Jurkaddd62e92011-02-16 17:49:14 -0800346 void setStayPressed(boolean stayPressed) {
347 mStayPressed = stayPressed;
348 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700349 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700350 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700351 } else {
352 if (mPressedBackground == null) {
353 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
354 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800355 }
Sunny Goyal508da152014-08-14 10:53:27 -0700356
357 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700358 ViewParent parent = getParent();
359 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
360 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
361 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700362 }
363
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800364 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800365 }
366
Sunny Goyal508da152014-08-14 10:53:27 -0700367 void clearPressedBackground() {
368 setPressed(false);
369 setStayPressed(false);
370 }
371
372 @Override
373 public boolean onKeyDown(int keyCode, KeyEvent event) {
374 if (super.onKeyDown(keyCode, event)) {
375 // Pre-create shadow so show immediately on click.
376 if (mPressedBackground == null) {
377 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700378 }
Sunny Goyal508da152014-08-14 10:53:27 -0700379 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700380 }
Sunny Goyal508da152014-08-14 10:53:27 -0700381 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800382 }
383
Sunny Goyal508da152014-08-14 10:53:27 -0700384 @Override
385 public boolean onKeyUp(int keyCode, KeyEvent event) {
386 // Unlike touch events, keypress event propagate pressed state change immediately,
387 // without waiting for onClickHandler to execute. Disable pressed state changes here
388 // to avoid flickering.
389 mIgnorePressedStateChange = true;
390 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700391
Sunny Goyal508da152014-08-14 10:53:27 -0700392 mPressedBackground = null;
393 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800394 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700395 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800396 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 @Override
399 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700400 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700401 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800402 drawBadgeIfNecessary(canvas);
Adam Cohen477828c2013-09-20 12:05:49 -0700403 return;
404 }
405
Michael Jurkabdb5c532011-02-01 15:05:06 -0800406 final Drawable background = mBackground;
407 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700408 final int scrollX = getScrollX();
409 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800410
Michael Jurkabdb5c532011-02-01 15:05:06 -0800411 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700412 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800413 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800415
416 if ((scrollX | scrollY) == 0) {
417 background.draw(canvas);
418 } else {
419 canvas.translate(scrollX, scrollY);
420 background.draw(canvas);
421 canvas.translate(-scrollX, -scrollY);
422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800424
425 // If text is transparent, don't draw any shadow
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800426 if ((getCurrentTextColor() >> 24) == 0) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800427 getPaint().clearShadowLayer();
428 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800429 drawBadgeIfNecessary(canvas);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800430 return;
431 }
432
Michael Jurkabdb5c532011-02-01 15:05:06 -0800433 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700434 float density = getResources().getDisplayMetrics().density;
435 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800436 super.draw(canvas);
437 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700438 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
439 getScrollX() + getWidth(),
440 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700441 getPaint().setShadowLayer(
442 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800443 super.draw(canvas);
444 canvas.restore();
Tony Wickham1237df02017-02-24 08:59:36 -0800445
446 drawBadgeIfNecessary(canvas);
447 }
448
449 /**
450 * Draws the icon badge in the top right corner of the icon bounds.
451 * @param canvas The canvas to draw to.
452 */
453 private void drawBadgeIfNecessary(Canvas canvas) {
454 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
455 getIconBounds(mTempIconBounds);
456 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
457 final int scrollX = getScrollX();
458 final int scrollY = getScrollY();
459 canvas.translate(scrollX, scrollY);
Tony Wickhame6a790e2017-05-16 12:07:38 -0700460 mBadgeRenderer.draw(canvas, mBadgeInfo, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800461 mTempSpaceForBadgeOffset);
462 canvas.translate(-scrollX, -scrollY);
463 }
464 }
465
466 public void forceHideBadge(boolean forceHideBadge) {
467 if (mForceHideBadge == forceHideBadge) {
468 return;
469 }
470 mForceHideBadge = forceHideBadge;
471
472 if (forceHideBadge) {
473 invalidate();
474 } else if (hasBadge()) {
475 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
476 }
477 }
478
479 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700480 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800481 }
482
483 public void getIconBounds(Rect outBounds) {
484 int top = getPaddingTop();
485 int left = (getWidth() - mIconSize) / 2;
486 int right = left + mIconSize;
487 int bottom = top + mIconSize;
488 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400490
491 @Override
492 protected void onAttachedToWindow() {
493 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700494
Winson Chung656d11c2010-11-29 17:15:47 -0800495 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400496 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400497 }
498
499 @Override
Winson1f064272016-07-18 17:18:02 -0700500 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
501 if (mCenterVertically) {
502 Paint.FontMetrics fm = getPaint().getFontMetrics();
503 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
504 (int) Math.ceil(fm.bottom - fm.top);
505 int height = MeasureSpec.getSize(heightMeasureSpec);
506 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
507 getPaddingBottom());
508 }
509 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
510 }
511
512 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400513 protected void onDetachedFromWindow() {
514 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800515 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700517
Adam Cohen477828c2013-09-20 12:05:49 -0700518 @Override
519 public void setTextColor(int color) {
520 mTextColor = color;
521 super.setTextColor(color);
522 }
523
Adam Cohen96bb7982014-07-07 11:58:56 -0700524 @Override
525 public void setTextColor(ColorStateList colors) {
526 mTextColor = colors.getDefaultColor();
527 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700528 }
529
Winson Chung5f8afe62013-08-12 16:19:28 -0700530 public void setTextVisibility(boolean visible) {
531 Resources res = getResources();
532 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700533 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700534 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700535 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700536 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700537 }
538
Winson Chungaffd7b42010-08-20 15:11:56 -0700539 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800540 public void cancelLongPress() {
541 super.cancelLongPress();
542
543 mLongPressHelper.cancelLongPress();
544 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500545
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800546 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700547 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400548 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700549 final boolean isPromise = info.isPromise();
550 final int progressLevel = isPromise ?
551 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
552 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700553
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700554 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
555 if (preloadDrawable != null && promiseStateChanged) {
556 preloadDrawable.maybePerformFinishedAnimation();
557 }
558 }
559 }
560
561 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
562 if (getTag() instanceof ItemInfoWithIcon) {
563 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
564 setContentDescription(progressLevel > 0
565 ? getContext().getString(R.string.app_downloading_title, info.title,
566 NumberFormat.getPercentInstance().format(progressLevel * 0.01))
567 : getContext().getString(R.string.app_waiting_download_title, info.title));
Sunny Goyalc469aad2015-10-01 11:24:23 -0700568
Winson Chungb745afb2015-03-02 11:51:23 -0800569 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700570 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800571 if (mIcon instanceof PreloadIconDrawable) {
572 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700573 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700574 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800575 preloadDrawable = DrawableFactory.get(getContext())
576 .newPendingIcon(info.iconBitmap, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700577 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700578 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700579 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700580 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700581 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400582 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700583 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400584 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700585
Tony Wickham1e618492017-02-02 12:57:18 -0800586 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800587 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800588 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700589 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
590 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800591 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700592 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800593 if (wasBadged || isBadged) {
594 mIconPalette = ((FastBitmapDrawable) mIcon).getIconPalette();
595 // Animate when a badge is first added or when it is removed.
596 if (animate && (wasBadged ^ isBadged) && isShown()) {
597 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
598 } else {
599 mBadgeScale = newBadgeScale;
600 invalidate();
601 }
602 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800603 }
604 }
605
Winson Chungb745afb2015-03-02 11:51:23 -0800606 /**
607 * Sets the icon for this view based on the layout direction.
608 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700609 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800610 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800611 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700612 applyCompoundDrawables(mIcon);
613 }
614
615 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800616 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800617 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800618 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700619 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800620 }
Winson Chungb745afb2015-03-02 11:51:23 -0800621 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700622
Sunny Goyal69b75642015-05-15 17:00:24 -0700623 @Override
624 public void requestLayout() {
625 if (!mDisableRelayout) {
626 super.requestLayout();
627 }
628 }
629
Sunny Goyal34b65272015-03-11 16:56:52 -0700630 /**
631 * Applies the item info if it is same as what the view is pointing to currently.
632 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800633 @Override
634 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700635 if (getTag() == info) {
636 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700637 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700638
Sunny Goyal34b65272015-03-11 16:56:52 -0700639 if (info instanceof AppInfo) {
640 applyFromApplicationInfo((AppInfo) info);
641 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800642 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800643 FolderIconPreviewVerifier verifier =
644 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
645 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700646 View folderIcon =
647 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
648 if (folderIcon != null) {
649 folderIcon.invalidate();
650 }
651 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700652 } else if (info instanceof PackageItemInfo) {
653 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700654 }
Winsonc0880492015-08-21 11:16:27 -0700655
Sunny Goyal69b75642015-05-15 17:00:24 -0700656 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700657 }
658 }
659
660 /**
661 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
662 */
663 public void verifyHighRes() {
664 if (mIconLoadRequest != null) {
665 mIconLoadRequest.cancel();
666 mIconLoadRequest = null;
667 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800668 if (getTag() instanceof ItemInfoWithIcon) {
669 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700670 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800671 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700672 .updateIconInBackground(BubbleTextView.this, info);
673 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700674 }
675 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700676
Jon Miranda47170112017-03-03 14:57:14 -0800677 public int getIconSize() {
678 return mIconSize;
679 }
680
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700681 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700682 * Interface to be implemented by the grand parent to allow click shadow effect.
683 */
Winsonc0880492015-08-21 11:16:27 -0700684 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700685 void setPressedIcon(BubbleTextView icon, Bitmap background);
686 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687}