blob: 94c7e07fb2ff41dc1d06b81809186b7573993d65 [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;
Sunny Goyal34b65272015-03-11 16:56:52 -070052
Sunny Goyalc469aad2015-10-01 11:24:23 -070053import java.text.NumberFormat;
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055/**
56 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
57 * because we want to make the bubble taller than the text and TextView's clip is
58 * too aggressive.
59 */
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080060public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070061
Sunny Goyal8f8f3982016-07-25 17:08:38 -070062 // Dimensions in DP
63 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
64 private static final float KEY_SHADOW_RADIUS = 1f;
65 private static final float KEY_SHADOW_OFFSET = 0.5f;
66 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
67 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080068
Sunny Goyaldfaccf62015-05-11 16:30:44 -070069 private static final int DISPLAY_WORKSPACE = 0;
70 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070071 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070072
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080073 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
74
Sunny Goyal53d7ee42015-05-22 12:25:45 -070075 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080076 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070077 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070079 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080 private final CheckLongPressHelper mLongPressHelper;
81 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070082 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080083
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080084 private boolean mBackgroundSizeChanged;
85
Sunny Goyal508da152014-08-14 10:53:27 -070086 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080087
Jason Monk02dd7ae2014-04-15 15:23:31 -040088 private float mSlop;
89
Winson Chung93f98ea2015-03-10 16:28:47 -070090 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070091 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080092 private final boolean mLayoutHorizontal;
93 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080095 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070096
Tony Wickham1237df02017-02-24 08:59:36 -080097 private BadgeInfo mBadgeInfo;
98 private BadgeRenderer mBadgeRenderer;
99 private IconPalette mIconPalette;
100 private float mBadgeScale;
101 private boolean mForceHideBadge;
102 private Point mTempSpaceForBadgeOffset = new Point();
103 private Rect mTempIconBounds = new Rect();
104
105 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
106 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
107 @Override
108 public Float get(BubbleTextView bubbleTextView) {
109 return bubbleTextView.mBadgeScale;
110 }
111
112 @Override
113 public void set(BubbleTextView bubbleTextView, Float value) {
114 bubbleTextView.mBadgeScale = value;
115 bubbleTextView.invalidate();
116 }
117 };
118
Sunny Goyal4ffec482016-02-09 11:28:52 -0800119 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800120 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800121 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700122 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800123 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700124 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500125
Sunny Goyal34b65272015-03-11 16:56:52 -0700126 private IconLoadRequest mIconLoadRequest;
127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700129 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 }
131
132 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700133 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 }
135
136 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
137 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700138 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700139 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700140
Adam Cohen96bb7982014-07-07 11:58:56 -0700141 TypedArray a = context.obtainStyledAttributes(attrs,
142 R.styleable.BubbleTextView, defStyle, 0);
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800143 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, false);
Winson Chungb745afb2015-03-02 11:51:23 -0800144 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700145 mDeferShadowGenerationOnTouch =
146 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700147
148 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
149 int defaultIconSize = grid.iconSizePx;
150 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700151 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700152 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700153 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
154 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700155 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700156 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700157 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700158 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700159 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700160 }
Winson1f064272016-07-18 17:18:02 -0700161 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700162
163 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
164 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700165 a.recycle();
166
Sunny Goyal15872da2014-07-08 15:43:54 -0700167 if (mCustomShadowsEnabled) {
168 // Draw the background itself as the parent is drawn twice.
169 mBackground = getBackground();
170 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700171
172 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
173 float density = getResources().getDisplayMetrics().density;
174 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700175 } else {
176 mBackground = null;
177 }
Winson Chungb745afb2015-03-02 11:51:23 -0800178
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800179 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700180 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800181
Sunny Goyal10629b02016-09-01 12:50:11 -0700182 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700183 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 }
185
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800186 public void applyFromShortcutInfo(ShortcutInfo info) {
187 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700188 }
189
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800190 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
191 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800192 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700193 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800194 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500195 }
Tony Wickham010d2552017-01-20 08:15:28 -0800196
Tony Wickham1e618492017-02-02 12:57:18 -0800197 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800198 }
199
Sunny Goyal508da152014-08-14 10:53:27 -0700200 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700201 applyIconAndLabel(info.iconBitmap, info);
202
Winson Chung888b3a12015-03-13 11:14:16 -0700203 // We don't need to check the info since it's not a ShortcutInfo
204 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700205
206 // Verify high res immediately
207 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800208
Tony Wickham1e618492017-02-02 12:57:18 -0800209 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700210 }
211
Sunny Goyal0e08f162015-05-12 11:32:39 -0700212 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700213 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700214 // We don't need to check the info since it's not a ShortcutInfo
215 super.setTag(info);
216
217 // Verify high res immediately
218 verifyHighRes();
219 }
220
Sunny Goyalf4204382016-07-13 10:46:07 -0700221 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800222 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800223 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700224 setIcon(iconDrawable);
225 setText(info.title);
226 if (info.contentDescription != null) {
227 setContentDescription(info.isDisabled()
228 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
229 : info.contentDescription);
230 }
231 }
232
Winson Chung7501adf2015-06-02 11:24:28 -0700233 /**
234 * Overrides the default long press timeout.
235 */
236 public void setLongPressTimeout(int longPressTimeout) {
237 mLongPressHelper.setLongPressTimeout(longPressTimeout);
238 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 @Override
241 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700242 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 mBackgroundSizeChanged = true;
244 }
245 return super.setFrame(left, top, right, bottom);
246 }
247
248 @Override
249 protected boolean verifyDrawable(Drawable who) {
250 return who == mBackground || super.verifyDrawable(who);
251 }
252
253 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700254 public void setTag(Object tag) {
255 if (tag != null) {
256 LauncherModel.checkItemInfo((ItemInfo) tag);
257 }
258 super.setTag(tag);
259 }
260
261 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800262 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700263 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800264 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800265 }
Sunny Goyal508da152014-08-14 10:53:27 -0700266 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800267
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800268 @Override
269 protected int[] onCreateDrawableState(int extraSpace) {
270 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
271 if (mStayPressed) {
272 mergeDrawableStates(drawableState, STATE_PRESSED);
273 }
274 return drawableState;
275 }
276
Winson Chungb745afb2015-03-02 11:51:23 -0800277 /** Returns the icon for this view. */
278 public Drawable getIcon() {
279 return mIcon;
280 }
281
282 /** Returns whether the layout is horizontal. */
283 public boolean isLayoutHorizontal() {
284 return mLayoutHorizontal;
285 }
286
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800287 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700288 public void setOnLongClickListener(OnLongClickListener l) {
289 super.setOnLongClickListener(l);
290 mOnLongClickListener = l;
291 }
292
293 public OnLongClickListener getOnLongClickListener() {
294 return mOnLongClickListener;
295 }
296
297 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800298 public boolean onTouchEvent(MotionEvent event) {
299 // Call the superclass onTouchEvent first, because sometimes it changes the state to
300 // isPressed() on an ACTION_UP
301 boolean result = super.onTouchEvent(event);
302
Mady Melloref044dd2015-06-02 15:35:07 -0700303 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700304 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700305 mLongPressHelper.cancelLongPress();
306 result = true;
307 }
308
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800309 switch (event.getAction()) {
310 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700311 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800312 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
313 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700314 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700315 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800316 }
Winson Chung88f33452012-02-23 15:23:44 -0800317
Mady Melloref044dd2015-06-02 15:35:07 -0700318 // If we're in a stylus button press, don't check for long press.
319 if (!mStylusEventHelper.inStylusButtonPressed()) {
320 mLongPressHelper.postCheckForLongPress();
321 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800322 break;
323 case MotionEvent.ACTION_CANCEL:
324 case MotionEvent.ACTION_UP:
325 // If we've touched down and up on an item, and it's still not "pressed", then
326 // destroy the pressed outline
327 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700328 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800329 }
Winson Chung88f33452012-02-23 15:23:44 -0800330
331 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800332 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400333 case MotionEvent.ACTION_MOVE:
334 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
335 mLongPressHelper.cancelLongPress();
336 }
337 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800338 }
339 return result;
340 }
341
Michael Jurkaddd62e92011-02-16 17:49:14 -0800342 void setStayPressed(boolean stayPressed) {
343 mStayPressed = stayPressed;
344 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700345 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700346 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700347 } else {
348 if (mPressedBackground == null) {
349 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
350 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800351 }
Sunny Goyal508da152014-08-14 10:53:27 -0700352
353 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700354 ViewParent parent = getParent();
355 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
356 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
357 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700358 }
359
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800360 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800361 }
362
Sunny Goyal508da152014-08-14 10:53:27 -0700363 void clearPressedBackground() {
364 setPressed(false);
365 setStayPressed(false);
366 }
367
368 @Override
369 public boolean onKeyDown(int keyCode, KeyEvent event) {
370 if (super.onKeyDown(keyCode, event)) {
371 // Pre-create shadow so show immediately on click.
372 if (mPressedBackground == null) {
373 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700374 }
Sunny Goyal508da152014-08-14 10:53:27 -0700375 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700376 }
Sunny Goyal508da152014-08-14 10:53:27 -0700377 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800378 }
379
Sunny Goyal508da152014-08-14 10:53:27 -0700380 @Override
381 public boolean onKeyUp(int keyCode, KeyEvent event) {
382 // Unlike touch events, keypress event propagate pressed state change immediately,
383 // without waiting for onClickHandler to execute. Disable pressed state changes here
384 // to avoid flickering.
385 mIgnorePressedStateChange = true;
386 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700387
Sunny Goyal508da152014-08-14 10:53:27 -0700388 mPressedBackground = null;
389 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800390 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700391 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800392 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 @Override
395 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700396 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700397 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800398 drawBadgeIfNecessary(canvas);
Adam Cohen477828c2013-09-20 12:05:49 -0700399 return;
400 }
401
Michael Jurkabdb5c532011-02-01 15:05:06 -0800402 final Drawable background = mBackground;
403 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700404 final int scrollX = getScrollX();
405 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800406
Michael Jurkabdb5c532011-02-01 15:05:06 -0800407 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700408 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800409 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800411
412 if ((scrollX | scrollY) == 0) {
413 background.draw(canvas);
414 } else {
415 canvas.translate(scrollX, scrollY);
416 background.draw(canvas);
417 canvas.translate(-scrollX, -scrollY);
418 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800420
421 // If text is transparent, don't draw any shadow
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800422 if ((getCurrentTextColor() >> 24) == 0) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800423 getPaint().clearShadowLayer();
424 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800425 drawBadgeIfNecessary(canvas);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800426 return;
427 }
428
Michael Jurkabdb5c532011-02-01 15:05:06 -0800429 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700430 float density = getResources().getDisplayMetrics().density;
431 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800432 super.draw(canvas);
433 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700434 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
435 getScrollX() + getWidth(),
436 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700437 getPaint().setShadowLayer(
438 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800439 super.draw(canvas);
440 canvas.restore();
Tony Wickham1237df02017-02-24 08:59:36 -0800441
442 drawBadgeIfNecessary(canvas);
443 }
444
445 /**
446 * Draws the icon badge in the top right corner of the icon bounds.
447 * @param canvas The canvas to draw to.
448 */
449 private void drawBadgeIfNecessary(Canvas canvas) {
450 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
451 getIconBounds(mTempIconBounds);
452 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
453 final int scrollX = getScrollX();
454 final int scrollY = getScrollY();
455 canvas.translate(scrollX, scrollY);
Tony Wickhame6a790e2017-05-16 12:07:38 -0700456 mBadgeRenderer.draw(canvas, mBadgeInfo, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800457 mTempSpaceForBadgeOffset);
458 canvas.translate(-scrollX, -scrollY);
459 }
460 }
461
462 public void forceHideBadge(boolean forceHideBadge) {
463 if (mForceHideBadge == forceHideBadge) {
464 return;
465 }
466 mForceHideBadge = forceHideBadge;
467
468 if (forceHideBadge) {
469 invalidate();
470 } else if (hasBadge()) {
471 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
472 }
473 }
474
475 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700476 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800477 }
478
479 public void getIconBounds(Rect outBounds) {
480 int top = getPaddingTop();
481 int left = (getWidth() - mIconSize) / 2;
482 int right = left + mIconSize;
483 int bottom = top + mIconSize;
484 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400486
487 @Override
488 protected void onAttachedToWindow() {
489 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700490
Winson Chung656d11c2010-11-29 17:15:47 -0800491 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400492 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400493 }
494
495 @Override
Winson1f064272016-07-18 17:18:02 -0700496 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
497 if (mCenterVertically) {
498 Paint.FontMetrics fm = getPaint().getFontMetrics();
499 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
500 (int) Math.ceil(fm.bottom - fm.top);
501 int height = MeasureSpec.getSize(heightMeasureSpec);
502 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
503 getPaddingBottom());
504 }
505 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
506 }
507
508 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400509 protected void onDetachedFromWindow() {
510 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800511 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700513
Adam Cohen477828c2013-09-20 12:05:49 -0700514 @Override
515 public void setTextColor(int color) {
516 mTextColor = color;
517 super.setTextColor(color);
518 }
519
Adam Cohen96bb7982014-07-07 11:58:56 -0700520 @Override
521 public void setTextColor(ColorStateList colors) {
522 mTextColor = colors.getDefaultColor();
523 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700524 }
525
Winson Chung5f8afe62013-08-12 16:19:28 -0700526 public void setTextVisibility(boolean visible) {
527 Resources res = getResources();
528 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700529 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700530 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700531 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700532 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700533 }
534
Winson Chungaffd7b42010-08-20 15:11:56 -0700535 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800536 public void cancelLongPress() {
537 super.cancelLongPress();
538
539 mLongPressHelper.cancelLongPress();
540 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500541
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800542 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700543 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400544 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700545 final boolean isPromise = info.isPromise();
546 final int progressLevel = isPromise ?
547 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
548 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700549
Sunny Goyalc469aad2015-10-01 11:24:23 -0700550 setContentDescription(progressLevel > 0 ?
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800551 getContext().getString(R.string.app_downloading_title, info.title,
552 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
Sunny Goyalc469aad2015-10-01 11:24:23 -0700553 getContext().getString(R.string.app_waiting_download_title, info.title));
554
Winson Chungb745afb2015-03-02 11:51:23 -0800555 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700556 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800557 if (mIcon instanceof PreloadIconDrawable) {
558 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700559 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800560 preloadDrawable = DrawableFactory.get(getContext())
561 .newPendingIcon(info.iconBitmap, getContext());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700562 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700563 }
564
565 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700566 if (promiseStateChanged) {
567 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700568 }
Sunny Goyale755d462014-07-22 13:48:29 -0700569 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400570 }
571 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700572
Tony Wickham1e618492017-02-02 12:57:18 -0800573 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800574 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800575 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700576 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
577 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800578 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700579 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800580 if (wasBadged || isBadged) {
581 mIconPalette = ((FastBitmapDrawable) mIcon).getIconPalette();
582 // Animate when a badge is first added or when it is removed.
583 if (animate && (wasBadged ^ isBadged) && isShown()) {
584 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
585 } else {
586 mBadgeScale = newBadgeScale;
587 invalidate();
588 }
589 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800590 }
591 }
592
Winson Chungb745afb2015-03-02 11:51:23 -0800593 /**
594 * Sets the icon for this view based on the layout direction.
595 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700596 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800597 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800598 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700599 applyCompoundDrawables(mIcon);
600 }
601
602 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800603 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800604 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800605 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700606 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800607 }
Winson Chungb745afb2015-03-02 11:51:23 -0800608 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700609
Sunny Goyal69b75642015-05-15 17:00:24 -0700610 @Override
611 public void requestLayout() {
612 if (!mDisableRelayout) {
613 super.requestLayout();
614 }
615 }
616
Sunny Goyal34b65272015-03-11 16:56:52 -0700617 /**
618 * Applies the item info if it is same as what the view is pointing to currently.
619 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800620 @Override
621 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700622 if (getTag() == info) {
623 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700624 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700625
Sunny Goyal34b65272015-03-11 16:56:52 -0700626 if (info instanceof AppInfo) {
627 applyFromApplicationInfo((AppInfo) info);
628 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800629 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal317698b2015-07-29 11:45:41 -0700630 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
631 View folderIcon =
632 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
633 if (folderIcon != null) {
634 folderIcon.invalidate();
635 }
636 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700637 } else if (info instanceof PackageItemInfo) {
638 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700639 }
Winsonc0880492015-08-21 11:16:27 -0700640
Sunny Goyal69b75642015-05-15 17:00:24 -0700641 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700642 }
643 }
644
645 /**
646 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
647 */
648 public void verifyHighRes() {
649 if (mIconLoadRequest != null) {
650 mIconLoadRequest.cancel();
651 mIconLoadRequest = null;
652 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800653 if (getTag() instanceof ItemInfoWithIcon) {
654 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700655 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800656 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700657 .updateIconInBackground(BubbleTextView.this, info);
658 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700659 }
660 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700661
662 /**
663 * Interface to be implemented by the grand parent to allow click shadow effect.
664 */
Winsonc0880492015-08-21 11:16:27 -0700665 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700666 void setPressedIcon(BubbleTextView icon, Bitmap background);
667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668}