blob: 47a5b4fbdca347cba13441cde3c88a6477eee4f0 [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;
44import com.android.launcher3.badging.NotificationInfo;
Sunny Goyal26119432016-02-18 22:09:23 +000045import com.android.launcher3.folder.FolderIcon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080046import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070047import com.android.launcher3.graphics.HolographicOutlineHelper;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070048import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070049
Sunny Goyalc469aad2015-10-01 11:24:23 -070050import java.text.NumberFormat;
Tony Wickham010d2552017-01-20 08:15:28 -080051import java.util.List;
Sunny Goyalc469aad2015-10-01 11:24:23 -070052
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053/**
54 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
55 * because we want to make the bubble taller than the text and TextView's clip is
56 * too aggressive.
57 */
Winson Chungb1777442015-06-16 13:35:04 -070058public class BubbleTextView extends TextView
Sunny Goyal2d7cca12017-01-03 16:52:43 -080059 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView, ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070060
Sameer Padala8fd74832014-09-08 16:00:29 -070061 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070062
Sunny Goyal8f8f3982016-07-25 17:08:38 -070063 // Dimensions in DP
64 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
65 private static final float KEY_SHADOW_RADIUS = 1f;
66 private static final float KEY_SHADOW_OFFSET = 0.5f;
67 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
68 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080069
Sunny Goyaldfaccf62015-05-11 16:30:44 -070070 private static final int DISPLAY_WORKSPACE = 0;
71 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070072 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070073
Sunny Goyal53d7ee42015-05-22 12:25:45 -070074 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080075 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070076 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080077 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070078 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079 private final CheckLongPressHelper mLongPressHelper;
80 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070081 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080082
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080083 private boolean mBackgroundSizeChanged;
84
Sunny Goyal508da152014-08-14 10:53:27 -070085 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080086
Jason Monk02dd7ae2014-04-15 15:23:31 -040087 private float mSlop;
88
Winson Chung93f98ea2015-03-10 16:28:47 -070089 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070090 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080091 private final boolean mLayoutHorizontal;
92 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080093 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080094 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070095
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080097 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080098 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070099 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800100 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700101 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500102
Sunny Goyal34b65272015-03-11 16:56:52 -0700103 private IconLoadRequest mIconLoadRequest;
104
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700106 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 }
108
109 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700110 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 }
112
113 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
114 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700115 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700116 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700117
Adam Cohen96bb7982014-07-07 11:58:56 -0700118 TypedArray a = context.obtainStyledAttributes(attrs,
119 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700120 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800121 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700122 mDeferShadowGenerationOnTouch =
123 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700124
125 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
126 int defaultIconSize = grid.iconSizePx;
127 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700128 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700129 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700130 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
131 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700132 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700133 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700134 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700135 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700136 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700137 }
Winson1f064272016-07-18 17:18:02 -0700138 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700139
140 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
141 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700142 a.recycle();
143
Sunny Goyal15872da2014-07-08 15:43:54 -0700144 if (mCustomShadowsEnabled) {
145 // Draw the background itself as the parent is drawn twice.
146 mBackground = getBackground();
147 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700148
149 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
150 float density = getResources().getDisplayMetrics().density;
151 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700152 } else {
153 mBackground = null;
154 }
Winson Chungb745afb2015-03-02 11:51:23 -0800155
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800156 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700157 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800158
Sunny Goyal10629b02016-09-01 12:50:11 -0700159 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700160 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 }
162
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800163 public void applyFromShortcutInfo(ShortcutInfo info) {
164 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700165 }
166
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800167 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
168 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800169 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700170 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800171 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500172 }
Tony Wickham010d2552017-01-20 08:15:28 -0800173
174 applyBadgeState(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800175 }
176
Sunny Goyal508da152014-08-14 10:53:27 -0700177 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700178 applyIconAndLabel(info.iconBitmap, info);
179
Winson Chung888b3a12015-03-13 11:14:16 -0700180 // We don't need to check the info since it's not a ShortcutInfo
181 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700182
183 // Verify high res immediately
184 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800185
186 applyBadgeState(info);
Sunny Goyal508da152014-08-14 10:53:27 -0700187 }
188
Sunny Goyal0e08f162015-05-12 11:32:39 -0700189 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700190 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700191 // We don't need to check the info since it's not a ShortcutInfo
192 super.setTag(info);
193
194 // Verify high res immediately
195 verifyHighRes();
196 }
197
Sunny Goyalf4204382016-07-13 10:46:07 -0700198 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800199 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800200 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700201 setIcon(iconDrawable);
202 setText(info.title);
203 if (info.contentDescription != null) {
204 setContentDescription(info.isDisabled()
205 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
206 : info.contentDescription);
207 }
208 }
209
Winson Chung7501adf2015-06-02 11:24:28 -0700210 /**
211 * Overrides the default long press timeout.
212 */
213 public void setLongPressTimeout(int longPressTimeout) {
214 mLongPressHelper.setLongPressTimeout(longPressTimeout);
215 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 @Override
218 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700219 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 mBackgroundSizeChanged = true;
221 }
222 return super.setFrame(left, top, right, bottom);
223 }
224
225 @Override
226 protected boolean verifyDrawable(Drawable who) {
227 return who == mBackground || super.verifyDrawable(who);
228 }
229
230 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700231 public void setTag(Object tag) {
232 if (tag != null) {
233 LauncherModel.checkItemInfo((ItemInfo) tag);
234 }
235 super.setTag(tag);
236 }
237
238 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700239 public void setPressed(boolean pressed) {
240 super.setPressed(pressed);
241
242 if (!mIgnorePressedStateChange) {
243 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800244 }
Sunny Goyal508da152014-08-14 10:53:27 -0700245 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800246
Winson Chungb745afb2015-03-02 11:51:23 -0800247 /** Returns the icon for this view. */
248 public Drawable getIcon() {
249 return mIcon;
250 }
251
252 /** Returns whether the layout is horizontal. */
253 public boolean isLayoutHorizontal() {
254 return mLayoutHorizontal;
255 }
256
Sunny Goyal508da152014-08-14 10:53:27 -0700257 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800258 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700259 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Tony Wickham6b910a22016-11-08 10:40:34 -0800260 if (isPressed() || mStayPressed) {
Kenny Guy44cba692016-01-21 19:50:02 +0000261 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700262 } else {
263 d.animateState(FastBitmapDrawable.State.NORMAL);
264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800266 }
267
268 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700269 public void setOnLongClickListener(OnLongClickListener l) {
270 super.setOnLongClickListener(l);
271 mOnLongClickListener = l;
272 }
273
274 public OnLongClickListener getOnLongClickListener() {
275 return mOnLongClickListener;
276 }
277
278 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800279 public boolean onTouchEvent(MotionEvent event) {
280 // Call the superclass onTouchEvent first, because sometimes it changes the state to
281 // isPressed() on an ACTION_UP
282 boolean result = super.onTouchEvent(event);
283
Mady Melloref044dd2015-06-02 15:35:07 -0700284 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700285 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700286 mLongPressHelper.cancelLongPress();
287 result = true;
288 }
289
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800290 switch (event.getAction()) {
291 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700292 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800293 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
294 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700295 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700296 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800297 }
Winson Chung88f33452012-02-23 15:23:44 -0800298
Mady Melloref044dd2015-06-02 15:35:07 -0700299 // If we're in a stylus button press, don't check for long press.
300 if (!mStylusEventHelper.inStylusButtonPressed()) {
301 mLongPressHelper.postCheckForLongPress();
302 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800303 break;
304 case MotionEvent.ACTION_CANCEL:
305 case MotionEvent.ACTION_UP:
306 // If we've touched down and up on an item, and it's still not "pressed", then
307 // destroy the pressed outline
308 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700309 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800310 }
Winson Chung88f33452012-02-23 15:23:44 -0800311
312 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800313 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400314 case MotionEvent.ACTION_MOVE:
315 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
316 mLongPressHelper.cancelLongPress();
317 }
318 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800319 }
320 return result;
321 }
322
Michael Jurkaddd62e92011-02-16 17:49:14 -0800323 void setStayPressed(boolean stayPressed) {
324 mStayPressed = stayPressed;
325 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700326 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700327 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700328 } else {
329 if (mPressedBackground == null) {
330 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
331 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800332 }
Sunny Goyal508da152014-08-14 10:53:27 -0700333
334 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700335 ViewParent parent = getParent();
336 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
337 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
338 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700339 }
340
341 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800342 }
343
Sunny Goyal508da152014-08-14 10:53:27 -0700344 void clearPressedBackground() {
345 setPressed(false);
346 setStayPressed(false);
347 }
348
349 @Override
350 public boolean onKeyDown(int keyCode, KeyEvent event) {
351 if (super.onKeyDown(keyCode, event)) {
352 // Pre-create shadow so show immediately on click.
353 if (mPressedBackground == null) {
354 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700355 }
Sunny Goyal508da152014-08-14 10:53:27 -0700356 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700357 }
Sunny Goyal508da152014-08-14 10:53:27 -0700358 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800359 }
360
Sunny Goyal508da152014-08-14 10:53:27 -0700361 @Override
362 public boolean onKeyUp(int keyCode, KeyEvent event) {
363 // Unlike touch events, keypress event propagate pressed state change immediately,
364 // without waiting for onClickHandler to execute. Disable pressed state changes here
365 // to avoid flickering.
366 mIgnorePressedStateChange = true;
367 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700368
Sunny Goyal508da152014-08-14 10:53:27 -0700369 mPressedBackground = null;
370 mIgnorePressedStateChange = false;
371 updateIconState();
372 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800373 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 @Override
376 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700377 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700378 super.draw(canvas);
379 return;
380 }
381
Michael Jurkabdb5c532011-02-01 15:05:06 -0800382 final Drawable background = mBackground;
383 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700384 final int scrollX = getScrollX();
385 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800386
Michael Jurkabdb5c532011-02-01 15:05:06 -0800387 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700388 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800389 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800391
392 if ((scrollX | scrollY) == 0) {
393 background.draw(canvas);
394 } else {
395 canvas.translate(scrollX, scrollY);
396 background.draw(canvas);
397 canvas.translate(-scrollX, -scrollY);
398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800400
401 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800402 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800403 getPaint().clearShadowLayer();
404 super.draw(canvas);
405 return;
406 }
407
Michael Jurkabdb5c532011-02-01 15:05:06 -0800408 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700409 float density = getResources().getDisplayMetrics().density;
410 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800411 super.draw(canvas);
412 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700413 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
414 getScrollX() + getWidth(),
415 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700416 getPaint().setShadowLayer(
417 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800418 super.draw(canvas);
419 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400421
422 @Override
423 protected void onAttachedToWindow() {
424 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700425
Winson Chung656d11c2010-11-29 17:15:47 -0800426 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700427
Winson Chungb745afb2015-03-02 11:51:23 -0800428 if (mIcon instanceof PreloadIconDrawable) {
429 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700430 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400431 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432 }
433
434 @Override
Winson1f064272016-07-18 17:18:02 -0700435 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
436 if (mCenterVertically) {
437 Paint.FontMetrics fm = getPaint().getFontMetrics();
438 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
439 (int) Math.ceil(fm.bottom - fm.top);
440 int height = MeasureSpec.getSize(heightMeasureSpec);
441 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
442 getPaddingBottom());
443 }
444 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
445 }
446
447 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400448 protected void onDetachedFromWindow() {
449 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800450 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700452
Adam Cohen477828c2013-09-20 12:05:49 -0700453 @Override
454 public void setTextColor(int color) {
455 mTextColor = color;
456 super.setTextColor(color);
457 }
458
Adam Cohen96bb7982014-07-07 11:58:56 -0700459 @Override
460 public void setTextColor(ColorStateList colors) {
461 mTextColor = colors.getDefaultColor();
462 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700463 }
464
Winson Chung5f8afe62013-08-12 16:19:28 -0700465 public void setTextVisibility(boolean visible) {
466 Resources res = getResources();
467 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700468 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700469 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700470 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700471 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700472 }
473
Winson Chungaffd7b42010-08-20 15:11:56 -0700474 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800475 public void cancelLongPress() {
476 super.cancelLongPress();
477
478 mLongPressHelper.cancelLongPress();
479 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500480
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800481 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700482 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400483 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700484 final boolean isPromise = info.isPromise();
485 final int progressLevel = isPromise ?
486 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
487 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700488
Sunny Goyalc469aad2015-10-01 11:24:23 -0700489 setContentDescription(progressLevel > 0 ?
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800490 getContext().getString(R.string.app_downloading_title, info.title,
491 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
Sunny Goyalc469aad2015-10-01 11:24:23 -0700492 getContext().getString(R.string.app_waiting_download_title, info.title));
493
Winson Chungb745afb2015-03-02 11:51:23 -0800494 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700495 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800496 if (mIcon instanceof PreloadIconDrawable) {
497 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700498 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800499 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700500 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700501 }
502
503 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700504 if (promiseStateChanged) {
505 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700506 }
Sunny Goyale755d462014-07-22 13:48:29 -0700507 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400508 }
509 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700510
Tony Wickham010d2552017-01-20 08:15:28 -0800511 public void applyBadgeState(ItemInfo itemInfo) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800512 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham010d2552017-01-20 08:15:28 -0800513 BadgeInfo badgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800514 BadgeRenderer badgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
515 ((FastBitmapDrawable) mIcon).applyIconBadge(badgeInfo, badgeRenderer);
516 }
517 }
518
Sunny Goyal95abbb32014-08-04 10:53:22 -0700519 private Theme getPreloaderTheme() {
520 Object tag = getTag();
521 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
522 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
523 : R.style.PreloadIcon;
524 Theme theme = sPreloaderThemes.get(style);
525 if (theme == null) {
526 theme = getResources().newTheme();
527 theme.applyStyle(style, true);
528 sPreloaderThemes.put(style, theme);
529 }
530 return theme;
531 }
Winson Chungb745afb2015-03-02 11:51:23 -0800532
533 /**
534 * Sets the icon for this view based on the layout direction.
535 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700536 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800537 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800538 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700539 applyCompoundDrawables(mIcon);
540 }
541
542 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800543 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800544 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800545 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700546 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800547 }
Winson Chungb745afb2015-03-02 11:51:23 -0800548 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700549
Sunny Goyal69b75642015-05-15 17:00:24 -0700550 @Override
551 public void requestLayout() {
552 if (!mDisableRelayout) {
553 super.requestLayout();
554 }
555 }
556
Sunny Goyal34b65272015-03-11 16:56:52 -0700557 /**
558 * Applies the item info if it is same as what the view is pointing to currently.
559 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800560 @Override
561 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700562 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700563 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
564 if (mIcon instanceof FastBitmapDrawable) {
565 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
566 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700567 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700568 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700569
Sunny Goyal34b65272015-03-11 16:56:52 -0700570 if (info instanceof AppInfo) {
571 applyFromApplicationInfo((AppInfo) info);
572 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800573 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal317698b2015-07-29 11:45:41 -0700574 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
575 View folderIcon =
576 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
577 if (folderIcon != null) {
578 folderIcon.invalidate();
579 }
580 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700581 } else if (info instanceof PackageItemInfo) {
582 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700583 }
Winsonc0880492015-08-21 11:16:27 -0700584
585 // If we are reapplying over an old icon, then we should update the new icon to the same
586 // state as the old icon
587 if (mIcon instanceof FastBitmapDrawable) {
588 ((FastBitmapDrawable) mIcon).setState(prevState);
589 }
590
Sunny Goyal69b75642015-05-15 17:00:24 -0700591 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700592 }
593 }
594
595 /**
596 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
597 */
598 public void verifyHighRes() {
599 if (mIconLoadRequest != null) {
600 mIconLoadRequest.cancel();
601 mIconLoadRequest = null;
602 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800603 if (getTag() instanceof ItemInfoWithIcon) {
604 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700605 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800606 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700607 .updateIconInBackground(BubbleTextView.this, info);
608 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700609 }
610 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700611
Winson Chungb1777442015-06-16 13:35:04 -0700612 @Override
Winsonc0880492015-08-21 11:16:27 -0700613 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
614 // We can only set the fast scroll focus state on a FastBitmapDrawable
615 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700616 return;
617 }
618
Winsonc0880492015-08-21 11:16:27 -0700619 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
620 if (animated) {
621 FastBitmapDrawable.State prevState = d.getCurrentState();
622 if (d.animateState(focusState)) {
623 // If the state was updated, then update the view accordingly
624 animate().scaleX(focusState.viewScale)
625 .scaleY(focusState.viewScale)
626 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
627 .setDuration(d.getDurationForStateChange(prevState, focusState))
628 .start();
629 }
630 } else {
631 if (d.setState(focusState)) {
632 // If the state was updated, then update the view accordingly
633 animate().cancel();
634 setScaleX(focusState.viewScale);
635 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700636 }
637 }
638 }
639
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700640 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700641 * Returns true if the view can show custom shortcuts.
642 */
643 public boolean hasDeepShortcuts() {
Tony Wickham010d2552017-01-20 08:15:28 -0800644 return !mLauncher.getPopupDataProvider().getShortcutIdsForItem((ItemInfo) getTag())
645 .isEmpty();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700646 }
647
648 /**
Winsonc0880492015-08-21 11:16:27 -0700649 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
650 */
651 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
652 final FastBitmapDrawable.State toState) {
653 switch (toState) {
654 case NORMAL:
655 switch (fromState) {
656 case FAST_SCROLL_HIGHLIGHTED:
657 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
658 }
659 }
660 return 0;
661 }
662
663 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700664 * Interface to be implemented by the grand parent to allow click shadow effect.
665 */
Winsonc0880492015-08-21 11:16:27 -0700666 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700667 void setPressedIcon(BubbleTextView icon, Bitmap background);
668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669}