blob: 8043eacdbe108a70d138f382ea1e11addfa27b9f [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;
Sunny Goyal26119432016-02-18 22:09:23 +000042import com.android.launcher3.folder.FolderIcon;
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;
Tony Wickham9438ed42017-01-20 09:38:25 -080045import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080046import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070047import com.android.launcher3.model.PackageItemInfo;
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 */
Winson Chungb1777442015-06-16 13:35:04 -070056public class BubbleTextView extends TextView
Sunny Goyal2d7cca12017-01-03 16:52:43 -080057 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView, ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070058
Sunny Goyal8f8f3982016-07-25 17:08:38 -070059 // Dimensions in DP
60 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
61 private static final float KEY_SHADOW_RADIUS = 1f;
62 private static final float KEY_SHADOW_OFFSET = 0.5f;
63 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
64 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080065
Sunny Goyaldfaccf62015-05-11 16:30:44 -070066 private static final int DISPLAY_WORKSPACE = 0;
67 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070068 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070069
Sunny Goyal53d7ee42015-05-22 12:25:45 -070070 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080071 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070072 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080073 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070074 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075 private final CheckLongPressHelper mLongPressHelper;
76 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070077 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079 private boolean mBackgroundSizeChanged;
80
Sunny Goyal508da152014-08-14 10:53:27 -070081 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080082
Jason Monk02dd7ae2014-04-15 15:23:31 -040083 private float mSlop;
84
Winson Chung93f98ea2015-03-10 16:28:47 -070085 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070086 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080087 private final boolean mLayoutHorizontal;
88 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080089 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080090 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070091
Sunny Goyal4ffec482016-02-09 11:28:52 -080092 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080093 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070095 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070097 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050098
Sunny Goyal34b65272015-03-11 16:56:52 -070099 private IconLoadRequest mIconLoadRequest;
100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700102 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 }
104
105 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700106 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 }
108
109 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
110 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700111 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700112 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700113
Adam Cohen96bb7982014-07-07 11:58:56 -0700114 TypedArray a = context.obtainStyledAttributes(attrs,
115 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700116 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800117 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700118 mDeferShadowGenerationOnTouch =
119 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700120
121 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
122 int defaultIconSize = grid.iconSizePx;
123 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700124 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700125 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700126 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
127 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700128 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700129 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700130 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700131 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700132 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700133 }
Winson1f064272016-07-18 17:18:02 -0700134 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700135
136 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
137 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700138 a.recycle();
139
Sunny Goyal15872da2014-07-08 15:43:54 -0700140 if (mCustomShadowsEnabled) {
141 // Draw the background itself as the parent is drawn twice.
142 mBackground = getBackground();
143 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700144
145 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
146 float density = getResources().getDisplayMetrics().density;
147 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700148 } else {
149 mBackground = null;
150 }
Winson Chungb745afb2015-03-02 11:51:23 -0800151
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800152 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700153 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800154
Sunny Goyal10629b02016-09-01 12:50:11 -0700155 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700156 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 }
158
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800159 public void applyFromShortcutInfo(ShortcutInfo info) {
160 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700161 }
162
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800163 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
164 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800165 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700166 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800167 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500168 }
Tony Wickham010d2552017-01-20 08:15:28 -0800169
170 applyBadgeState(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800171 }
172
Sunny Goyal508da152014-08-14 10:53:27 -0700173 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700174 applyIconAndLabel(info.iconBitmap, info);
175
Winson Chung888b3a12015-03-13 11:14:16 -0700176 // We don't need to check the info since it's not a ShortcutInfo
177 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700178
179 // Verify high res immediately
180 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800181
182 applyBadgeState(info);
Sunny Goyal508da152014-08-14 10:53:27 -0700183 }
184
Sunny Goyal0e08f162015-05-12 11:32:39 -0700185 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700186 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700187 // We don't need to check the info since it's not a ShortcutInfo
188 super.setTag(info);
189
190 // Verify high res immediately
191 verifyHighRes();
192 }
193
Sunny Goyalf4204382016-07-13 10:46:07 -0700194 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800195 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800196 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700197 setIcon(iconDrawable);
198 setText(info.title);
199 if (info.contentDescription != null) {
200 setContentDescription(info.isDisabled()
201 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
202 : info.contentDescription);
203 }
204 }
205
Winson Chung7501adf2015-06-02 11:24:28 -0700206 /**
207 * Overrides the default long press timeout.
208 */
209 public void setLongPressTimeout(int longPressTimeout) {
210 mLongPressHelper.setLongPressTimeout(longPressTimeout);
211 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 @Override
214 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700215 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 mBackgroundSizeChanged = true;
217 }
218 return super.setFrame(left, top, right, bottom);
219 }
220
221 @Override
222 protected boolean verifyDrawable(Drawable who) {
223 return who == mBackground || super.verifyDrawable(who);
224 }
225
226 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700227 public void setTag(Object tag) {
228 if (tag != null) {
229 LauncherModel.checkItemInfo((ItemInfo) tag);
230 }
231 super.setTag(tag);
232 }
233
234 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700235 public void setPressed(boolean pressed) {
236 super.setPressed(pressed);
237
238 if (!mIgnorePressedStateChange) {
239 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800240 }
Sunny Goyal508da152014-08-14 10:53:27 -0700241 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800242
Winson Chungb745afb2015-03-02 11:51:23 -0800243 /** Returns the icon for this view. */
244 public Drawable getIcon() {
245 return mIcon;
246 }
247
248 /** Returns whether the layout is horizontal. */
249 public boolean isLayoutHorizontal() {
250 return mLayoutHorizontal;
251 }
252
Sunny Goyal508da152014-08-14 10:53:27 -0700253 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800254 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700255 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Tony Wickham6b910a22016-11-08 10:40:34 -0800256 if (isPressed() || mStayPressed) {
Kenny Guy44cba692016-01-21 19:50:02 +0000257 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700258 } else {
259 d.animateState(FastBitmapDrawable.State.NORMAL);
260 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800262 }
263
264 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700265 public void setOnLongClickListener(OnLongClickListener l) {
266 super.setOnLongClickListener(l);
267 mOnLongClickListener = l;
268 }
269
270 public OnLongClickListener getOnLongClickListener() {
271 return mOnLongClickListener;
272 }
273
274 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800275 public boolean onTouchEvent(MotionEvent event) {
276 // Call the superclass onTouchEvent first, because sometimes it changes the state to
277 // isPressed() on an ACTION_UP
278 boolean result = super.onTouchEvent(event);
279
Mady Melloref044dd2015-06-02 15:35:07 -0700280 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700281 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700282 mLongPressHelper.cancelLongPress();
283 result = true;
284 }
285
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800286 switch (event.getAction()) {
287 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700288 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800289 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
290 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700291 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700292 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800293 }
Winson Chung88f33452012-02-23 15:23:44 -0800294
Mady Melloref044dd2015-06-02 15:35:07 -0700295 // If we're in a stylus button press, don't check for long press.
296 if (!mStylusEventHelper.inStylusButtonPressed()) {
297 mLongPressHelper.postCheckForLongPress();
298 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800299 break;
300 case MotionEvent.ACTION_CANCEL:
301 case MotionEvent.ACTION_UP:
302 // If we've touched down and up on an item, and it's still not "pressed", then
303 // destroy the pressed outline
304 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700305 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800306 }
Winson Chung88f33452012-02-23 15:23:44 -0800307
308 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800309 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400310 case MotionEvent.ACTION_MOVE:
311 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
312 mLongPressHelper.cancelLongPress();
313 }
314 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800315 }
316 return result;
317 }
318
Michael Jurkaddd62e92011-02-16 17:49:14 -0800319 void setStayPressed(boolean stayPressed) {
320 mStayPressed = stayPressed;
321 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700322 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700323 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700324 } else {
325 if (mPressedBackground == null) {
326 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
327 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800328 }
Sunny Goyal508da152014-08-14 10:53:27 -0700329
330 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700331 ViewParent parent = getParent();
332 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
333 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
334 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700335 }
336
337 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800338 }
339
Sunny Goyal508da152014-08-14 10:53:27 -0700340 void clearPressedBackground() {
341 setPressed(false);
342 setStayPressed(false);
343 }
344
345 @Override
346 public boolean onKeyDown(int keyCode, KeyEvent event) {
347 if (super.onKeyDown(keyCode, event)) {
348 // Pre-create shadow so show immediately on click.
349 if (mPressedBackground == null) {
350 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700351 }
Sunny Goyal508da152014-08-14 10:53:27 -0700352 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700353 }
Sunny Goyal508da152014-08-14 10:53:27 -0700354 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800355 }
356
Sunny Goyal508da152014-08-14 10:53:27 -0700357 @Override
358 public boolean onKeyUp(int keyCode, KeyEvent event) {
359 // Unlike touch events, keypress event propagate pressed state change immediately,
360 // without waiting for onClickHandler to execute. Disable pressed state changes here
361 // to avoid flickering.
362 mIgnorePressedStateChange = true;
363 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700364
Sunny Goyal508da152014-08-14 10:53:27 -0700365 mPressedBackground = null;
366 mIgnorePressedStateChange = false;
367 updateIconState();
368 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800369 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 @Override
372 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700373 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700374 super.draw(canvas);
375 return;
376 }
377
Michael Jurkabdb5c532011-02-01 15:05:06 -0800378 final Drawable background = mBackground;
379 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700380 final int scrollX = getScrollX();
381 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800382
Michael Jurkabdb5c532011-02-01 15:05:06 -0800383 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700384 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800385 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800387
388 if ((scrollX | scrollY) == 0) {
389 background.draw(canvas);
390 } else {
391 canvas.translate(scrollX, scrollY);
392 background.draw(canvas);
393 canvas.translate(-scrollX, -scrollY);
394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800396
397 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800398 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800399 getPaint().clearShadowLayer();
400 super.draw(canvas);
401 return;
402 }
403
Michael Jurkabdb5c532011-02-01 15:05:06 -0800404 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700405 float density = getResources().getDisplayMetrics().density;
406 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800407 super.draw(canvas);
408 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700409 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
410 getScrollX() + getWidth(),
411 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700412 getPaint().setShadowLayer(
413 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800414 super.draw(canvas);
415 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400417
418 @Override
419 protected void onAttachedToWindow() {
420 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700421
Winson Chung656d11c2010-11-29 17:15:47 -0800422 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400423 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400424 }
425
426 @Override
Winson1f064272016-07-18 17:18:02 -0700427 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
428 if (mCenterVertically) {
429 Paint.FontMetrics fm = getPaint().getFontMetrics();
430 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
431 (int) Math.ceil(fm.bottom - fm.top);
432 int height = MeasureSpec.getSize(heightMeasureSpec);
433 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
434 getPaddingBottom());
435 }
436 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
437 }
438
439 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440 protected void onDetachedFromWindow() {
441 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800442 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700444
Adam Cohen477828c2013-09-20 12:05:49 -0700445 @Override
446 public void setTextColor(int color) {
447 mTextColor = color;
448 super.setTextColor(color);
449 }
450
Adam Cohen96bb7982014-07-07 11:58:56 -0700451 @Override
452 public void setTextColor(ColorStateList colors) {
453 mTextColor = colors.getDefaultColor();
454 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700455 }
456
Winson Chung5f8afe62013-08-12 16:19:28 -0700457 public void setTextVisibility(boolean visible) {
458 Resources res = getResources();
459 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700460 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700461 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700462 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700463 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700464 }
465
Winson Chungaffd7b42010-08-20 15:11:56 -0700466 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800467 public void cancelLongPress() {
468 super.cancelLongPress();
469
470 mLongPressHelper.cancelLongPress();
471 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500472
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800473 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700474 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400475 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700476 final boolean isPromise = info.isPromise();
477 final int progressLevel = isPromise ?
478 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
479 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700480
Sunny Goyalc469aad2015-10-01 11:24:23 -0700481 setContentDescription(progressLevel > 0 ?
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800482 getContext().getString(R.string.app_downloading_title, info.title,
483 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
Sunny Goyalc469aad2015-10-01 11:24:23 -0700484 getContext().getString(R.string.app_waiting_download_title, info.title));
485
Winson Chungb745afb2015-03-02 11:51:23 -0800486 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700487 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800488 if (mIcon instanceof PreloadIconDrawable) {
489 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700490 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800491 preloadDrawable = DrawableFactory.get(getContext())
492 .newPendingIcon(info.iconBitmap, getContext());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700493 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700494 }
495
496 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700497 if (promiseStateChanged) {
498 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700499 }
Sunny Goyale755d462014-07-22 13:48:29 -0700500 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400501 }
502 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700503
Tony Wickham010d2552017-01-20 08:15:28 -0800504 public void applyBadgeState(ItemInfo itemInfo) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800505 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham010d2552017-01-20 08:15:28 -0800506 BadgeInfo badgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800507 BadgeRenderer badgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
508 ((FastBitmapDrawable) mIcon).applyIconBadge(badgeInfo, badgeRenderer);
509 }
510 }
511
Tony Wickham9438ed42017-01-20 09:38:25 -0800512 public IconPalette getIconPalette() {
513 return mIcon instanceof FastBitmapDrawable ? ((FastBitmapDrawable) mIcon).getIconPalette()
514 : null;
515 }
516
Winson Chungb745afb2015-03-02 11:51:23 -0800517 /**
518 * Sets the icon for this view based on the layout direction.
519 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700520 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800521 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800522 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700523 applyCompoundDrawables(mIcon);
524 }
525
526 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800527 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800528 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800529 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700530 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800531 }
Winson Chungb745afb2015-03-02 11:51:23 -0800532 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700533
Sunny Goyal69b75642015-05-15 17:00:24 -0700534 @Override
535 public void requestLayout() {
536 if (!mDisableRelayout) {
537 super.requestLayout();
538 }
539 }
540
Sunny Goyal34b65272015-03-11 16:56:52 -0700541 /**
542 * Applies the item info if it is same as what the view is pointing to currently.
543 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800544 @Override
545 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700546 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700547 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
548 if (mIcon instanceof FastBitmapDrawable) {
549 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
550 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700551 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700552 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700553
Sunny Goyal34b65272015-03-11 16:56:52 -0700554 if (info instanceof AppInfo) {
555 applyFromApplicationInfo((AppInfo) info);
556 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800557 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal317698b2015-07-29 11:45:41 -0700558 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
559 View folderIcon =
560 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
561 if (folderIcon != null) {
562 folderIcon.invalidate();
563 }
564 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700565 } else if (info instanceof PackageItemInfo) {
566 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700567 }
Winsonc0880492015-08-21 11:16:27 -0700568
569 // If we are reapplying over an old icon, then we should update the new icon to the same
570 // state as the old icon
571 if (mIcon instanceof FastBitmapDrawable) {
572 ((FastBitmapDrawable) mIcon).setState(prevState);
573 }
574
Sunny Goyal69b75642015-05-15 17:00:24 -0700575 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700576 }
577 }
578
579 /**
580 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
581 */
582 public void verifyHighRes() {
583 if (mIconLoadRequest != null) {
584 mIconLoadRequest.cancel();
585 mIconLoadRequest = null;
586 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800587 if (getTag() instanceof ItemInfoWithIcon) {
588 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700589 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800590 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700591 .updateIconInBackground(BubbleTextView.this, info);
592 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700593 }
594 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700595
Winson Chungb1777442015-06-16 13:35:04 -0700596 @Override
Winsonc0880492015-08-21 11:16:27 -0700597 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
598 // We can only set the fast scroll focus state on a FastBitmapDrawable
599 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700600 return;
601 }
602
Winsonc0880492015-08-21 11:16:27 -0700603 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
604 if (animated) {
605 FastBitmapDrawable.State prevState = d.getCurrentState();
606 if (d.animateState(focusState)) {
607 // If the state was updated, then update the view accordingly
608 animate().scaleX(focusState.viewScale)
609 .scaleY(focusState.viewScale)
610 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
611 .setDuration(d.getDurationForStateChange(prevState, focusState))
612 .start();
613 }
614 } else {
615 if (d.setState(focusState)) {
616 // If the state was updated, then update the view accordingly
617 animate().cancel();
618 setScaleX(focusState.viewScale);
619 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700620 }
621 }
622 }
623
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700624 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700625 * Returns true if the view can show custom shortcuts.
626 */
627 public boolean hasDeepShortcuts() {
Tony Wickham010d2552017-01-20 08:15:28 -0800628 return !mLauncher.getPopupDataProvider().getShortcutIdsForItem((ItemInfo) getTag())
629 .isEmpty();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700630 }
631
632 /**
Winsonc0880492015-08-21 11:16:27 -0700633 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
634 */
635 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
636 final FastBitmapDrawable.State toState) {
637 switch (toState) {
638 case NORMAL:
639 switch (fromState) {
640 case FAST_SCROLL_HIGHLIGHTED:
641 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
642 }
643 }
644 return 0;
645 }
646
647 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700648 * Interface to be implemented by the grand parent to allow click shadow effect.
649 */
Winsonc0880492015-08-21 11:16:27 -0700650 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700651 void setPressedIcon(BubbleTextView icon, Bitmap background);
652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653}