blob: ea1ee9e6d31189f217265d75c0dff5178b1d7d17 [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;
Mario Bertschlera6936942017-05-31 14:48:19 -070054import com.android.launcher3.util.Themes;
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;
103 private IconPalette mIconPalette;
104 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
Sunny Goyal4ffec482016-02-09 11:28:52 -0800123 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800124 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800125 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700126 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800127 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700128 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500129
Sunny Goyal34b65272015-03-11 16:56:52 -0700130 private IconLoadRequest mIconLoadRequest;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700133 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 }
135
136 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700137 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 }
139
140 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
141 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700142 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700143 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700144
Adam Cohen96bb7982014-07-07 11:58:56 -0700145 TypedArray a = context.obtainStyledAttributes(attrs,
146 R.styleable.BubbleTextView, defStyle, 0);
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800147 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, false);
Winson Chungb745afb2015-03-02 11:51:23 -0800148 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700149 mDeferShadowGenerationOnTouch =
150 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Mario Bertschlera6936942017-05-31 14:48:19 -0700151 mAmbientShadowColor = a.getColor(R.styleable.BubbleTextView_ambientShadowColor, 0x33000000);
152 mKeyShadowColor = a.getColor(R.styleable.BubbleTextView_keyShadowColor, 0x66000000);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700153
154 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
155 int defaultIconSize = grid.iconSizePx;
156 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700157 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700158 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700159 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
160 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700161 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700162 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700163 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700164 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700165 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700166 }
Winson1f064272016-07-18 17:18:02 -0700167 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700168
169 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
170 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700171 a.recycle();
172
Sunny Goyal15872da2014-07-08 15:43:54 -0700173 if (mCustomShadowsEnabled) {
174 // Draw the background itself as the parent is drawn twice.
175 mBackground = getBackground();
176 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700177
178 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
179 float density = getResources().getDisplayMetrics().density;
Mario Bertschlera6936942017-05-31 14:48:19 -0700180 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, mAmbientShadowColor);
Sunny Goyal15872da2014-07-08 15:43:54 -0700181 } else {
182 mBackground = null;
183 }
Winson Chungb745afb2015-03-02 11:51:23 -0800184
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800185 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700186 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800187
Sunny Goyal10629b02016-09-01 12:50:11 -0700188 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700189 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800192 public void applyFromShortcutInfo(ShortcutInfo info) {
193 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700194 }
195
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800196 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
197 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800198 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700199 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800200 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500201 }
Tony Wickham010d2552017-01-20 08:15:28 -0800202
Tony Wickham1e618492017-02-02 12:57:18 -0800203 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800204 }
205
Sunny Goyal508da152014-08-14 10:53:27 -0700206 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700207 applyIconAndLabel(info.iconBitmap, info);
208
Winson Chung888b3a12015-03-13 11:14:16 -0700209 // We don't need to check the info since it's not a ShortcutInfo
210 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700211
212 // Verify high res immediately
213 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800214
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700215 if (info instanceof PromiseAppInfo) {
216 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
217 applyProgressLevel(promiseAppInfo.level);
218 }
Tony Wickham1e618492017-02-02 12:57:18 -0800219 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700220 }
221
Sunny Goyal0e08f162015-05-12 11:32:39 -0700222 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700223 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700224 // We don't need to check the info since it's not a ShortcutInfo
225 super.setTag(info);
226
227 // Verify high res immediately
228 verifyHighRes();
229 }
230
Sunny Goyalf4204382016-07-13 10:46:07 -0700231 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800232 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800233 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700234 setIcon(iconDrawable);
235 setText(info.title);
236 if (info.contentDescription != null) {
237 setContentDescription(info.isDisabled()
238 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
239 : info.contentDescription);
240 }
241 }
242
Winson Chung7501adf2015-06-02 11:24:28 -0700243 /**
244 * Overrides the default long press timeout.
245 */
246 public void setLongPressTimeout(int longPressTimeout) {
247 mLongPressHelper.setLongPressTimeout(longPressTimeout);
248 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 @Override
251 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700252 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 mBackgroundSizeChanged = true;
254 }
255 return super.setFrame(left, top, right, bottom);
256 }
257
258 @Override
259 protected boolean verifyDrawable(Drawable who) {
260 return who == mBackground || super.verifyDrawable(who);
261 }
262
263 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700264 public void setTag(Object tag) {
265 if (tag != null) {
266 LauncherModel.checkItemInfo((ItemInfo) tag);
267 }
268 super.setTag(tag);
269 }
270
271 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800272 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700273 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800274 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800275 }
Sunny Goyal508da152014-08-14 10:53:27 -0700276 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800277
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800278 @Override
279 protected int[] onCreateDrawableState(int extraSpace) {
280 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
281 if (mStayPressed) {
282 mergeDrawableStates(drawableState, STATE_PRESSED);
283 }
284 return drawableState;
285 }
286
Winson Chungb745afb2015-03-02 11:51:23 -0800287 /** Returns the icon for this view. */
288 public Drawable getIcon() {
289 return mIcon;
290 }
291
292 /** Returns whether the layout is horizontal. */
293 public boolean isLayoutHorizontal() {
294 return mLayoutHorizontal;
295 }
296
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800297 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700298 public void setOnLongClickListener(OnLongClickListener l) {
299 super.setOnLongClickListener(l);
300 mOnLongClickListener = l;
301 }
302
303 public OnLongClickListener getOnLongClickListener() {
304 return mOnLongClickListener;
305 }
306
307 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800308 public boolean onTouchEvent(MotionEvent event) {
309 // Call the superclass onTouchEvent first, because sometimes it changes the state to
310 // isPressed() on an ACTION_UP
311 boolean result = super.onTouchEvent(event);
312
Mady Melloref044dd2015-06-02 15:35:07 -0700313 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700314 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700315 mLongPressHelper.cancelLongPress();
316 result = true;
317 }
318
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800319 switch (event.getAction()) {
320 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700321 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800322 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
323 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700324 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700325 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800326 }
Winson Chung88f33452012-02-23 15:23:44 -0800327
Mady Melloref044dd2015-06-02 15:35:07 -0700328 // If we're in a stylus button press, don't check for long press.
329 if (!mStylusEventHelper.inStylusButtonPressed()) {
330 mLongPressHelper.postCheckForLongPress();
331 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800332 break;
333 case MotionEvent.ACTION_CANCEL:
334 case MotionEvent.ACTION_UP:
335 // If we've touched down and up on an item, and it's still not "pressed", then
336 // destroy the pressed outline
337 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700338 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800339 }
Winson Chung88f33452012-02-23 15:23:44 -0800340
341 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800342 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400343 case MotionEvent.ACTION_MOVE:
344 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
345 mLongPressHelper.cancelLongPress();
346 }
347 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800348 }
349 return result;
350 }
351
Michael Jurkaddd62e92011-02-16 17:49:14 -0800352 void setStayPressed(boolean stayPressed) {
353 mStayPressed = stayPressed;
354 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700355 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700356 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700357 } else {
358 if (mPressedBackground == null) {
359 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
360 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800361 }
Sunny Goyal508da152014-08-14 10:53:27 -0700362
363 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700364 ViewParent parent = getParent();
365 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
366 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
367 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700368 }
369
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800370 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800371 }
372
Sunny Goyal508da152014-08-14 10:53:27 -0700373 void clearPressedBackground() {
374 setPressed(false);
375 setStayPressed(false);
376 }
377
378 @Override
379 public boolean onKeyDown(int keyCode, KeyEvent event) {
380 if (super.onKeyDown(keyCode, event)) {
381 // Pre-create shadow so show immediately on click.
382 if (mPressedBackground == null) {
383 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700384 }
Sunny Goyal508da152014-08-14 10:53:27 -0700385 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700386 }
Sunny Goyal508da152014-08-14 10:53:27 -0700387 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800388 }
389
Sunny Goyal508da152014-08-14 10:53:27 -0700390 @Override
391 public boolean onKeyUp(int keyCode, KeyEvent event) {
392 // Unlike touch events, keypress event propagate pressed state change immediately,
393 // without waiting for onClickHandler to execute. Disable pressed state changes here
394 // to avoid flickering.
395 mIgnorePressedStateChange = true;
396 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700397
Sunny Goyal508da152014-08-14 10:53:27 -0700398 mPressedBackground = null;
399 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800400 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700401 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800402 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 @Override
405 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700406 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700407 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800408 drawBadgeIfNecessary(canvas);
Adam Cohen477828c2013-09-20 12:05:49 -0700409 return;
410 }
411
Michael Jurkabdb5c532011-02-01 15:05:06 -0800412 final Drawable background = mBackground;
413 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700414 final int scrollX = getScrollX();
415 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800416
Michael Jurkabdb5c532011-02-01 15:05:06 -0800417 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700418 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800419 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800421
422 if ((scrollX | scrollY) == 0) {
423 background.draw(canvas);
424 } else {
425 canvas.translate(scrollX, scrollY);
426 background.draw(canvas);
427 canvas.translate(-scrollX, -scrollY);
428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800430
431 // If text is transparent, don't draw any shadow
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800432 if ((getCurrentTextColor() >> 24) == 0) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800433 getPaint().clearShadowLayer();
434 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800435 drawBadgeIfNecessary(canvas);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800436 return;
437 }
438
Michael Jurkabdb5c532011-02-01 15:05:06 -0800439 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700440 float density = getResources().getDisplayMetrics().density;
Mario Bertschlera6936942017-05-31 14:48:19 -0700441 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, mAmbientShadowColor);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800442 super.draw(canvas);
443 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700444 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
445 getScrollX() + getWidth(),
446 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700447 getPaint().setShadowLayer(
Mario Bertschlera6936942017-05-31 14:48:19 -0700448 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, mKeyShadowColor);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800449 super.draw(canvas);
450 canvas.restore();
Tony Wickham1237df02017-02-24 08:59:36 -0800451
452 drawBadgeIfNecessary(canvas);
453 }
454
455 /**
456 * Draws the icon badge in the top right corner of the icon bounds.
457 * @param canvas The canvas to draw to.
458 */
459 private void drawBadgeIfNecessary(Canvas canvas) {
460 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
461 getIconBounds(mTempIconBounds);
462 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
463 final int scrollX = getScrollX();
464 final int scrollY = getScrollY();
465 canvas.translate(scrollX, scrollY);
Tony Wickhame6a790e2017-05-16 12:07:38 -0700466 mBadgeRenderer.draw(canvas, mBadgeInfo, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800467 mTempSpaceForBadgeOffset);
468 canvas.translate(-scrollX, -scrollY);
469 }
470 }
471
472 public void forceHideBadge(boolean forceHideBadge) {
473 if (mForceHideBadge == forceHideBadge) {
474 return;
475 }
476 mForceHideBadge = forceHideBadge;
477
478 if (forceHideBadge) {
479 invalidate();
480 } else if (hasBadge()) {
481 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
482 }
483 }
484
485 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700486 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800487 }
488
489 public void getIconBounds(Rect outBounds) {
490 int top = getPaddingTop();
491 int left = (getWidth() - mIconSize) / 2;
492 int right = left + mIconSize;
493 int bottom = top + mIconSize;
494 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400496
497 @Override
498 protected void onAttachedToWindow() {
499 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700500
Winson Chung656d11c2010-11-29 17:15:47 -0800501 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400502 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400503 }
504
505 @Override
Winson1f064272016-07-18 17:18:02 -0700506 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
507 if (mCenterVertically) {
508 Paint.FontMetrics fm = getPaint().getFontMetrics();
509 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
510 (int) Math.ceil(fm.bottom - fm.top);
511 int height = MeasureSpec.getSize(heightMeasureSpec);
512 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
513 getPaddingBottom());
514 }
515 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
516 }
517
518 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400519 protected void onDetachedFromWindow() {
520 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800521 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400522 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700523
Adam Cohen477828c2013-09-20 12:05:49 -0700524 @Override
525 public void setTextColor(int color) {
526 mTextColor = color;
527 super.setTextColor(color);
528 }
529
Adam Cohen96bb7982014-07-07 11:58:56 -0700530 @Override
531 public void setTextColor(ColorStateList colors) {
532 mTextColor = colors.getDefaultColor();
533 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700534 }
535
Winson Chung5f8afe62013-08-12 16:19:28 -0700536 public void setTextVisibility(boolean visible) {
537 Resources res = getResources();
538 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700539 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700540 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700541 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700542 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700543 }
544
Winson Chungaffd7b42010-08-20 15:11:56 -0700545 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800546 public void cancelLongPress() {
547 super.cancelLongPress();
548
549 mLongPressHelper.cancelLongPress();
550 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500551
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800552 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700553 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400554 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700555 final boolean isPromise = info.isPromise();
556 final int progressLevel = isPromise ?
557 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
558 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700559
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700560 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
561 if (preloadDrawable != null && promiseStateChanged) {
562 preloadDrawable.maybePerformFinishedAnimation();
563 }
564 }
565 }
566
567 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
568 if (getTag() instanceof ItemInfoWithIcon) {
569 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
570 setContentDescription(progressLevel > 0
571 ? getContext().getString(R.string.app_downloading_title, info.title,
572 NumberFormat.getPercentInstance().format(progressLevel * 0.01))
573 : getContext().getString(R.string.app_waiting_download_title, info.title));
Sunny Goyalc469aad2015-10-01 11:24:23 -0700574
Winson Chungb745afb2015-03-02 11:51:23 -0800575 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700576 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800577 if (mIcon instanceof PreloadIconDrawable) {
578 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700579 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700580 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800581 preloadDrawable = DrawableFactory.get(getContext())
582 .newPendingIcon(info.iconBitmap, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700583 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700584 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700585 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700586 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700587 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400588 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700589 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400590 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700591
Tony Wickham1e618492017-02-02 12:57:18 -0800592 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800593 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800594 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700595 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
596 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800597 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700598 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800599 if (wasBadged || isBadged) {
600 mIconPalette = ((FastBitmapDrawable) mIcon).getIconPalette();
601 // Animate when a badge is first added or when it is removed.
602 if (animate && (wasBadged ^ isBadged) && isShown()) {
603 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
604 } else {
605 mBadgeScale = newBadgeScale;
606 invalidate();
607 }
608 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800609 }
610 }
611
Winson Chungb745afb2015-03-02 11:51:23 -0800612 /**
613 * Sets the icon for this view based on the layout direction.
614 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700615 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800616 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800617 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700618 if (mIsIconVisible) {
619 applyCompoundDrawables(mIcon);
620 }
621 }
622
623 public void setIconVisible(boolean visible) {
624 mIsIconVisible = visible;
625 mDisableRelayout = true;
626 Drawable icon = mIcon;
627 if (!visible) {
628 icon = new ColorDrawable(Color.TRANSPARENT);
629 icon.setBounds(0, 0, mIconSize, mIconSize);
630 }
631 applyCompoundDrawables(icon);
632 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700633 }
634
635 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800636 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800637 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800638 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700639 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800640 }
Winson Chungb745afb2015-03-02 11:51:23 -0800641 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700642
Sunny Goyal69b75642015-05-15 17:00:24 -0700643 @Override
644 public void requestLayout() {
645 if (!mDisableRelayout) {
646 super.requestLayout();
647 }
648 }
649
Sunny Goyal34b65272015-03-11 16:56:52 -0700650 /**
651 * Applies the item info if it is same as what the view is pointing to currently.
652 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800653 @Override
654 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700655 if (getTag() == info) {
656 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700657 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700658
Sunny Goyal34b65272015-03-11 16:56:52 -0700659 if (info instanceof AppInfo) {
660 applyFromApplicationInfo((AppInfo) info);
661 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800662 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800663 FolderIconPreviewVerifier verifier =
664 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
665 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700666 View folderIcon =
667 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
668 if (folderIcon != null) {
669 folderIcon.invalidate();
670 }
671 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700672 } else if (info instanceof PackageItemInfo) {
673 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700674 }
Winsonc0880492015-08-21 11:16:27 -0700675
Sunny Goyal69b75642015-05-15 17:00:24 -0700676 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700677 }
678 }
679
680 /**
681 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
682 */
683 public void verifyHighRes() {
684 if (mIconLoadRequest != null) {
685 mIconLoadRequest.cancel();
686 mIconLoadRequest = null;
687 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800688 if (getTag() instanceof ItemInfoWithIcon) {
689 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700690 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800691 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700692 .updateIconInBackground(BubbleTextView.this, info);
693 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700694 }
695 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700696
Jon Miranda47170112017-03-03 14:57:14 -0800697 public int getIconSize() {
698 return mIconSize;
699 }
700
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700701 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700702 * Interface to be implemented by the grand parent to allow click shadow effect.
703 */
Winsonc0880492015-08-21 11:16:27 -0700704 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700705 void setPressedIcon(BubbleTextView icon, Bitmap background);
706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707}