blob: ed8b53132de5244a1f9f92d9ddbad8bb761967b2 [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;
Sunny Goyal95abbb32014-08-04 10:53:22 -070022import android.content.res.Resources.Theme;
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;
Michael Jurka137142e2011-01-05 20:57:04 -080027import android.graphics.Region;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080029import android.util.AttributeSet;
Sunny Goyal95abbb32014-08-04 10:53:22 -070030import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070031import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070032import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080033import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070034import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040035import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080036import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070037import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080038import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070039
Sunny Goyal34b65272015-03-11 16:56:52 -070040import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080041import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080042import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080043import com.android.launcher3.badge.BadgeRenderer;
Sunny Goyal26119432016-02-18 22:09:23 +000044import com.android.launcher3.folder.FolderIcon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080045import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070046import com.android.launcher3.graphics.HolographicOutlineHelper;
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
Sameer Padala8fd74832014-09-08 16:00:29 -070059 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070060
Sunny Goyal8f8f3982016-07-25 17:08:38 -070061 // Dimensions in DP
62 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
63 private static final float KEY_SHADOW_RADIUS = 1f;
64 private static final float KEY_SHADOW_OFFSET = 0.5f;
65 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
66 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080067
Sunny Goyaldfaccf62015-05-11 16:30:44 -070068 private static final int DISPLAY_WORKSPACE = 0;
69 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070070 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070071
Sunny Goyal53d7ee42015-05-22 12:25:45 -070072 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080073 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070074 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070076 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080077 private final CheckLongPressHelper mLongPressHelper;
78 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070079 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080081 private boolean mBackgroundSizeChanged;
82
Sunny Goyal508da152014-08-14 10:53:27 -070083 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080084
Jason Monk02dd7ae2014-04-15 15:23:31 -040085 private float mSlop;
86
Winson Chung93f98ea2015-03-10 16:28:47 -070087 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070088 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080089 private final boolean mLayoutHorizontal;
90 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080091 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080092 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070093
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080095 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070097 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080098 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070099 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500100
Sunny Goyal34b65272015-03-11 16:56:52 -0700101 private IconLoadRequest mIconLoadRequest;
102
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700104 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 }
106
107 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700108 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 }
110
111 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
112 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700113 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700114 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700115
Adam Cohen96bb7982014-07-07 11:58:56 -0700116 TypedArray a = context.obtainStyledAttributes(attrs,
117 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700118 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800119 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700120 mDeferShadowGenerationOnTouch =
121 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700122
123 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
124 int defaultIconSize = grid.iconSizePx;
125 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700126 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700127 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700128 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
129 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700130 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700131 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700132 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700133 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700134 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700135 }
Winson1f064272016-07-18 17:18:02 -0700136 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700137
138 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
139 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700140 a.recycle();
141
Sunny Goyal15872da2014-07-08 15:43:54 -0700142 if (mCustomShadowsEnabled) {
143 // Draw the background itself as the parent is drawn twice.
144 mBackground = getBackground();
145 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700146
147 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
148 float density = getResources().getDisplayMetrics().density;
149 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700150 } else {
151 mBackground = null;
152 }
Winson Chungb745afb2015-03-02 11:51:23 -0800153
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800154 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700155 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800156
Sunny Goyal10629b02016-09-01 12:50:11 -0700157 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700158 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 }
160
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800161 public void applyFromShortcutInfo(ShortcutInfo info) {
162 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700163 }
164
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800165 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
166 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800167 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700168 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800169 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500170 }
Tony Wickham010d2552017-01-20 08:15:28 -0800171
172 applyBadgeState(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800173 }
174
Sunny Goyal508da152014-08-14 10:53:27 -0700175 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700176 applyIconAndLabel(info.iconBitmap, info);
177
Winson Chung888b3a12015-03-13 11:14:16 -0700178 // We don't need to check the info since it's not a ShortcutInfo
179 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700180
181 // Verify high res immediately
182 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800183
184 applyBadgeState(info);
Sunny Goyal508da152014-08-14 10:53:27 -0700185 }
186
Sunny Goyal0e08f162015-05-12 11:32:39 -0700187 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700188 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700189 // We don't need to check the info since it's not a ShortcutInfo
190 super.setTag(info);
191
192 // Verify high res immediately
193 verifyHighRes();
194 }
195
Sunny Goyalf4204382016-07-13 10:46:07 -0700196 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800197 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800198 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700199 setIcon(iconDrawable);
200 setText(info.title);
201 if (info.contentDescription != null) {
202 setContentDescription(info.isDisabled()
203 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
204 : info.contentDescription);
205 }
206 }
207
Winson Chung7501adf2015-06-02 11:24:28 -0700208 /**
209 * Overrides the default long press timeout.
210 */
211 public void setLongPressTimeout(int longPressTimeout) {
212 mLongPressHelper.setLongPressTimeout(longPressTimeout);
213 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 @Override
216 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700217 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 mBackgroundSizeChanged = true;
219 }
220 return super.setFrame(left, top, right, bottom);
221 }
222
223 @Override
224 protected boolean verifyDrawable(Drawable who) {
225 return who == mBackground || super.verifyDrawable(who);
226 }
227
228 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700229 public void setTag(Object tag) {
230 if (tag != null) {
231 LauncherModel.checkItemInfo((ItemInfo) tag);
232 }
233 super.setTag(tag);
234 }
235
236 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700237 public void setPressed(boolean pressed) {
238 super.setPressed(pressed);
239
240 if (!mIgnorePressedStateChange) {
241 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800242 }
Sunny Goyal508da152014-08-14 10:53:27 -0700243 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800244
Winson Chungb745afb2015-03-02 11:51:23 -0800245 /** Returns the icon for this view. */
246 public Drawable getIcon() {
247 return mIcon;
248 }
249
250 /** Returns whether the layout is horizontal. */
251 public boolean isLayoutHorizontal() {
252 return mLayoutHorizontal;
253 }
254
Sunny Goyal508da152014-08-14 10:53:27 -0700255 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800256 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700257 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Tony Wickham6b910a22016-11-08 10:40:34 -0800258 if (isPressed() || mStayPressed) {
Kenny Guy44cba692016-01-21 19:50:02 +0000259 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700260 } else {
261 d.animateState(FastBitmapDrawable.State.NORMAL);
262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800264 }
265
266 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700267 public void setOnLongClickListener(OnLongClickListener l) {
268 super.setOnLongClickListener(l);
269 mOnLongClickListener = l;
270 }
271
272 public OnLongClickListener getOnLongClickListener() {
273 return mOnLongClickListener;
274 }
275
276 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800277 public boolean onTouchEvent(MotionEvent event) {
278 // Call the superclass onTouchEvent first, because sometimes it changes the state to
279 // isPressed() on an ACTION_UP
280 boolean result = super.onTouchEvent(event);
281
Mady Melloref044dd2015-06-02 15:35:07 -0700282 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700283 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700284 mLongPressHelper.cancelLongPress();
285 result = true;
286 }
287
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800288 switch (event.getAction()) {
289 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700290 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800291 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
292 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700293 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700294 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800295 }
Winson Chung88f33452012-02-23 15:23:44 -0800296
Mady Melloref044dd2015-06-02 15:35:07 -0700297 // If we're in a stylus button press, don't check for long press.
298 if (!mStylusEventHelper.inStylusButtonPressed()) {
299 mLongPressHelper.postCheckForLongPress();
300 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800301 break;
302 case MotionEvent.ACTION_CANCEL:
303 case MotionEvent.ACTION_UP:
304 // If we've touched down and up on an item, and it's still not "pressed", then
305 // destroy the pressed outline
306 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700307 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800308 }
Winson Chung88f33452012-02-23 15:23:44 -0800309
310 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800311 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400312 case MotionEvent.ACTION_MOVE:
313 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
314 mLongPressHelper.cancelLongPress();
315 }
316 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800317 }
318 return result;
319 }
320
Michael Jurkaddd62e92011-02-16 17:49:14 -0800321 void setStayPressed(boolean stayPressed) {
322 mStayPressed = stayPressed;
323 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700324 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700325 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700326 } else {
327 if (mPressedBackground == null) {
328 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
329 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800330 }
Sunny Goyal508da152014-08-14 10:53:27 -0700331
332 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700333 ViewParent parent = getParent();
334 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
335 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
336 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700337 }
338
339 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800340 }
341
Sunny Goyal508da152014-08-14 10:53:27 -0700342 void clearPressedBackground() {
343 setPressed(false);
344 setStayPressed(false);
345 }
346
347 @Override
348 public boolean onKeyDown(int keyCode, KeyEvent event) {
349 if (super.onKeyDown(keyCode, event)) {
350 // Pre-create shadow so show immediately on click.
351 if (mPressedBackground == null) {
352 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700353 }
Sunny Goyal508da152014-08-14 10:53:27 -0700354 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700355 }
Sunny Goyal508da152014-08-14 10:53:27 -0700356 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800357 }
358
Sunny Goyal508da152014-08-14 10:53:27 -0700359 @Override
360 public boolean onKeyUp(int keyCode, KeyEvent event) {
361 // Unlike touch events, keypress event propagate pressed state change immediately,
362 // without waiting for onClickHandler to execute. Disable pressed state changes here
363 // to avoid flickering.
364 mIgnorePressedStateChange = true;
365 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700366
Sunny Goyal508da152014-08-14 10:53:27 -0700367 mPressedBackground = null;
368 mIgnorePressedStateChange = false;
369 updateIconState();
370 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800371 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 @Override
374 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700375 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700376 super.draw(canvas);
377 return;
378 }
379
Michael Jurkabdb5c532011-02-01 15:05:06 -0800380 final Drawable background = mBackground;
381 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700382 final int scrollX = getScrollX();
383 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800384
Michael Jurkabdb5c532011-02-01 15:05:06 -0800385 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700386 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800387 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800389
390 if ((scrollX | scrollY) == 0) {
391 background.draw(canvas);
392 } else {
393 canvas.translate(scrollX, scrollY);
394 background.draw(canvas);
395 canvas.translate(-scrollX, -scrollY);
396 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800398
399 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800400 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800401 getPaint().clearShadowLayer();
402 super.draw(canvas);
403 return;
404 }
405
Michael Jurkabdb5c532011-02-01 15:05:06 -0800406 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700407 float density = getResources().getDisplayMetrics().density;
408 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800409 super.draw(canvas);
410 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700411 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
412 getScrollX() + getWidth(),
413 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700414 getPaint().setShadowLayer(
415 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800416 super.draw(canvas);
417 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419
420 @Override
421 protected void onAttachedToWindow() {
422 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700423
Winson Chung656d11c2010-11-29 17:15:47 -0800424 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700425
Winson Chungb745afb2015-03-02 11:51:23 -0800426 if (mIcon instanceof PreloadIconDrawable) {
427 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700428 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400429 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 }
431
432 @Override
Winson1f064272016-07-18 17:18:02 -0700433 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
434 if (mCenterVertically) {
435 Paint.FontMetrics fm = getPaint().getFontMetrics();
436 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
437 (int) Math.ceil(fm.bottom - fm.top);
438 int height = MeasureSpec.getSize(heightMeasureSpec);
439 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
440 getPaddingBottom());
441 }
442 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
443 }
444
445 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400446 protected void onDetachedFromWindow() {
447 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800448 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700450
Adam Cohen477828c2013-09-20 12:05:49 -0700451 @Override
452 public void setTextColor(int color) {
453 mTextColor = color;
454 super.setTextColor(color);
455 }
456
Adam Cohen96bb7982014-07-07 11:58:56 -0700457 @Override
458 public void setTextColor(ColorStateList colors) {
459 mTextColor = colors.getDefaultColor();
460 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700461 }
462
Winson Chung5f8afe62013-08-12 16:19:28 -0700463 public void setTextVisibility(boolean visible) {
464 Resources res = getResources();
465 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700466 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700467 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700468 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700469 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700470 }
471
Winson Chungaffd7b42010-08-20 15:11:56 -0700472 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800473 public void cancelLongPress() {
474 super.cancelLongPress();
475
476 mLongPressHelper.cancelLongPress();
477 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500478
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800479 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700480 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400481 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700482 final boolean isPromise = info.isPromise();
483 final int progressLevel = isPromise ?
484 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
485 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700486
Sunny Goyalc469aad2015-10-01 11:24:23 -0700487 setContentDescription(progressLevel > 0 ?
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800488 getContext().getString(R.string.app_downloading_title, info.title,
489 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
Sunny Goyalc469aad2015-10-01 11:24:23 -0700490 getContext().getString(R.string.app_waiting_download_title, info.title));
491
Winson Chungb745afb2015-03-02 11:51:23 -0800492 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700493 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800494 if (mIcon instanceof PreloadIconDrawable) {
495 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700496 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800497 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700498 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700499 }
500
501 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700502 if (promiseStateChanged) {
503 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700504 }
Sunny Goyale755d462014-07-22 13:48:29 -0700505 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400506 }
507 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700508
Tony Wickham010d2552017-01-20 08:15:28 -0800509 public void applyBadgeState(ItemInfo itemInfo) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800510 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham010d2552017-01-20 08:15:28 -0800511 BadgeInfo badgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800512 BadgeRenderer badgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
513 ((FastBitmapDrawable) mIcon).applyIconBadge(badgeInfo, badgeRenderer);
514 }
515 }
516
Sunny Goyal95abbb32014-08-04 10:53:22 -0700517 private Theme getPreloaderTheme() {
518 Object tag = getTag();
519 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
520 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
521 : R.style.PreloadIcon;
522 Theme theme = sPreloaderThemes.get(style);
523 if (theme == null) {
524 theme = getResources().newTheme();
525 theme.applyStyle(style, true);
526 sPreloaderThemes.put(style, theme);
527 }
528 return theme;
529 }
Winson Chungb745afb2015-03-02 11:51:23 -0800530
531 /**
532 * Sets the icon for this view based on the layout direction.
533 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700534 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800535 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800536 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700537 applyCompoundDrawables(mIcon);
538 }
539
540 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800541 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800542 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800543 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700544 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800545 }
Winson Chungb745afb2015-03-02 11:51:23 -0800546 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700547
Sunny Goyal69b75642015-05-15 17:00:24 -0700548 @Override
549 public void requestLayout() {
550 if (!mDisableRelayout) {
551 super.requestLayout();
552 }
553 }
554
Sunny Goyal34b65272015-03-11 16:56:52 -0700555 /**
556 * Applies the item info if it is same as what the view is pointing to currently.
557 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800558 @Override
559 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700560 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700561 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
562 if (mIcon instanceof FastBitmapDrawable) {
563 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
564 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700565 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700566 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700567
Sunny Goyal34b65272015-03-11 16:56:52 -0700568 if (info instanceof AppInfo) {
569 applyFromApplicationInfo((AppInfo) info);
570 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800571 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal317698b2015-07-29 11:45:41 -0700572 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
573 View folderIcon =
574 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
575 if (folderIcon != null) {
576 folderIcon.invalidate();
577 }
578 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700579 } else if (info instanceof PackageItemInfo) {
580 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700581 }
Winsonc0880492015-08-21 11:16:27 -0700582
583 // If we are reapplying over an old icon, then we should update the new icon to the same
584 // state as the old icon
585 if (mIcon instanceof FastBitmapDrawable) {
586 ((FastBitmapDrawable) mIcon).setState(prevState);
587 }
588
Sunny Goyal69b75642015-05-15 17:00:24 -0700589 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700590 }
591 }
592
593 /**
594 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
595 */
596 public void verifyHighRes() {
597 if (mIconLoadRequest != null) {
598 mIconLoadRequest.cancel();
599 mIconLoadRequest = null;
600 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800601 if (getTag() instanceof ItemInfoWithIcon) {
602 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700603 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800604 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700605 .updateIconInBackground(BubbleTextView.this, info);
606 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700607 }
608 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700609
Winson Chungb1777442015-06-16 13:35:04 -0700610 @Override
Winsonc0880492015-08-21 11:16:27 -0700611 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
612 // We can only set the fast scroll focus state on a FastBitmapDrawable
613 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700614 return;
615 }
616
Winsonc0880492015-08-21 11:16:27 -0700617 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
618 if (animated) {
619 FastBitmapDrawable.State prevState = d.getCurrentState();
620 if (d.animateState(focusState)) {
621 // If the state was updated, then update the view accordingly
622 animate().scaleX(focusState.viewScale)
623 .scaleY(focusState.viewScale)
624 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
625 .setDuration(d.getDurationForStateChange(prevState, focusState))
626 .start();
627 }
628 } else {
629 if (d.setState(focusState)) {
630 // If the state was updated, then update the view accordingly
631 animate().cancel();
632 setScaleX(focusState.viewScale);
633 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700634 }
635 }
636 }
637
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700638 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700639 * Returns true if the view can show custom shortcuts.
640 */
641 public boolean hasDeepShortcuts() {
Tony Wickham010d2552017-01-20 08:15:28 -0800642 return !mLauncher.getPopupDataProvider().getShortcutIdsForItem((ItemInfo) getTag())
643 .isEmpty();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700644 }
645
646 /**
Winsonc0880492015-08-21 11:16:27 -0700647 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
648 */
649 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
650 final FastBitmapDrawable.State toState) {
651 switch (toState) {
652 case NORMAL:
653 switch (fromState) {
654 case FAST_SCROLL_HIGHLIGHTED:
655 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
656 }
657 }
658 return 0;
659 }
660
661 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700662 * Interface to be implemented by the grand parent to allow click shadow effect.
663 */
Winsonc0880492015-08-21 11:16:27 -0700664 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700665 void setPressedIcon(BubbleTextView icon, Bitmap background);
666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667}