blob: 97e93a0087cfc0d48565d417daf190c2f85817d9 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Tony Wickham1237df02017-02-24 08:59:36 -080019import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070021import android.content.res.ColorStateList;
Winson Chung656d11c2010-11-29 17:15:47 -080022import android.content.res.Resources;
Adam Cohen96bb7982014-07-07 11:58:56 -070023import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080024import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Canvas;
Winson1f064272016-07-18 17:18:02 -070026import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080027import android.graphics.Point;
28import android.graphics.Rect;
Michael Jurka137142e2011-01-05 20:57:04 -080029import android.graphics.Region;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080031import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080032import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070033import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070034import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080035import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070036import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040037import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080038import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070039import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080040import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070041
Sunny Goyal34b65272015-03-11 16:56:52 -070042import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080043import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080044import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080045import com.android.launcher3.badge.BadgeRenderer;
Sunny Goyal26119432016-02-18 22:09:23 +000046import com.android.launcher3.folder.FolderIcon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080047import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070048import com.android.launcher3.graphics.HolographicOutlineHelper;
Tony Wickham1237df02017-02-24 08:59:36 -080049import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080050import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070051import com.android.launcher3.model.PackageItemInfo;
Tony Wickham9407d4a2017-02-24 15:08:13 -080052import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyal34b65272015-03-11 16:56:52 -070053
Sunny Goyalc469aad2015-10-01 11:24:23 -070054import java.text.NumberFormat;
55
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056/**
57 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
58 * because we want to make the bubble taller than the text and TextView's clip is
59 * too aggressive.
60 */
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080061public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070062
Sunny Goyal8f8f3982016-07-25 17:08:38 -070063 // Dimensions in DP
64 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
65 private static final float KEY_SHADOW_RADIUS = 1f;
66 private static final float KEY_SHADOW_OFFSET = 0.5f;
67 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
68 private static final int KEY_SHADOW_COLOR = 0x66000000;
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;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080084
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080085 private boolean mBackgroundSizeChanged;
86
Sunny Goyal508da152014-08-14 10:53:27 -070087 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080088
Jason Monk02dd7ae2014-04-15 15:23:31 -040089 private float mSlop;
90
Winson Chung93f98ea2015-03-10 16:28:47 -070091 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070092 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080093 private final boolean mLayoutHorizontal;
94 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080096 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070097
Tony Wickham1237df02017-02-24 08:59:36 -080098 private BadgeInfo mBadgeInfo;
99 private BadgeRenderer mBadgeRenderer;
100 private IconPalette mIconPalette;
101 private float mBadgeScale;
102 private boolean mForceHideBadge;
103 private Point mTempSpaceForBadgeOffset = new Point();
104 private Rect mTempIconBounds = new Rect();
105
106 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
107 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
108 @Override
109 public Float get(BubbleTextView bubbleTextView) {
110 return bubbleTextView.mBadgeScale;
111 }
112
113 @Override
114 public void set(BubbleTextView bubbleTextView, Float value) {
115 bubbleTextView.mBadgeScale = value;
116 bubbleTextView.invalidate();
117 }
118 };
119
Sunny Goyal4ffec482016-02-09 11:28:52 -0800120 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800121 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800122 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700123 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800124 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700125 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500126
Sunny Goyal34b65272015-03-11 16:56:52 -0700127 private IconLoadRequest mIconLoadRequest;
128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700130 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 }
132
133 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700134 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 }
136
137 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
138 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700139 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700140 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700141
Adam Cohen96bb7982014-07-07 11:58:56 -0700142 TypedArray a = context.obtainStyledAttributes(attrs,
143 R.styleable.BubbleTextView, defStyle, 0);
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800144 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, false);
Winson Chungb745afb2015-03-02 11:51:23 -0800145 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700146 mDeferShadowGenerationOnTouch =
147 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700148
149 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
150 int defaultIconSize = grid.iconSizePx;
151 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700152 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700153 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700154 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
155 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700156 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700157 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700158 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700159 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700160 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700161 }
Winson1f064272016-07-18 17:18:02 -0700162 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700163
164 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
165 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700166 a.recycle();
167
Sunny Goyal15872da2014-07-08 15:43:54 -0700168 if (mCustomShadowsEnabled) {
169 // Draw the background itself as the parent is drawn twice.
170 mBackground = getBackground();
171 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700172
173 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
174 float density = getResources().getDisplayMetrics().density;
175 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700176 } else {
177 mBackground = null;
178 }
Winson Chungb745afb2015-03-02 11:51:23 -0800179
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800180 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700181 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800182
Sunny Goyal10629b02016-09-01 12:50:11 -0700183 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700184 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 }
186
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800187 public void applyFromShortcutInfo(ShortcutInfo info) {
188 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700189 }
190
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800191 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
192 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800193 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700194 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800195 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500196 }
Tony Wickham010d2552017-01-20 08:15:28 -0800197
Tony Wickham1e618492017-02-02 12:57:18 -0800198 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800199 }
200
Sunny Goyal508da152014-08-14 10:53:27 -0700201 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700202 applyIconAndLabel(info.iconBitmap, info);
203
Winson Chung888b3a12015-03-13 11:14:16 -0700204 // We don't need to check the info since it's not a ShortcutInfo
205 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700206
207 // Verify high res immediately
208 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800209
Tony Wickham1e618492017-02-02 12:57:18 -0800210 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700211 }
212
Sunny Goyal0e08f162015-05-12 11:32:39 -0700213 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700214 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700215 // We don't need to check the info since it's not a ShortcutInfo
216 super.setTag(info);
217
218 // Verify high res immediately
219 verifyHighRes();
220 }
221
Sunny Goyalf4204382016-07-13 10:46:07 -0700222 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800223 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800224 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700225 setIcon(iconDrawable);
226 setText(info.title);
227 if (info.contentDescription != null) {
228 setContentDescription(info.isDisabled()
229 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
230 : info.contentDescription);
231 }
232 }
233
Winson Chung7501adf2015-06-02 11:24:28 -0700234 /**
235 * Overrides the default long press timeout.
236 */
237 public void setLongPressTimeout(int longPressTimeout) {
238 mLongPressHelper.setLongPressTimeout(longPressTimeout);
239 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 @Override
242 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700243 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 mBackgroundSizeChanged = true;
245 }
246 return super.setFrame(left, top, right, bottom);
247 }
248
249 @Override
250 protected boolean verifyDrawable(Drawable who) {
251 return who == mBackground || super.verifyDrawable(who);
252 }
253
254 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700255 public void setTag(Object tag) {
256 if (tag != null) {
257 LauncherModel.checkItemInfo((ItemInfo) tag);
258 }
259 super.setTag(tag);
260 }
261
262 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800263 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700264 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800265 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800266 }
Sunny Goyal508da152014-08-14 10:53:27 -0700267 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800268
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800269 @Override
270 protected int[] onCreateDrawableState(int extraSpace) {
271 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
272 if (mStayPressed) {
273 mergeDrawableStates(drawableState, STATE_PRESSED);
274 }
275 return drawableState;
276 }
277
Winson Chungb745afb2015-03-02 11:51:23 -0800278 /** Returns the icon for this view. */
279 public Drawable getIcon() {
280 return mIcon;
281 }
282
283 /** Returns whether the layout is horizontal. */
284 public boolean isLayoutHorizontal() {
285 return mLayoutHorizontal;
286 }
287
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800288 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700289 public void setOnLongClickListener(OnLongClickListener l) {
290 super.setOnLongClickListener(l);
291 mOnLongClickListener = l;
292 }
293
294 public OnLongClickListener getOnLongClickListener() {
295 return mOnLongClickListener;
296 }
297
298 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800299 public boolean onTouchEvent(MotionEvent event) {
300 // Call the superclass onTouchEvent first, because sometimes it changes the state to
301 // isPressed() on an ACTION_UP
302 boolean result = super.onTouchEvent(event);
303
Mady Melloref044dd2015-06-02 15:35:07 -0700304 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700305 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700306 mLongPressHelper.cancelLongPress();
307 result = true;
308 }
309
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800310 switch (event.getAction()) {
311 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700312 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800313 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
314 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700315 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700316 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800317 }
Winson Chung88f33452012-02-23 15:23:44 -0800318
Mady Melloref044dd2015-06-02 15:35:07 -0700319 // If we're in a stylus button press, don't check for long press.
320 if (!mStylusEventHelper.inStylusButtonPressed()) {
321 mLongPressHelper.postCheckForLongPress();
322 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800323 break;
324 case MotionEvent.ACTION_CANCEL:
325 case MotionEvent.ACTION_UP:
326 // If we've touched down and up on an item, and it's still not "pressed", then
327 // destroy the pressed outline
328 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700329 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800330 }
Winson Chung88f33452012-02-23 15:23:44 -0800331
332 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800333 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400334 case MotionEvent.ACTION_MOVE:
335 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
336 mLongPressHelper.cancelLongPress();
337 }
338 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800339 }
340 return result;
341 }
342
Michael Jurkaddd62e92011-02-16 17:49:14 -0800343 void setStayPressed(boolean stayPressed) {
344 mStayPressed = stayPressed;
345 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700346 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700347 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700348 } else {
349 if (mPressedBackground == null) {
350 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
351 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800352 }
Sunny Goyal508da152014-08-14 10:53:27 -0700353
354 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700355 ViewParent parent = getParent();
356 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
357 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
358 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700359 }
360
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800361 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800362 }
363
Sunny Goyal508da152014-08-14 10:53:27 -0700364 void clearPressedBackground() {
365 setPressed(false);
366 setStayPressed(false);
367 }
368
369 @Override
370 public boolean onKeyDown(int keyCode, KeyEvent event) {
371 if (super.onKeyDown(keyCode, event)) {
372 // Pre-create shadow so show immediately on click.
373 if (mPressedBackground == null) {
374 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700375 }
Sunny Goyal508da152014-08-14 10:53:27 -0700376 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700377 }
Sunny Goyal508da152014-08-14 10:53:27 -0700378 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800379 }
380
Sunny Goyal508da152014-08-14 10:53:27 -0700381 @Override
382 public boolean onKeyUp(int keyCode, KeyEvent event) {
383 // Unlike touch events, keypress event propagate pressed state change immediately,
384 // without waiting for onClickHandler to execute. Disable pressed state changes here
385 // to avoid flickering.
386 mIgnorePressedStateChange = true;
387 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700388
Sunny Goyal508da152014-08-14 10:53:27 -0700389 mPressedBackground = null;
390 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800391 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700392 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800393 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 @Override
396 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700397 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700398 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800399 drawBadgeIfNecessary(canvas);
Adam Cohen477828c2013-09-20 12:05:49 -0700400 return;
401 }
402
Michael Jurkabdb5c532011-02-01 15:05:06 -0800403 final Drawable background = mBackground;
404 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700405 final int scrollX = getScrollX();
406 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800407
Michael Jurkabdb5c532011-02-01 15:05:06 -0800408 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700409 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800410 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800412
413 if ((scrollX | scrollY) == 0) {
414 background.draw(canvas);
415 } else {
416 canvas.translate(scrollX, scrollY);
417 background.draw(canvas);
418 canvas.translate(-scrollX, -scrollY);
419 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800421
422 // If text is transparent, don't draw any shadow
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800423 if ((getCurrentTextColor() >> 24) == 0) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800424 getPaint().clearShadowLayer();
425 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800426 drawBadgeIfNecessary(canvas);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800427 return;
428 }
429
Michael Jurkabdb5c532011-02-01 15:05:06 -0800430 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700431 float density = getResources().getDisplayMetrics().density;
432 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800433 super.draw(canvas);
434 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700435 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
436 getScrollX() + getWidth(),
437 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700438 getPaint().setShadowLayer(
439 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800440 super.draw(canvas);
441 canvas.restore();
Tony Wickham1237df02017-02-24 08:59:36 -0800442
443 drawBadgeIfNecessary(canvas);
444 }
445
446 /**
447 * Draws the icon badge in the top right corner of the icon bounds.
448 * @param canvas The canvas to draw to.
449 */
450 private void drawBadgeIfNecessary(Canvas canvas) {
451 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
452 getIconBounds(mTempIconBounds);
453 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
454 final int scrollX = getScrollX();
455 final int scrollY = getScrollY();
456 canvas.translate(scrollX, scrollY);
457 mBadgeRenderer.draw(canvas, mIconPalette, mBadgeInfo, mTempIconBounds, mBadgeScale,
458 mTempSpaceForBadgeOffset);
459 canvas.translate(-scrollX, -scrollY);
460 }
461 }
462
463 public void forceHideBadge(boolean forceHideBadge) {
464 if (mForceHideBadge == forceHideBadge) {
465 return;
466 }
467 mForceHideBadge = forceHideBadge;
468
469 if (forceHideBadge) {
470 invalidate();
471 } else if (hasBadge()) {
472 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
473 }
474 }
475
476 private boolean hasBadge() {
477 return (mBadgeInfo != null && mBadgeInfo.getNotificationCount() > 0);
478 }
479
480 public void getIconBounds(Rect outBounds) {
481 int top = getPaddingTop();
482 int left = (getWidth() - mIconSize) / 2;
483 int right = left + mIconSize;
484 int bottom = top + mIconSize;
485 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400487
488 @Override
489 protected void onAttachedToWindow() {
490 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700491
Winson Chung656d11c2010-11-29 17:15:47 -0800492 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400493 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400494 }
495
496 @Override
Winson1f064272016-07-18 17:18:02 -0700497 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
498 if (mCenterVertically) {
499 Paint.FontMetrics fm = getPaint().getFontMetrics();
500 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
501 (int) Math.ceil(fm.bottom - fm.top);
502 int height = MeasureSpec.getSize(heightMeasureSpec);
503 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
504 getPaddingBottom());
505 }
506 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
507 }
508
509 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400510 protected void onDetachedFromWindow() {
511 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800512 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400513 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700514
Adam Cohen477828c2013-09-20 12:05:49 -0700515 @Override
516 public void setTextColor(int color) {
517 mTextColor = color;
518 super.setTextColor(color);
519 }
520
Adam Cohen96bb7982014-07-07 11:58:56 -0700521 @Override
522 public void setTextColor(ColorStateList colors) {
523 mTextColor = colors.getDefaultColor();
524 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700525 }
526
Winson Chung5f8afe62013-08-12 16:19:28 -0700527 public void setTextVisibility(boolean visible) {
528 Resources res = getResources();
529 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700530 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700531 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700532 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700533 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700534 }
535
Winson Chungaffd7b42010-08-20 15:11:56 -0700536 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800537 public void cancelLongPress() {
538 super.cancelLongPress();
539
540 mLongPressHelper.cancelLongPress();
541 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500542
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800543 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700544 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400545 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700546 final boolean isPromise = info.isPromise();
547 final int progressLevel = isPromise ?
548 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
549 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700550
Sunny Goyalc469aad2015-10-01 11:24:23 -0700551 setContentDescription(progressLevel > 0 ?
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800552 getContext().getString(R.string.app_downloading_title, info.title,
553 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
Sunny Goyalc469aad2015-10-01 11:24:23 -0700554 getContext().getString(R.string.app_waiting_download_title, info.title));
555
Winson Chungb745afb2015-03-02 11:51:23 -0800556 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700557 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800558 if (mIcon instanceof PreloadIconDrawable) {
559 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700560 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800561 preloadDrawable = DrawableFactory.get(getContext())
562 .newPendingIcon(info.iconBitmap, getContext());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700563 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700564 }
565
566 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700567 if (promiseStateChanged) {
568 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700569 }
Sunny Goyale755d462014-07-22 13:48:29 -0700570 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400571 }
572 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700573
Tony Wickham1e618492017-02-02 12:57:18 -0800574 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800575 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham010d2552017-01-20 08:15:28 -0800576 BadgeInfo badgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800577 BadgeRenderer badgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham9407d4a2017-02-24 15:08:13 -0800578 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(mLauncher);
579 if (popup != null) {
580 popup.updateNotificationHeader(badgeInfo, itemInfo);
581 }
Tony Wickham1237df02017-02-24 08:59:36 -0800582
583 boolean wasBadged = mBadgeInfo != null;
584 boolean isBadged = badgeInfo != null;
585 float newBadgeScale = isBadged ? 1f : 0;
586 mBadgeInfo = badgeInfo;
587 mBadgeRenderer = badgeRenderer;
588 if (wasBadged || isBadged) {
589 mIconPalette = ((FastBitmapDrawable) mIcon).getIconPalette();
590 // Animate when a badge is first added or when it is removed.
591 if (animate && (wasBadged ^ isBadged) && isShown()) {
592 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
593 } else {
594 mBadgeScale = newBadgeScale;
595 invalidate();
596 }
597 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800598 }
599 }
600
Winson Chungb745afb2015-03-02 11:51:23 -0800601 /**
602 * Sets the icon for this view based on the layout direction.
603 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700604 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800605 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800606 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700607 applyCompoundDrawables(mIcon);
608 }
609
610 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800611 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800612 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800613 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700614 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800615 }
Winson Chungb745afb2015-03-02 11:51:23 -0800616 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700617
Sunny Goyal69b75642015-05-15 17:00:24 -0700618 @Override
619 public void requestLayout() {
620 if (!mDisableRelayout) {
621 super.requestLayout();
622 }
623 }
624
Sunny Goyal34b65272015-03-11 16:56:52 -0700625 /**
626 * Applies the item info if it is same as what the view is pointing to currently.
627 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800628 @Override
629 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700630 if (getTag() == info) {
631 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700632 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700633
Sunny Goyal34b65272015-03-11 16:56:52 -0700634 if (info instanceof AppInfo) {
635 applyFromApplicationInfo((AppInfo) info);
636 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800637 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal317698b2015-07-29 11:45:41 -0700638 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
639 View folderIcon =
640 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
641 if (folderIcon != null) {
642 folderIcon.invalidate();
643 }
644 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700645 } else if (info instanceof PackageItemInfo) {
646 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700647 }
Winsonc0880492015-08-21 11:16:27 -0700648
Sunny Goyal69b75642015-05-15 17:00:24 -0700649 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700650 }
651 }
652
653 /**
654 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
655 */
656 public void verifyHighRes() {
657 if (mIconLoadRequest != null) {
658 mIconLoadRequest.cancel();
659 mIconLoadRequest = null;
660 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800661 if (getTag() instanceof ItemInfoWithIcon) {
662 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700663 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800664 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700665 .updateIconInBackground(BubbleTextView.this, info);
666 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700667 }
668 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700669
670 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700671 * Returns true if the view can show custom shortcuts.
672 */
673 public boolean hasDeepShortcuts() {
Tony Wickham010d2552017-01-20 08:15:28 -0800674 return !mLauncher.getPopupDataProvider().getShortcutIdsForItem((ItemInfo) getTag())
675 .isEmpty();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700676 }
677
678 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700679 * Interface to be implemented by the grand parent to allow click shadow effect.
680 */
Winsonc0880492015-08-21 11:16:27 -0700681 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700682 void setPressedIcon(BubbleTextView icon, Bitmap background);
683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684}