blob: add0185ba5e90bed4f88a6acd5fd5687ba0035a7 [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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070020import android.content.res.ColorStateList;
Winson Chung656d11c2010-11-29 17:15:47 -080021import android.content.res.Resources;
Adam Cohen96bb7982014-07-07 11:58:56 -070022import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080023import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Canvas;
Winson1f064272016-07-18 17:18:02 -070025import android.graphics.Paint;
Michael Jurka137142e2011-01-05 20:57:04 -080026import android.graphics.Region;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080028import android.util.AttributeSet;
Winson Chung5f8afe62013-08-12 16:19:28 -070029import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070030import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080031import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070032import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040033import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080034import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070035import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080036import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070037
Sunny Goyal34b65272015-03-11 16:56:52 -070038import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080039import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080040import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080041import com.android.launcher3.badge.BadgeRenderer;
Jon Miranda529af302017-02-28 14:18:54 -080042import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080043import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070044import com.android.launcher3.graphics.HolographicOutlineHelper;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080045import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070046import com.android.launcher3.model.PackageItemInfo;
Tony Wickham9407d4a2017-02-24 15:08:13 -080047import com.android.launcher3.popup.PopupContainerWithArrow;
Sunny Goyal34b65272015-03-11 16:56:52 -070048
Sunny Goyalc469aad2015-10-01 11:24:23 -070049import java.text.NumberFormat;
50
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051/**
52 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
53 * because we want to make the bubble taller than the text and TextView's clip is
54 * too aggressive.
55 */
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080056public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070057
Sunny Goyal8f8f3982016-07-25 17:08:38 -070058 // Dimensions in DP
59 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
60 private static final float KEY_SHADOW_RADIUS = 1f;
61 private static final float KEY_SHADOW_OFFSET = 0.5f;
62 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
63 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080064
Sunny Goyaldfaccf62015-05-11 16:30:44 -070065 private static final int DISPLAY_WORKSPACE = 0;
66 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070067 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070068
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080069 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
70
Sunny Goyal53d7ee42015-05-22 12:25:45 -070071 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080072 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070073 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080074 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070075 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080076 private final CheckLongPressHelper mLongPressHelper;
77 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070078 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080 private boolean mBackgroundSizeChanged;
81
Sunny Goyal508da152014-08-14 10:53:27 -070082 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080083
Jason Monk02dd7ae2014-04-15 15:23:31 -040084 private float mSlop;
85
Winson Chung93f98ea2015-03-10 16:28:47 -070086 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070087 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080088 private final boolean mLayoutHorizontal;
89 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080090 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080091 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070092
Sunny Goyal4ffec482016-02-09 11:28:52 -080093 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080094 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070096 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070098 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050099
Sunny Goyal34b65272015-03-11 16:56:52 -0700100 private IconLoadRequest mIconLoadRequest;
101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700103 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 }
105
106 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700107 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 }
109
110 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
111 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700112 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700113 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700114
Adam Cohen96bb7982014-07-07 11:58:56 -0700115 TypedArray a = context.obtainStyledAttributes(attrs,
116 R.styleable.BubbleTextView, defStyle, 0);
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800117 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, false);
Winson Chungb745afb2015-03-02 11:51:23 -0800118 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700119 mDeferShadowGenerationOnTouch =
120 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700121
122 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
123 int defaultIconSize = grid.iconSizePx;
124 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700125 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700126 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700127 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
128 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700129 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700130 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700131 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700132 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700133 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700134 }
Winson1f064272016-07-18 17:18:02 -0700135 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700136
137 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
138 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700139 a.recycle();
140
Sunny Goyal15872da2014-07-08 15:43:54 -0700141 if (mCustomShadowsEnabled) {
142 // Draw the background itself as the parent is drawn twice.
143 mBackground = getBackground();
144 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700145
146 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
147 float density = getResources().getDisplayMetrics().density;
148 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700149 } else {
150 mBackground = null;
151 }
Winson Chungb745afb2015-03-02 11:51:23 -0800152
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800153 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700154 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800155
Sunny Goyal10629b02016-09-01 12:50:11 -0700156 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700157 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 }
159
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800160 public void applyFromShortcutInfo(ShortcutInfo info) {
161 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700162 }
163
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800164 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
165 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800166 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700167 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800168 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500169 }
Tony Wickham010d2552017-01-20 08:15:28 -0800170
Tony Wickham1e618492017-02-02 12:57:18 -0800171 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800172 }
173
Sunny Goyal508da152014-08-14 10:53:27 -0700174 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700175 applyIconAndLabel(info.iconBitmap, info);
176
Winson Chung888b3a12015-03-13 11:14:16 -0700177 // We don't need to check the info since it's not a ShortcutInfo
178 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700179
180 // Verify high res immediately
181 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800182
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700183 if (info instanceof PromiseAppInfo) {
184 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
185 applyProgressLevel(promiseAppInfo.level);
186 }
Tony Wickham1e618492017-02-02 12:57:18 -0800187 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700188 }
189
Sunny Goyal0e08f162015-05-12 11:32:39 -0700190 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700191 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700192 // We don't need to check the info since it's not a ShortcutInfo
193 super.setTag(info);
194
195 // Verify high res immediately
196 verifyHighRes();
197 }
198
Sunny Goyalf4204382016-07-13 10:46:07 -0700199 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800200 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800201 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700202 setIcon(iconDrawable);
203 setText(info.title);
204 if (info.contentDescription != null) {
205 setContentDescription(info.isDisabled()
206 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
207 : info.contentDescription);
208 }
209 }
210
Winson Chung7501adf2015-06-02 11:24:28 -0700211 /**
212 * Overrides the default long press timeout.
213 */
214 public void setLongPressTimeout(int longPressTimeout) {
215 mLongPressHelper.setLongPressTimeout(longPressTimeout);
216 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 @Override
219 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700220 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 mBackgroundSizeChanged = true;
222 }
223 return super.setFrame(left, top, right, bottom);
224 }
225
226 @Override
227 protected boolean verifyDrawable(Drawable who) {
228 return who == mBackground || super.verifyDrawable(who);
229 }
230
231 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700232 public void setTag(Object tag) {
233 if (tag != null) {
234 LauncherModel.checkItemInfo((ItemInfo) tag);
235 }
236 super.setTag(tag);
237 }
238
239 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800240 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700241 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800242 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800243 }
Sunny Goyal508da152014-08-14 10:53:27 -0700244 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800245
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800246 @Override
247 protected int[] onCreateDrawableState(int extraSpace) {
248 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
249 if (mStayPressed) {
250 mergeDrawableStates(drawableState, STATE_PRESSED);
251 }
252 return drawableState;
253 }
254
Winson Chungb745afb2015-03-02 11:51:23 -0800255 /** Returns the icon for this view. */
256 public Drawable getIcon() {
257 return mIcon;
258 }
259
260 /** Returns whether the layout is horizontal. */
261 public boolean isLayoutHorizontal() {
262 return mLayoutHorizontal;
263 }
264
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800265 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700266 public void setOnLongClickListener(OnLongClickListener l) {
267 super.setOnLongClickListener(l);
268 mOnLongClickListener = l;
269 }
270
271 public OnLongClickListener getOnLongClickListener() {
272 return mOnLongClickListener;
273 }
274
275 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800276 public boolean onTouchEvent(MotionEvent event) {
277 // Call the superclass onTouchEvent first, because sometimes it changes the state to
278 // isPressed() on an ACTION_UP
279 boolean result = super.onTouchEvent(event);
280
Mady Melloref044dd2015-06-02 15:35:07 -0700281 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700282 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700283 mLongPressHelper.cancelLongPress();
284 result = true;
285 }
286
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800287 switch (event.getAction()) {
288 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700289 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800290 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
291 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700292 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700293 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800294 }
Winson Chung88f33452012-02-23 15:23:44 -0800295
Mady Melloref044dd2015-06-02 15:35:07 -0700296 // If we're in a stylus button press, don't check for long press.
297 if (!mStylusEventHelper.inStylusButtonPressed()) {
298 mLongPressHelper.postCheckForLongPress();
299 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800300 break;
301 case MotionEvent.ACTION_CANCEL:
302 case MotionEvent.ACTION_UP:
303 // If we've touched down and up on an item, and it's still not "pressed", then
304 // destroy the pressed outline
305 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700306 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800307 }
Winson Chung88f33452012-02-23 15:23:44 -0800308
309 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800310 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400311 case MotionEvent.ACTION_MOVE:
312 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
313 mLongPressHelper.cancelLongPress();
314 }
315 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800316 }
317 return result;
318 }
319
Michael Jurkaddd62e92011-02-16 17:49:14 -0800320 void setStayPressed(boolean stayPressed) {
321 mStayPressed = stayPressed;
322 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700323 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700324 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700325 } else {
326 if (mPressedBackground == null) {
327 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
328 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800329 }
Sunny Goyal508da152014-08-14 10:53:27 -0700330
331 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700332 ViewParent parent = getParent();
333 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
334 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
335 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700336 }
337
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800338 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800339 }
340
Sunny Goyal508da152014-08-14 10:53:27 -0700341 void clearPressedBackground() {
342 setPressed(false);
343 setStayPressed(false);
344 }
345
346 @Override
347 public boolean onKeyDown(int keyCode, KeyEvent event) {
348 if (super.onKeyDown(keyCode, event)) {
349 // Pre-create shadow so show immediately on click.
350 if (mPressedBackground == null) {
351 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700352 }
Sunny Goyal508da152014-08-14 10:53:27 -0700353 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700354 }
Sunny Goyal508da152014-08-14 10:53:27 -0700355 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800356 }
357
Sunny Goyal508da152014-08-14 10:53:27 -0700358 @Override
359 public boolean onKeyUp(int keyCode, KeyEvent event) {
360 // Unlike touch events, keypress event propagate pressed state change immediately,
361 // without waiting for onClickHandler to execute. Disable pressed state changes here
362 // to avoid flickering.
363 mIgnorePressedStateChange = true;
364 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700365
Sunny Goyal508da152014-08-14 10:53:27 -0700366 mPressedBackground = null;
367 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800368 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700369 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800370 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800371
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 @Override
373 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700374 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700375 super.draw(canvas);
376 return;
377 }
378
Michael Jurkabdb5c532011-02-01 15:05:06 -0800379 final Drawable background = mBackground;
380 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700381 final int scrollX = getScrollX();
382 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800383
Michael Jurkabdb5c532011-02-01 15:05:06 -0800384 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700385 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800386 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800388
389 if ((scrollX | scrollY) == 0) {
390 background.draw(canvas);
391 } else {
392 canvas.translate(scrollX, scrollY);
393 background.draw(canvas);
394 canvas.translate(-scrollX, -scrollY);
395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800397
398 // If text is transparent, don't draw any shadow
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800399 if ((getCurrentTextColor() >> 24) == 0) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800400 getPaint().clearShadowLayer();
401 super.draw(canvas);
402 return;
403 }
404
Michael Jurkabdb5c532011-02-01 15:05:06 -0800405 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700406 float density = getResources().getDisplayMetrics().density;
407 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800408 super.draw(canvas);
409 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700410 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
411 getScrollX() + getWidth(),
412 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700413 getPaint().setShadowLayer(
414 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800415 super.draw(canvas);
416 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400418
419 @Override
420 protected void onAttachedToWindow() {
421 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700422
Winson Chung656d11c2010-11-29 17:15:47 -0800423 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400424 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 }
426
427 @Override
Winson1f064272016-07-18 17:18:02 -0700428 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
429 if (mCenterVertically) {
430 Paint.FontMetrics fm = getPaint().getFontMetrics();
431 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
432 (int) Math.ceil(fm.bottom - fm.top);
433 int height = MeasureSpec.getSize(heightMeasureSpec);
434 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
435 getPaddingBottom());
436 }
437 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
438 }
439
440 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441 protected void onDetachedFromWindow() {
442 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800443 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400444 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700445
Adam Cohen477828c2013-09-20 12:05:49 -0700446 @Override
447 public void setTextColor(int color) {
448 mTextColor = color;
449 super.setTextColor(color);
450 }
451
Adam Cohen96bb7982014-07-07 11:58:56 -0700452 @Override
453 public void setTextColor(ColorStateList colors) {
454 mTextColor = colors.getDefaultColor();
455 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700456 }
457
Winson Chung5f8afe62013-08-12 16:19:28 -0700458 public void setTextVisibility(boolean visible) {
459 Resources res = getResources();
460 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700461 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700462 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700463 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700464 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700465 }
466
Winson Chungaffd7b42010-08-20 15:11:56 -0700467 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800468 public void cancelLongPress() {
469 super.cancelLongPress();
470
471 mLongPressHelper.cancelLongPress();
472 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500473
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800474 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700475 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400476 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700477 final boolean isPromise = info.isPromise();
478 final int progressLevel = isPromise ?
479 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
480 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700481
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700482 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
483 if (preloadDrawable != null && promiseStateChanged) {
484 preloadDrawable.maybePerformFinishedAnimation();
485 }
486 }
487 }
488
489 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
490 if (getTag() instanceof ItemInfoWithIcon) {
491 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
492 setContentDescription(progressLevel > 0
493 ? getContext().getString(R.string.app_downloading_title, info.title,
494 NumberFormat.getPercentInstance().format(progressLevel * 0.01))
495 : getContext().getString(R.string.app_waiting_download_title, info.title));
Sunny Goyalc469aad2015-10-01 11:24:23 -0700496
Winson Chungb745afb2015-03-02 11:51:23 -0800497 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700498 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800499 if (mIcon instanceof PreloadIconDrawable) {
500 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700501 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700502 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800503 preloadDrawable = DrawableFactory.get(getContext())
504 .newPendingIcon(info.iconBitmap, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700505 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700506 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700507 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700508 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700509 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400510 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700511 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400512 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700513
Tony Wickham1e618492017-02-02 12:57:18 -0800514 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800515 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham010d2552017-01-20 08:15:28 -0800516 BadgeInfo badgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800517 BadgeRenderer badgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham9407d4a2017-02-24 15:08:13 -0800518 PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(mLauncher);
519 if (popup != null) {
520 popup.updateNotificationHeader(badgeInfo, itemInfo);
521 }
Tony Wickham1e618492017-02-02 12:57:18 -0800522 ((FastBitmapDrawable) mIcon).applyIconBadge(badgeInfo, badgeRenderer, animate);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800523 }
524 }
525
Winson Chungb745afb2015-03-02 11:51:23 -0800526 /**
527 * Sets the icon for this view based on the layout direction.
528 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700529 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800530 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800531 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700532 applyCompoundDrawables(mIcon);
533 }
534
535 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800536 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800537 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800538 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700539 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800540 }
Winson Chungb745afb2015-03-02 11:51:23 -0800541 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700542
Sunny Goyal69b75642015-05-15 17:00:24 -0700543 @Override
544 public void requestLayout() {
545 if (!mDisableRelayout) {
546 super.requestLayout();
547 }
548 }
549
Sunny Goyal34b65272015-03-11 16:56:52 -0700550 /**
551 * Applies the item info if it is same as what the view is pointing to currently.
552 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800553 @Override
554 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700555 if (getTag() == info) {
556 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700557 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700558
Sunny Goyal34b65272015-03-11 16:56:52 -0700559 if (info instanceof AppInfo) {
560 applyFromApplicationInfo((AppInfo) info);
561 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800562 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800563 FolderIconPreviewVerifier verifier =
564 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
565 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700566 View folderIcon =
567 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
568 if (folderIcon != null) {
569 folderIcon.invalidate();
570 }
571 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700572 } else if (info instanceof PackageItemInfo) {
573 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700574 }
Winsonc0880492015-08-21 11:16:27 -0700575
Sunny Goyal69b75642015-05-15 17:00:24 -0700576 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700577 }
578 }
579
580 /**
581 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
582 */
583 public void verifyHighRes() {
584 if (mIconLoadRequest != null) {
585 mIconLoadRequest.cancel();
586 mIconLoadRequest = null;
587 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800588 if (getTag() instanceof ItemInfoWithIcon) {
589 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700590 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800591 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700592 .updateIconInBackground(BubbleTextView.this, info);
593 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700594 }
595 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700596
597 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700598 * Returns true if the view can show custom shortcuts.
599 */
600 public boolean hasDeepShortcuts() {
Tony Wickham010d2552017-01-20 08:15:28 -0800601 return !mLauncher.getPopupDataProvider().getShortcutIdsForItem((ItemInfo) getTag())
602 .isEmpty();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700603 }
604
Jon Miranda47170112017-03-03 14:57:14 -0800605 public int getIconSize() {
606 return mIconSize;
607 }
608
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700609 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700610 * Interface to be implemented by the grand parent to allow click shadow effect.
611 */
Winsonc0880492015-08-21 11:16:27 -0700612 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700613 void setPressedIcon(BubbleTextView icon, Bitmap background);
614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615}