blob: b6e73287f413c17cc828eaf71493465ae4a3bdd3 [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
Tony8f402802017-06-16 17:24:54 -070019import android.animation.Animator;
Tony Wickham1237df02017-02-24 08:59:36 -080020import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070022import android.content.res.ColorStateList;
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;
Tony8f402802017-06-16 17:24:54 -070033import android.support.v4.graphics.ColorUtils;
Winson Chung656d11c2010-11-29 17:15:47 -080034import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080035import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070036import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070037import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080038import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070039import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040040import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080041import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070042import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080043import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070044
Sunny Goyal34b65272015-03-11 16:56:52 -070045import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080046import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080047import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080048import com.android.launcher3.badge.BadgeRenderer;
Jon Miranda529af302017-02-28 14:18:54 -080049import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080050import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070051import com.android.launcher3.graphics.HolographicOutlineHelper;
Tony Wickham1237df02017-02-24 08:59:36 -080052import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080053import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070054import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070055
Sunny Goyalc469aad2015-10-01 11:24:23 -070056import java.text.NumberFormat;
57
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058/**
59 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
60 * because we want to make the bubble taller than the text and TextView's clip is
61 * too aggressive.
62 */
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080063public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070064
Sunny Goyal8f8f3982016-07-25 17:08:38 -070065 // Dimensions in DP
66 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
67 private static final float KEY_SHADOW_RADIUS = 1f;
68 private static final float KEY_SHADOW_OFFSET = 0.5f;
Winson Chung656d11c2010-11-29 17:15:47 -080069
Sunny Goyaldfaccf62015-05-11 16:30:44 -070070 private static final int DISPLAY_WORKSPACE = 0;
71 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070072 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070073
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080074 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
75
Sunny Goyal53d7ee42015-05-22 12:25:45 -070076 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080077 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070078 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070080 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080081 private final CheckLongPressHelper mLongPressHelper;
82 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070083 private final StylusEventHelper mStylusEventHelper;
Mario Bertschlera6936942017-05-31 14:48:19 -070084 private final int mAmbientShadowColor;
85 private final int mKeyShadowColor;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080086
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080087 private boolean mBackgroundSizeChanged;
88
Sunny Goyal508da152014-08-14 10:53:27 -070089 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080090
Jason Monk02dd7ae2014-04-15 15:23:31 -040091 private float mSlop;
92
Winson Chung93f98ea2015-03-10 16:28:47 -070093 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070094 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080095 private final boolean mLayoutHorizontal;
96 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080098 private int mTextColor;
Tony17b7f9b2017-05-23 12:19:09 -070099 private boolean mIsIconVisible = true;
Winson Chung5f8afe62013-08-12 16:19:28 -0700100
Tony Wickham1237df02017-02-24 08:59:36 -0800101 private BadgeInfo mBadgeInfo;
102 private BadgeRenderer mBadgeRenderer;
Tony Wickham7092db02017-06-07 14:32:23 -0700103 private IconPalette mBadgePalette;
Tony Wickham1237df02017-02-24 08:59:36 -0800104 private float mBadgeScale;
105 private boolean mForceHideBadge;
106 private Point mTempSpaceForBadgeOffset = new Point();
107 private Rect mTempIconBounds = new Rect();
108
109 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
110 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
111 @Override
112 public Float get(BubbleTextView bubbleTextView) {
113 return bubbleTextView.mBadgeScale;
114 }
115
116 @Override
117 public void set(BubbleTextView bubbleTextView, Float value) {
118 bubbleTextView.mBadgeScale = value;
119 bubbleTextView.invalidate();
120 }
121 };
122
Tony8f402802017-06-16 17:24:54 -0700123 private static final Property<BubbleTextView, Integer> TEXT_ALPHA_PROPERTY
124 = new Property<BubbleTextView, Integer>(Integer.class, "textAlpha") {
125 @Override
126 public Integer get(BubbleTextView bubbleTextView) {
127 return bubbleTextView.getTextAlpha();
128 }
129
130 @Override
131 public void set(BubbleTextView bubbleTextView, Integer alpha) {
132 bubbleTextView.setTextAlpha(alpha);
133 }
134 };
135
Sunny Goyal4ffec482016-02-09 11:28:52 -0800136 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800137 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800138 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700139 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800140 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700141 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500142
Sunny Goyal34b65272015-03-11 16:56:52 -0700143 private IconLoadRequest mIconLoadRequest;
144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700146 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 }
148
149 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700150 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
153 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
154 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700155 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700156 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700157
Adam Cohen96bb7982014-07-07 11:58:56 -0700158 TypedArray a = context.obtainStyledAttributes(attrs,
159 R.styleable.BubbleTextView, defStyle, 0);
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800160 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, false);
Winson Chungb745afb2015-03-02 11:51:23 -0800161 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700162 mDeferShadowGenerationOnTouch =
163 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Mario Bertschlera6936942017-05-31 14:48:19 -0700164 mAmbientShadowColor = a.getColor(R.styleable.BubbleTextView_ambientShadowColor, 0x33000000);
165 mKeyShadowColor = a.getColor(R.styleable.BubbleTextView_keyShadowColor, 0x66000000);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700166
167 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
168 int defaultIconSize = grid.iconSizePx;
169 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700170 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700171 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700172 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
173 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700174 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700175 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700176 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700177 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700178 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700179 }
Winson1f064272016-07-18 17:18:02 -0700180 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700181
182 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
183 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700184 a.recycle();
185
Sunny Goyal15872da2014-07-08 15:43:54 -0700186 if (mCustomShadowsEnabled) {
187 // Draw the background itself as the parent is drawn twice.
188 mBackground = getBackground();
189 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700190
191 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
192 float density = getResources().getDisplayMetrics().density;
Mario Bertschlera6936942017-05-31 14:48:19 -0700193 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, mAmbientShadowColor);
Sunny Goyal15872da2014-07-08 15:43:54 -0700194 } else {
195 mBackground = null;
196 }
Winson Chungb745afb2015-03-02 11:51:23 -0800197
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800198 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700199 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800200
Sunny Goyal10629b02016-09-01 12:50:11 -0700201 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700202 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 }
204
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800205 public void applyFromShortcutInfo(ShortcutInfo info) {
206 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700207 }
208
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800209 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
210 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800211 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700212 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800213 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500214 }
Tony Wickham010d2552017-01-20 08:15:28 -0800215
Tony Wickham1e618492017-02-02 12:57:18 -0800216 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800217 }
218
Sunny Goyal508da152014-08-14 10:53:27 -0700219 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700220 applyIconAndLabel(info.iconBitmap, info);
221
Winson Chung888b3a12015-03-13 11:14:16 -0700222 // We don't need to check the info since it's not a ShortcutInfo
223 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700224
225 // Verify high res immediately
226 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800227
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700228 if (info instanceof PromiseAppInfo) {
229 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
230 applyProgressLevel(promiseAppInfo.level);
231 }
Tony Wickham1e618492017-02-02 12:57:18 -0800232 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700233 }
234
Sunny Goyal0e08f162015-05-12 11:32:39 -0700235 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700236 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700237 // We don't need to check the info since it's not a ShortcutInfo
238 super.setTag(info);
239
240 // Verify high res immediately
241 verifyHighRes();
242 }
243
Sunny Goyalf4204382016-07-13 10:46:07 -0700244 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800245 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800246 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700247 setIcon(iconDrawable);
248 setText(info.title);
249 if (info.contentDescription != null) {
250 setContentDescription(info.isDisabled()
251 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
252 : info.contentDescription);
253 }
254 }
255
Winson Chung7501adf2015-06-02 11:24:28 -0700256 /**
257 * Overrides the default long press timeout.
258 */
259 public void setLongPressTimeout(int longPressTimeout) {
260 mLongPressHelper.setLongPressTimeout(longPressTimeout);
261 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 @Override
264 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700265 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 mBackgroundSizeChanged = true;
267 }
268 return super.setFrame(left, top, right, bottom);
269 }
270
271 @Override
272 protected boolean verifyDrawable(Drawable who) {
273 return who == mBackground || super.verifyDrawable(who);
274 }
275
276 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700277 public void setTag(Object tag) {
278 if (tag != null) {
279 LauncherModel.checkItemInfo((ItemInfo) tag);
280 }
281 super.setTag(tag);
282 }
283
284 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800285 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700286 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800287 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800288 }
Sunny Goyal508da152014-08-14 10:53:27 -0700289 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800290
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800291 @Override
292 protected int[] onCreateDrawableState(int extraSpace) {
293 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
294 if (mStayPressed) {
295 mergeDrawableStates(drawableState, STATE_PRESSED);
296 }
297 return drawableState;
298 }
299
Winson Chungb745afb2015-03-02 11:51:23 -0800300 /** Returns the icon for this view. */
301 public Drawable getIcon() {
302 return mIcon;
303 }
304
305 /** Returns whether the layout is horizontal. */
306 public boolean isLayoutHorizontal() {
307 return mLayoutHorizontal;
308 }
309
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800310 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700311 public void setOnLongClickListener(OnLongClickListener l) {
312 super.setOnLongClickListener(l);
313 mOnLongClickListener = l;
314 }
315
316 public OnLongClickListener getOnLongClickListener() {
317 return mOnLongClickListener;
318 }
319
320 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800321 public boolean onTouchEvent(MotionEvent event) {
322 // Call the superclass onTouchEvent first, because sometimes it changes the state to
323 // isPressed() on an ACTION_UP
324 boolean result = super.onTouchEvent(event);
325
Mady Melloref044dd2015-06-02 15:35:07 -0700326 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700327 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700328 mLongPressHelper.cancelLongPress();
329 result = true;
330 }
331
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800332 switch (event.getAction()) {
333 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700334 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800335 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
336 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700337 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700338 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800339 }
Winson Chung88f33452012-02-23 15:23:44 -0800340
Mady Melloref044dd2015-06-02 15:35:07 -0700341 // If we're in a stylus button press, don't check for long press.
342 if (!mStylusEventHelper.inStylusButtonPressed()) {
343 mLongPressHelper.postCheckForLongPress();
344 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800345 break;
346 case MotionEvent.ACTION_CANCEL:
347 case MotionEvent.ACTION_UP:
348 // If we've touched down and up on an item, and it's still not "pressed", then
349 // destroy the pressed outline
350 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700351 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800352 }
Winson Chung88f33452012-02-23 15:23:44 -0800353
354 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800355 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400356 case MotionEvent.ACTION_MOVE:
357 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
358 mLongPressHelper.cancelLongPress();
359 }
360 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800361 }
362 return result;
363 }
364
Michael Jurkaddd62e92011-02-16 17:49:14 -0800365 void setStayPressed(boolean stayPressed) {
366 mStayPressed = stayPressed;
367 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700368 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700369 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700370 } else {
371 if (mPressedBackground == null) {
372 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
373 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800374 }
Sunny Goyal508da152014-08-14 10:53:27 -0700375
376 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700377 ViewParent parent = getParent();
378 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
379 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
380 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700381 }
382
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800383 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800384 }
385
Sunny Goyal508da152014-08-14 10:53:27 -0700386 void clearPressedBackground() {
387 setPressed(false);
388 setStayPressed(false);
389 }
390
391 @Override
392 public boolean onKeyDown(int keyCode, KeyEvent event) {
393 if (super.onKeyDown(keyCode, event)) {
394 // Pre-create shadow so show immediately on click.
395 if (mPressedBackground == null) {
396 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700397 }
Sunny Goyal508da152014-08-14 10:53:27 -0700398 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700399 }
Sunny Goyal508da152014-08-14 10:53:27 -0700400 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800401 }
402
Sunny Goyal508da152014-08-14 10:53:27 -0700403 @Override
404 public boolean onKeyUp(int keyCode, KeyEvent event) {
405 // Unlike touch events, keypress event propagate pressed state change immediately,
406 // without waiting for onClickHandler to execute. Disable pressed state changes here
407 // to avoid flickering.
408 mIgnorePressedStateChange = true;
409 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700410
Sunny Goyal508da152014-08-14 10:53:27 -0700411 mPressedBackground = null;
412 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800413 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700414 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800415 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 @Override
418 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700419 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700420 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800421 drawBadgeIfNecessary(canvas);
Adam Cohen477828c2013-09-20 12:05:49 -0700422 return;
423 }
424
Michael Jurkabdb5c532011-02-01 15:05:06 -0800425 final Drawable background = mBackground;
426 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700427 final int scrollX = getScrollX();
428 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800429
Michael Jurkabdb5c532011-02-01 15:05:06 -0800430 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700431 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800432 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800434
435 if ((scrollX | scrollY) == 0) {
436 background.draw(canvas);
437 } else {
438 canvas.translate(scrollX, scrollY);
439 background.draw(canvas);
440 canvas.translate(-scrollX, -scrollY);
441 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800443
444 // If text is transparent, don't draw any shadow
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800445 if ((getCurrentTextColor() >> 24) == 0) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800446 getPaint().clearShadowLayer();
447 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800448 drawBadgeIfNecessary(canvas);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800449 return;
450 }
451
Michael Jurkabdb5c532011-02-01 15:05:06 -0800452 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700453 float density = getResources().getDisplayMetrics().density;
Mario Bertschlera6936942017-05-31 14:48:19 -0700454 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, mAmbientShadowColor);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800455 super.draw(canvas);
456 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700457 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
458 getScrollX() + getWidth(),
459 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700460 getPaint().setShadowLayer(
Mario Bertschlera6936942017-05-31 14:48:19 -0700461 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, mKeyShadowColor);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800462 super.draw(canvas);
463 canvas.restore();
Tony Wickham1237df02017-02-24 08:59:36 -0800464
465 drawBadgeIfNecessary(canvas);
466 }
467
468 /**
469 * Draws the icon badge in the top right corner of the icon bounds.
470 * @param canvas The canvas to draw to.
471 */
472 private void drawBadgeIfNecessary(Canvas canvas) {
473 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
474 getIconBounds(mTempIconBounds);
475 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
476 final int scrollX = getScrollX();
477 final int scrollY = getScrollY();
478 canvas.translate(scrollX, scrollY);
Tony Wickham7092db02017-06-07 14:32:23 -0700479 mBadgeRenderer.draw(canvas, mBadgePalette, mBadgeInfo, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800480 mTempSpaceForBadgeOffset);
481 canvas.translate(-scrollX, -scrollY);
482 }
483 }
484
485 public void forceHideBadge(boolean forceHideBadge) {
486 if (mForceHideBadge == forceHideBadge) {
487 return;
488 }
489 mForceHideBadge = forceHideBadge;
490
491 if (forceHideBadge) {
492 invalidate();
493 } else if (hasBadge()) {
494 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
495 }
496 }
497
498 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700499 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800500 }
501
502 public void getIconBounds(Rect outBounds) {
503 int top = getPaddingTop();
504 int left = (getWidth() - mIconSize) / 2;
505 int right = left + mIconSize;
506 int bottom = top + mIconSize;
507 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400509
510 @Override
511 protected void onAttachedToWindow() {
512 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700513
Winson Chung656d11c2010-11-29 17:15:47 -0800514 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400515 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 }
517
518 @Override
Winson1f064272016-07-18 17:18:02 -0700519 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
520 if (mCenterVertically) {
521 Paint.FontMetrics fm = getPaint().getFontMetrics();
522 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
523 (int) Math.ceil(fm.bottom - fm.top);
524 int height = MeasureSpec.getSize(heightMeasureSpec);
525 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
526 getPaddingBottom());
527 }
528 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
529 }
530
531 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400532 protected void onDetachedFromWindow() {
533 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800534 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700536
Adam Cohen477828c2013-09-20 12:05:49 -0700537 @Override
538 public void setTextColor(int color) {
539 mTextColor = color;
540 super.setTextColor(color);
541 }
542
Adam Cohen96bb7982014-07-07 11:58:56 -0700543 @Override
544 public void setTextColor(ColorStateList colors) {
545 mTextColor = colors.getDefaultColor();
546 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700547 }
548
Winson Chung5f8afe62013-08-12 16:19:28 -0700549 public void setTextVisibility(boolean visible) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700550 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700551 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700552 } else {
Tony8f402802017-06-16 17:24:54 -0700553 setTextAlpha(0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700554 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700555 }
556
Tony8f402802017-06-16 17:24:54 -0700557 private void setTextAlpha(int alpha) {
558 super.setTextColor(ColorUtils.setAlphaComponent(mTextColor, alpha));
559 }
560
561 private int getTextAlpha() {
562 return Color.alpha(getCurrentTextColor());
563 }
564
565 /**
566 * Creates an animator to fade the text in or out.
567 * @param fadeIn Whether the text should fade in or fade out.
568 */
569 public Animator createTextAlphaAnimator(boolean fadeIn) {
570 return ObjectAnimator.ofInt(this, TEXT_ALPHA_PROPERTY, fadeIn ? Color.alpha(mTextColor) : 0);
571 }
572
Winson Chungaffd7b42010-08-20 15:11:56 -0700573 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800574 public void cancelLongPress() {
575 super.cancelLongPress();
576
577 mLongPressHelper.cancelLongPress();
578 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500579
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800580 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700581 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400582 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700583 final boolean isPromise = info.isPromise();
584 final int progressLevel = isPromise ?
585 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
586 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700587
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700588 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
589 if (preloadDrawable != null && promiseStateChanged) {
590 preloadDrawable.maybePerformFinishedAnimation();
591 }
592 }
593 }
594
595 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
596 if (getTag() instanceof ItemInfoWithIcon) {
597 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
598 setContentDescription(progressLevel > 0
599 ? getContext().getString(R.string.app_downloading_title, info.title,
600 NumberFormat.getPercentInstance().format(progressLevel * 0.01))
601 : getContext().getString(R.string.app_waiting_download_title, info.title));
Sunny Goyalc469aad2015-10-01 11:24:23 -0700602
Winson Chungb745afb2015-03-02 11:51:23 -0800603 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700604 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800605 if (mIcon instanceof PreloadIconDrawable) {
606 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700607 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700608 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800609 preloadDrawable = DrawableFactory.get(getContext())
610 .newPendingIcon(info.iconBitmap, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700611 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700612 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700613 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700614 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700615 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400616 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700617 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400618 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700619
Tony Wickham1e618492017-02-02 12:57:18 -0800620 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800621 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800622 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700623 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
624 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800625 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700626 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800627 if (wasBadged || isBadged) {
Tony Wickham7092db02017-06-07 14:32:23 -0700628 mBadgePalette = IconPalette.getBadgePalette(getResources());
629 if (mBadgePalette == null) {
630 mBadgePalette = ((FastBitmapDrawable) mIcon).getIconPalette();
631 }
Tony Wickham1237df02017-02-24 08:59:36 -0800632 // Animate when a badge is first added or when it is removed.
633 if (animate && (wasBadged ^ isBadged) && isShown()) {
634 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
635 } else {
636 mBadgeScale = newBadgeScale;
637 invalidate();
638 }
639 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800640 }
641 }
642
Tony Wickham7092db02017-06-07 14:32:23 -0700643 public IconPalette getBadgePalette() {
644 return mBadgePalette;
645 }
646
Winson Chungb745afb2015-03-02 11:51:23 -0800647 /**
648 * Sets the icon for this view based on the layout direction.
649 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700650 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800651 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800652 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700653 if (mIsIconVisible) {
654 applyCompoundDrawables(mIcon);
655 }
656 }
657
658 public void setIconVisible(boolean visible) {
659 mIsIconVisible = visible;
660 mDisableRelayout = true;
661 Drawable icon = mIcon;
662 if (!visible) {
663 icon = new ColorDrawable(Color.TRANSPARENT);
664 icon.setBounds(0, 0, mIconSize, mIconSize);
665 }
666 applyCompoundDrawables(icon);
667 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700668 }
669
670 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800671 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800672 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800673 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700674 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800675 }
Winson Chungb745afb2015-03-02 11:51:23 -0800676 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700677
Sunny Goyal69b75642015-05-15 17:00:24 -0700678 @Override
679 public void requestLayout() {
680 if (!mDisableRelayout) {
681 super.requestLayout();
682 }
683 }
684
Sunny Goyal34b65272015-03-11 16:56:52 -0700685 /**
686 * Applies the item info if it is same as what the view is pointing to currently.
687 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800688 @Override
689 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700690 if (getTag() == info) {
691 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700692 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700693
Sunny Goyal34b65272015-03-11 16:56:52 -0700694 if (info instanceof AppInfo) {
695 applyFromApplicationInfo((AppInfo) info);
696 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800697 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800698 FolderIconPreviewVerifier verifier =
699 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
700 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700701 View folderIcon =
702 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
703 if (folderIcon != null) {
704 folderIcon.invalidate();
705 }
706 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700707 } else if (info instanceof PackageItemInfo) {
708 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700709 }
Winsonc0880492015-08-21 11:16:27 -0700710
Sunny Goyal69b75642015-05-15 17:00:24 -0700711 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700712 }
713 }
714
715 /**
716 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
717 */
718 public void verifyHighRes() {
719 if (mIconLoadRequest != null) {
720 mIconLoadRequest.cancel();
721 mIconLoadRequest = null;
722 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800723 if (getTag() instanceof ItemInfoWithIcon) {
724 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700725 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800726 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700727 .updateIconInBackground(BubbleTextView.this, info);
728 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700729 }
730 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700731
Jon Miranda47170112017-03-03 14:57:14 -0800732 public int getIconSize() {
733 return mIconSize;
734 }
735
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700736 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700737 * Interface to be implemented by the grand parent to allow click shadow effect.
738 */
Winsonc0880492015-08-21 11:16:27 -0700739 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700740 void setPressedIcon(BubbleTextView icon, Bitmap background);
741 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742}