blob: 27e190e5d8323c5bac41fec4e71c9078b78e8fd4 [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;
Tony17b7f9b2017-05-23 12:19:09 -070026import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070027import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080028import android.graphics.Point;
29import android.graphics.Rect;
Michael Jurka137142e2011-01-05 20:57:04 -080030import android.graphics.Region;
Tony17b7f9b2017-05-23 12:19:09 -070031import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080033import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080034import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070035import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070036import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080037import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070038import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040039import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080040import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070041import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080042import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070043
Sunny Goyal34b65272015-03-11 16:56:52 -070044import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080045import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080046import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080047import com.android.launcher3.badge.BadgeRenderer;
Jon Miranda529af302017-02-28 14:18:54 -080048import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080049import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070050import com.android.launcher3.graphics.HolographicOutlineHelper;
Tony Wickham1237df02017-02-24 08:59:36 -080051import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080052import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070053import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070054
Sunny Goyalc469aad2015-10-01 11:24:23 -070055import java.text.NumberFormat;
56
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057/**
58 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
59 * because we want to make the bubble taller than the text and TextView's clip is
60 * too aggressive.
61 */
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080062public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070063
Sunny Goyal8f8f3982016-07-25 17:08:38 -070064 // Dimensions in DP
65 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
66 private static final float KEY_SHADOW_RADIUS = 1f;
67 private static final float KEY_SHADOW_OFFSET = 0.5f;
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;
Mario Bertschlera6936942017-05-31 14:48:19 -070083 private final int mAmbientShadowColor;
84 private final int mKeyShadowColor;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080085
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080086 private boolean mBackgroundSizeChanged;
87
Sunny Goyal508da152014-08-14 10:53:27 -070088 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080089
Jason Monk02dd7ae2014-04-15 15:23:31 -040090 private float mSlop;
91
Winson Chung93f98ea2015-03-10 16:28:47 -070092 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070093 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080094 private final boolean mLayoutHorizontal;
95 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080097 private int mTextColor;
Tony17b7f9b2017-05-23 12:19:09 -070098 private boolean mIsIconVisible = true;
Winson Chung5f8afe62013-08-12 16:19:28 -070099
Tony Wickham1237df02017-02-24 08:59:36 -0800100 private BadgeInfo mBadgeInfo;
101 private BadgeRenderer mBadgeRenderer;
Tony Wickham7092db02017-06-07 14:32:23 -0700102 private IconPalette mBadgePalette;
Tony Wickham1237df02017-02-24 08:59:36 -0800103 private float mBadgeScale;
104 private boolean mForceHideBadge;
105 private Point mTempSpaceForBadgeOffset = new Point();
106 private Rect mTempIconBounds = new Rect();
107
108 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
109 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
110 @Override
111 public Float get(BubbleTextView bubbleTextView) {
112 return bubbleTextView.mBadgeScale;
113 }
114
115 @Override
116 public void set(BubbleTextView bubbleTextView, Float value) {
117 bubbleTextView.mBadgeScale = value;
118 bubbleTextView.invalidate();
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();
Adam Cohen96bb7982014-07-07 11:58:56 -0700143
Adam Cohen96bb7982014-07-07 11:58:56 -0700144 TypedArray a = context.obtainStyledAttributes(attrs,
145 R.styleable.BubbleTextView, defStyle, 0);
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800146 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, false);
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);
Mario Bertschlera6936942017-05-31 14:48:19 -0700150 mAmbientShadowColor = a.getColor(R.styleable.BubbleTextView_ambientShadowColor, 0x33000000);
151 mKeyShadowColor = a.getColor(R.styleable.BubbleTextView_keyShadowColor, 0x66000000);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700152
153 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
154 int defaultIconSize = grid.iconSizePx;
155 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700156 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700157 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700158 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
159 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700160 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700161 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700162 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700163 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700164 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700165 }
Winson1f064272016-07-18 17:18:02 -0700166 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700167
168 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
169 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700170 a.recycle();
171
Sunny Goyal15872da2014-07-08 15:43:54 -0700172 if (mCustomShadowsEnabled) {
173 // Draw the background itself as the parent is drawn twice.
174 mBackground = getBackground();
175 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700176
177 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
178 float density = getResources().getDisplayMetrics().density;
Mario Bertschlera6936942017-05-31 14:48:19 -0700179 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, mAmbientShadowColor);
Sunny Goyal15872da2014-07-08 15:43:54 -0700180 } else {
181 mBackground = null;
182 }
Winson Chungb745afb2015-03-02 11:51:23 -0800183
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800184 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700185 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800186
Sunny Goyal10629b02016-09-01 12:50:11 -0700187 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700188 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 }
190
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800191 public void applyFromShortcutInfo(ShortcutInfo info) {
192 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700193 }
194
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800195 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
196 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800197 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700198 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800199 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500200 }
Tony Wickham010d2552017-01-20 08:15:28 -0800201
Tony Wickham1e618492017-02-02 12:57:18 -0800202 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800203 }
204
Sunny Goyal508da152014-08-14 10:53:27 -0700205 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700206 applyIconAndLabel(info.iconBitmap, info);
207
Winson Chung888b3a12015-03-13 11:14:16 -0700208 // We don't need to check the info since it's not a ShortcutInfo
209 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700210
211 // Verify high res immediately
212 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800213
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700214 if (info instanceof PromiseAppInfo) {
215 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
216 applyProgressLevel(promiseAppInfo.level);
217 }
Tony Wickham1e618492017-02-02 12:57:18 -0800218 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700219 }
220
Sunny Goyal0e08f162015-05-12 11:32:39 -0700221 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700222 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700223 // We don't need to check the info since it's not a ShortcutInfo
224 super.setTag(info);
225
226 // Verify high res immediately
227 verifyHighRes();
228 }
229
Sunny Goyalf4204382016-07-13 10:46:07 -0700230 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800231 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800232 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700233 setIcon(iconDrawable);
234 setText(info.title);
235 if (info.contentDescription != null) {
236 setContentDescription(info.isDisabled()
237 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
238 : info.contentDescription);
239 }
240 }
241
Winson Chung7501adf2015-06-02 11:24:28 -0700242 /**
243 * Overrides the default long press timeout.
244 */
245 public void setLongPressTimeout(int longPressTimeout) {
246 mLongPressHelper.setLongPressTimeout(longPressTimeout);
247 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 @Override
250 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700251 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 mBackgroundSizeChanged = true;
253 }
254 return super.setFrame(left, top, right, bottom);
255 }
256
257 @Override
258 protected boolean verifyDrawable(Drawable who) {
259 return who == mBackground || super.verifyDrawable(who);
260 }
261
262 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700263 public void setTag(Object tag) {
264 if (tag != null) {
265 LauncherModel.checkItemInfo((ItemInfo) tag);
266 }
267 super.setTag(tag);
268 }
269
270 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800271 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700272 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800273 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800274 }
Sunny Goyal508da152014-08-14 10:53:27 -0700275 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800276
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800277 @Override
278 protected int[] onCreateDrawableState(int extraSpace) {
279 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
280 if (mStayPressed) {
281 mergeDrawableStates(drawableState, STATE_PRESSED);
282 }
283 return drawableState;
284 }
285
Winson Chungb745afb2015-03-02 11:51:23 -0800286 /** Returns the icon for this view. */
287 public Drawable getIcon() {
288 return mIcon;
289 }
290
291 /** Returns whether the layout is horizontal. */
292 public boolean isLayoutHorizontal() {
293 return mLayoutHorizontal;
294 }
295
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800296 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700297 public void setOnLongClickListener(OnLongClickListener l) {
298 super.setOnLongClickListener(l);
299 mOnLongClickListener = l;
300 }
301
302 public OnLongClickListener getOnLongClickListener() {
303 return mOnLongClickListener;
304 }
305
306 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800307 public boolean onTouchEvent(MotionEvent event) {
308 // Call the superclass onTouchEvent first, because sometimes it changes the state to
309 // isPressed() on an ACTION_UP
310 boolean result = super.onTouchEvent(event);
311
Mady Melloref044dd2015-06-02 15:35:07 -0700312 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700313 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700314 mLongPressHelper.cancelLongPress();
315 result = true;
316 }
317
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800318 switch (event.getAction()) {
319 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700320 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800321 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
322 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700323 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700324 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800325 }
Winson Chung88f33452012-02-23 15:23:44 -0800326
Mady Melloref044dd2015-06-02 15:35:07 -0700327 // If we're in a stylus button press, don't check for long press.
328 if (!mStylusEventHelper.inStylusButtonPressed()) {
329 mLongPressHelper.postCheckForLongPress();
330 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800331 break;
332 case MotionEvent.ACTION_CANCEL:
333 case MotionEvent.ACTION_UP:
334 // If we've touched down and up on an item, and it's still not "pressed", then
335 // destroy the pressed outline
336 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700337 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800338 }
Winson Chung88f33452012-02-23 15:23:44 -0800339
340 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800341 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400342 case MotionEvent.ACTION_MOVE:
343 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
344 mLongPressHelper.cancelLongPress();
345 }
346 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800347 }
348 return result;
349 }
350
Michael Jurkaddd62e92011-02-16 17:49:14 -0800351 void setStayPressed(boolean stayPressed) {
352 mStayPressed = stayPressed;
353 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700354 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700355 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700356 } else {
357 if (mPressedBackground == null) {
358 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
359 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800360 }
Sunny Goyal508da152014-08-14 10:53:27 -0700361
362 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700363 ViewParent parent = getParent();
364 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
365 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
366 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700367 }
368
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800369 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800370 }
371
Sunny Goyal508da152014-08-14 10:53:27 -0700372 void clearPressedBackground() {
373 setPressed(false);
374 setStayPressed(false);
375 }
376
377 @Override
378 public boolean onKeyDown(int keyCode, KeyEvent event) {
379 if (super.onKeyDown(keyCode, event)) {
380 // Pre-create shadow so show immediately on click.
381 if (mPressedBackground == null) {
382 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700383 }
Sunny Goyal508da152014-08-14 10:53:27 -0700384 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700385 }
Sunny Goyal508da152014-08-14 10:53:27 -0700386 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800387 }
388
Sunny Goyal508da152014-08-14 10:53:27 -0700389 @Override
390 public boolean onKeyUp(int keyCode, KeyEvent event) {
391 // Unlike touch events, keypress event propagate pressed state change immediately,
392 // without waiting for onClickHandler to execute. Disable pressed state changes here
393 // to avoid flickering.
394 mIgnorePressedStateChange = true;
395 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700396
Sunny Goyal508da152014-08-14 10:53:27 -0700397 mPressedBackground = null;
398 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800399 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700400 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800401 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 @Override
404 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700405 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700406 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800407 drawBadgeIfNecessary(canvas);
Adam Cohen477828c2013-09-20 12:05:49 -0700408 return;
409 }
410
Michael Jurkabdb5c532011-02-01 15:05:06 -0800411 final Drawable background = mBackground;
412 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700413 final int scrollX = getScrollX();
414 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800415
Michael Jurkabdb5c532011-02-01 15:05:06 -0800416 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700417 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800418 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800420
421 if ((scrollX | scrollY) == 0) {
422 background.draw(canvas);
423 } else {
424 canvas.translate(scrollX, scrollY);
425 background.draw(canvas);
426 canvas.translate(-scrollX, -scrollY);
427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800429
430 // If text is transparent, don't draw any shadow
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800431 if ((getCurrentTextColor() >> 24) == 0) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800432 getPaint().clearShadowLayer();
433 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800434 drawBadgeIfNecessary(canvas);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800435 return;
436 }
437
Michael Jurkabdb5c532011-02-01 15:05:06 -0800438 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700439 float density = getResources().getDisplayMetrics().density;
Mario Bertschlera6936942017-05-31 14:48:19 -0700440 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, mAmbientShadowColor);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800441 super.draw(canvas);
442 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700443 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
444 getScrollX() + getWidth(),
445 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700446 getPaint().setShadowLayer(
Mario Bertschlera6936942017-05-31 14:48:19 -0700447 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, mKeyShadowColor);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800448 super.draw(canvas);
449 canvas.restore();
Tony Wickham1237df02017-02-24 08:59:36 -0800450
451 drawBadgeIfNecessary(canvas);
452 }
453
454 /**
455 * Draws the icon badge in the top right corner of the icon bounds.
456 * @param canvas The canvas to draw to.
457 */
458 private void drawBadgeIfNecessary(Canvas canvas) {
459 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
460 getIconBounds(mTempIconBounds);
461 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
462 final int scrollX = getScrollX();
463 final int scrollY = getScrollY();
464 canvas.translate(scrollX, scrollY);
Tony Wickham7092db02017-06-07 14:32:23 -0700465 mBadgeRenderer.draw(canvas, mBadgePalette, mBadgeInfo, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800466 mTempSpaceForBadgeOffset);
467 canvas.translate(-scrollX, -scrollY);
468 }
469 }
470
471 public void forceHideBadge(boolean forceHideBadge) {
472 if (mForceHideBadge == forceHideBadge) {
473 return;
474 }
475 mForceHideBadge = forceHideBadge;
476
477 if (forceHideBadge) {
478 invalidate();
479 } else if (hasBadge()) {
480 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
481 }
482 }
483
484 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700485 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800486 }
487
488 public void getIconBounds(Rect outBounds) {
489 int top = getPaddingTop();
490 int left = (getWidth() - mIconSize) / 2;
491 int right = left + mIconSize;
492 int bottom = top + mIconSize;
493 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400495
496 @Override
497 protected void onAttachedToWindow() {
498 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700499
Winson Chung656d11c2010-11-29 17:15:47 -0800500 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400501 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400502 }
503
504 @Override
Winson1f064272016-07-18 17:18:02 -0700505 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
506 if (mCenterVertically) {
507 Paint.FontMetrics fm = getPaint().getFontMetrics();
508 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
509 (int) Math.ceil(fm.bottom - fm.top);
510 int height = MeasureSpec.getSize(heightMeasureSpec);
511 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
512 getPaddingBottom());
513 }
514 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
515 }
516
517 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400518 protected void onDetachedFromWindow() {
519 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800520 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400521 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700522
Adam Cohen477828c2013-09-20 12:05:49 -0700523 @Override
524 public void setTextColor(int color) {
525 mTextColor = color;
526 super.setTextColor(color);
527 }
528
Adam Cohen96bb7982014-07-07 11:58:56 -0700529 @Override
530 public void setTextColor(ColorStateList colors) {
531 mTextColor = colors.getDefaultColor();
532 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700533 }
534
Winson Chung5f8afe62013-08-12 16:19:28 -0700535 public void setTextVisibility(boolean visible) {
536 Resources res = getResources();
537 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700538 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700539 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700540 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700541 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700542 }
543
Winson Chungaffd7b42010-08-20 15:11:56 -0700544 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800545 public void cancelLongPress() {
546 super.cancelLongPress();
547
548 mLongPressHelper.cancelLongPress();
549 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500550
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800551 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700552 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400553 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700554 final boolean isPromise = info.isPromise();
555 final int progressLevel = isPromise ?
556 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
557 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700558
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700559 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
560 if (preloadDrawable != null && promiseStateChanged) {
561 preloadDrawable.maybePerformFinishedAnimation();
562 }
563 }
564 }
565
566 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
567 if (getTag() instanceof ItemInfoWithIcon) {
568 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
569 setContentDescription(progressLevel > 0
570 ? getContext().getString(R.string.app_downloading_title, info.title,
571 NumberFormat.getPercentInstance().format(progressLevel * 0.01))
572 : getContext().getString(R.string.app_waiting_download_title, info.title));
Sunny Goyalc469aad2015-10-01 11:24:23 -0700573
Winson Chungb745afb2015-03-02 11:51:23 -0800574 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700575 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800576 if (mIcon instanceof PreloadIconDrawable) {
577 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700578 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700579 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800580 preloadDrawable = DrawableFactory.get(getContext())
581 .newPendingIcon(info.iconBitmap, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700582 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700583 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700584 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700585 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700586 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400587 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700588 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400589 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700590
Tony Wickham1e618492017-02-02 12:57:18 -0800591 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800592 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800593 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700594 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
595 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800596 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700597 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800598 if (wasBadged || isBadged) {
Tony Wickham7092db02017-06-07 14:32:23 -0700599 mBadgePalette = IconPalette.getBadgePalette(getResources());
600 if (mBadgePalette == null) {
601 mBadgePalette = ((FastBitmapDrawable) mIcon).getIconPalette();
602 }
Tony Wickham1237df02017-02-24 08:59:36 -0800603 // Animate when a badge is first added or when it is removed.
604 if (animate && (wasBadged ^ isBadged) && isShown()) {
605 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
606 } else {
607 mBadgeScale = newBadgeScale;
608 invalidate();
609 }
610 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800611 }
612 }
613
Tony Wickham7092db02017-06-07 14:32:23 -0700614 public IconPalette getBadgePalette() {
615 return mBadgePalette;
616 }
617
Winson Chungb745afb2015-03-02 11:51:23 -0800618 /**
619 * Sets the icon for this view based on the layout direction.
620 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700621 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800622 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800623 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700624 if (mIsIconVisible) {
625 applyCompoundDrawables(mIcon);
626 }
627 }
628
629 public void setIconVisible(boolean visible) {
630 mIsIconVisible = visible;
631 mDisableRelayout = true;
632 Drawable icon = mIcon;
633 if (!visible) {
634 icon = new ColorDrawable(Color.TRANSPARENT);
635 icon.setBounds(0, 0, mIconSize, mIconSize);
636 }
637 applyCompoundDrawables(icon);
638 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700639 }
640
641 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800642 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800643 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800644 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700645 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800646 }
Winson Chungb745afb2015-03-02 11:51:23 -0800647 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700648
Sunny Goyal69b75642015-05-15 17:00:24 -0700649 @Override
650 public void requestLayout() {
651 if (!mDisableRelayout) {
652 super.requestLayout();
653 }
654 }
655
Sunny Goyal34b65272015-03-11 16:56:52 -0700656 /**
657 * Applies the item info if it is same as what the view is pointing to currently.
658 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800659 @Override
660 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700661 if (getTag() == info) {
662 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700663 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700664
Sunny Goyal34b65272015-03-11 16:56:52 -0700665 if (info instanceof AppInfo) {
666 applyFromApplicationInfo((AppInfo) info);
667 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800668 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800669 FolderIconPreviewVerifier verifier =
670 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
671 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700672 View folderIcon =
673 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
674 if (folderIcon != null) {
675 folderIcon.invalidate();
676 }
677 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700678 } else if (info instanceof PackageItemInfo) {
679 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700680 }
Winsonc0880492015-08-21 11:16:27 -0700681
Sunny Goyal69b75642015-05-15 17:00:24 -0700682 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700683 }
684 }
685
686 /**
687 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
688 */
689 public void verifyHighRes() {
690 if (mIconLoadRequest != null) {
691 mIconLoadRequest.cancel();
692 mIconLoadRequest = null;
693 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800694 if (getTag() instanceof ItemInfoWithIcon) {
695 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700696 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800697 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700698 .updateIconInBackground(BubbleTextView.this, info);
699 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700700 }
701 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700702
Jon Miranda47170112017-03-03 14:57:14 -0800703 public int getIconSize() {
704 return mIconSize;
705 }
706
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700707 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700708 * Interface to be implemented by the grand parent to allow click shadow effect.
709 */
Winsonc0880492015-08-21 11:16:27 -0700710 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700711 void setPressedIcon(BubbleTextView icon, Bitmap background);
712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713}