blob: 5bdc4ed2a790297b000546738e96dbf9489d4422 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Tony Wickham1237df02017-02-24 08:59:36 -080019import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070021import android.content.res.ColorStateList;
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;
Tony17b7f9b2017-05-23 12:19:09 -070025import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070026import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080027import android.graphics.Point;
28import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070029import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.drawable.Drawable;
Tony8f402802017-06-16 17:24:54 -070031import android.support.v4.graphics.ColorUtils;
Winson Chung656d11c2010-11-29 17:15:47 -080032import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080033import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070034import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070035import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080036import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070037import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040038import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080039import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070040import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080041import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070042
Sunny Goyal34b65272015-03-11 16:56:52 -070043import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080044import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080045import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080046import com.android.launcher3.badge.BadgeRenderer;
Tony7308cde2017-06-27 22:38:33 -070047import com.android.launcher3.folder.FolderIcon;
Jon Miranda529af302017-02-28 14:18:54 -080048import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080049import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070050import com.android.launcher3.graphics.HolographicOutlineHelper;
Tony Wickham1237df02017-02-24 08:59:36 -080051import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080052import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070053import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070054
Sunny Goyalc469aad2015-10-01 11:24:23 -070055import java.text.NumberFormat;
56
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057/**
58 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
59 * because we want to make the bubble taller than the text and TextView's clip is
60 * too aggressive.
61 */
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080062public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070063
Sunny Goyaldfaccf62015-05-11 16:30:44 -070064 private static final int DISPLAY_WORKSPACE = 0;
65 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070066 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070067
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080068 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
69
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 Goyal4e5a8782017-06-23 09:34:06 -070073
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080074 private final CheckLongPressHelper mLongPressHelper;
75 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070076 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal9314b7c2017-06-23 10:36:27 -070077 private final float mSlop;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078
Sunny Goyal508da152014-08-14 10:53:27 -070079 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080080
Winson Chung93f98ea2015-03-10 16:28:47 -070081 private final boolean mDeferShadowGenerationOnTouch;
Winson Chungb745afb2015-03-02 11:51:23 -080082 private final boolean mLayoutHorizontal;
83 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080084 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080085 private int mTextColor;
Tony17b7f9b2017-05-23 12:19:09 -070086 private boolean mIsIconVisible = true;
Winson Chung5f8afe62013-08-12 16:19:28 -070087
Tony Wickham1237df02017-02-24 08:59:36 -080088 private BadgeInfo mBadgeInfo;
89 private BadgeRenderer mBadgeRenderer;
Tony Wickham7092db02017-06-07 14:32:23 -070090 private IconPalette mBadgePalette;
Tony Wickham1237df02017-02-24 08:59:36 -080091 private float mBadgeScale;
92 private boolean mForceHideBadge;
93 private Point mTempSpaceForBadgeOffset = new Point();
94 private Rect mTempIconBounds = new Rect();
95
96 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
97 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
98 @Override
99 public Float get(BubbleTextView bubbleTextView) {
100 return bubbleTextView.mBadgeScale;
101 }
102
103 @Override
104 public void set(BubbleTextView bubbleTextView, Float value) {
105 bubbleTextView.mBadgeScale = value;
106 bubbleTextView.invalidate();
107 }
108 };
109
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700110 public static final Property<BubbleTextView, Integer> TEXT_ALPHA_PROPERTY
Tony8f402802017-06-16 17:24:54 -0700111 = new Property<BubbleTextView, Integer>(Integer.class, "textAlpha") {
112 @Override
113 public Integer get(BubbleTextView bubbleTextView) {
114 return bubbleTextView.getTextAlpha();
115 }
116
117 @Override
118 public void set(BubbleTextView bubbleTextView, Integer alpha) {
119 bubbleTextView.setTextAlpha(alpha);
120 }
121 };
122
Sunny Goyal4ffec482016-02-09 11:28:52 -0800123 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800124 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800125 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700126 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800127 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700128 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500129
Sunny Goyal34b65272015-03-11 16:56:52 -0700130 private IconLoadRequest mIconLoadRequest;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700133 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 }
135
136 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700137 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 }
139
140 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
141 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700142 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700143 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700144 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700145
Adam Cohen96bb7982014-07-07 11:58:56 -0700146 TypedArray a = context.obtainStyledAttributes(attrs,
147 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800148 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700149 mDeferShadowGenerationOnTouch =
150 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700151
152 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
153 int defaultIconSize = grid.iconSizePx;
154 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700155 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700156 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700157 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700158 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
159 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700160 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700161 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700162 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700163 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700164 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700165 }
Winson1f064272016-07-18 17:18:02 -0700166 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700167
168 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
169 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700170 a.recycle();
171
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800172 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700173 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800174
Sunny Goyal10629b02016-09-01 12:50:11 -0700175 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700176 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 }
179
Jon Miranda52549442017-10-26 11:28:06 -0700180 /**
181 * Resets the view so it can be recycled.
182 */
183 public void reset() {
184 mBadgeInfo = null;
185 mBadgePalette = null;
Jon Miranda48043ee2017-11-01 15:40:22 -0700186 mBadgeScale = 0f;
Jon Miranda52549442017-10-26 11:28:06 -0700187 mForceHideBadge = false;
188 }
189
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800190 public void applyFromShortcutInfo(ShortcutInfo info) {
191 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700192 }
193
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800194 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
195 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800196 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700197 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800198 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500199 }
Tony Wickham010d2552017-01-20 08:15:28 -0800200
Tony Wickham1e618492017-02-02 12:57:18 -0800201 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800202 }
203
Sunny Goyal508da152014-08-14 10:53:27 -0700204 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700205 applyIconAndLabel(info.iconBitmap, info);
206
Winson Chung888b3a12015-03-13 11:14:16 -0700207 // We don't need to check the info since it's not a ShortcutInfo
208 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700209
210 // Verify high res immediately
211 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800212
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700213 if (info instanceof PromiseAppInfo) {
214 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
215 applyProgressLevel(promiseAppInfo.level);
216 }
Tony Wickham1e618492017-02-02 12:57:18 -0800217 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700218 }
219
Sunny Goyal0e08f162015-05-12 11:32:39 -0700220 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700221 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700222 // We don't need to check the info since it's not a ShortcutInfo
223 super.setTag(info);
224
225 // Verify high res immediately
226 verifyHighRes();
227 }
228
Sunny Goyalf4204382016-07-13 10:46:07 -0700229 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800230 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800231 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700232 setIcon(iconDrawable);
233 setText(info.title);
234 if (info.contentDescription != null) {
235 setContentDescription(info.isDisabled()
236 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
237 : info.contentDescription);
238 }
239 }
240
Winson Chung7501adf2015-06-02 11:24:28 -0700241 /**
242 * Overrides the default long press timeout.
243 */
244 public void setLongPressTimeout(int longPressTimeout) {
245 mLongPressHelper.setLongPressTimeout(longPressTimeout);
246 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700249 public void setTag(Object tag) {
250 if (tag != null) {
251 LauncherModel.checkItemInfo((ItemInfo) tag);
252 }
253 super.setTag(tag);
254 }
255
256 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800257 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700258 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800259 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800260 }
Sunny Goyal508da152014-08-14 10:53:27 -0700261 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800262
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800263 @Override
264 protected int[] onCreateDrawableState(int extraSpace) {
265 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
266 if (mStayPressed) {
267 mergeDrawableStates(drawableState, STATE_PRESSED);
268 }
269 return drawableState;
270 }
271
Winson Chungb745afb2015-03-02 11:51:23 -0800272 /** Returns the icon for this view. */
273 public Drawable getIcon() {
274 return mIcon;
275 }
276
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700277 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800278 public boolean onTouchEvent(MotionEvent event) {
279 // Call the superclass onTouchEvent first, because sometimes it changes the state to
280 // isPressed() on an ACTION_UP
281 boolean result = super.onTouchEvent(event);
282
Mady Melloref044dd2015-06-02 15:35:07 -0700283 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700284 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700285 mLongPressHelper.cancelLongPress();
286 result = true;
287 }
288
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800289 switch (event.getAction()) {
290 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700291 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800292 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
293 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700294 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700295 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800296 }
Winson Chung88f33452012-02-23 15:23:44 -0800297
Mady Melloref044dd2015-06-02 15:35:07 -0700298 // If we're in a stylus button press, don't check for long press.
299 if (!mStylusEventHelper.inStylusButtonPressed()) {
300 mLongPressHelper.postCheckForLongPress();
301 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800302 break;
303 case MotionEvent.ACTION_CANCEL:
304 case MotionEvent.ACTION_UP:
305 // If we've touched down and up on an item, and it's still not "pressed", then
306 // destroy the pressed outline
307 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700308 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800309 }
Winson Chung88f33452012-02-23 15:23:44 -0800310
311 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800312 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400313 case MotionEvent.ACTION_MOVE:
314 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
315 mLongPressHelper.cancelLongPress();
316 }
317 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800318 }
319 return result;
320 }
321
Michael Jurkaddd62e92011-02-16 17:49:14 -0800322 void setStayPressed(boolean stayPressed) {
323 mStayPressed = stayPressed;
324 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700325 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700326 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700327 } else {
328 if (mPressedBackground == null) {
329 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
330 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800331 }
Sunny Goyal508da152014-08-14 10:53:27 -0700332
333 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700334 ViewParent parent = getParent();
335 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
336 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
337 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700338 }
339
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800340 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800341 }
342
Sunny Goyal508da152014-08-14 10:53:27 -0700343 void clearPressedBackground() {
344 setPressed(false);
345 setStayPressed(false);
346 }
347
348 @Override
349 public boolean onKeyDown(int keyCode, KeyEvent event) {
350 if (super.onKeyDown(keyCode, event)) {
351 // Pre-create shadow so show immediately on click.
352 if (mPressedBackground == null) {
353 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700354 }
Sunny Goyal508da152014-08-14 10:53:27 -0700355 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700356 }
Sunny Goyal508da152014-08-14 10:53:27 -0700357 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800358 }
359
Sunny Goyal508da152014-08-14 10:53:27 -0700360 @Override
361 public boolean onKeyUp(int keyCode, KeyEvent event) {
362 // Unlike touch events, keypress event propagate pressed state change immediately,
363 // without waiting for onClickHandler to execute. Disable pressed state changes here
364 // to avoid flickering.
365 mIgnorePressedStateChange = true;
366 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700367
Sunny Goyal508da152014-08-14 10:53:27 -0700368 mPressedBackground = null;
369 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800370 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700371 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800372 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800373
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700374 @SuppressWarnings("wrongcall")
375 protected void drawWithoutBadge(Canvas canvas) {
376 super.onDraw(canvas);
377 }
378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700380 public void onDraw(Canvas canvas) {
381 super.onDraw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800382 drawBadgeIfNecessary(canvas);
383 }
384
385 /**
386 * Draws the icon badge in the top right corner of the icon bounds.
387 * @param canvas The canvas to draw to.
388 */
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700389 protected void drawBadgeIfNecessary(Canvas canvas) {
Tony Wickham1237df02017-02-24 08:59:36 -0800390 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
391 getIconBounds(mTempIconBounds);
392 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
393 final int scrollX = getScrollX();
394 final int scrollY = getScrollY();
395 canvas.translate(scrollX, scrollY);
Tony Wickham7092db02017-06-07 14:32:23 -0700396 mBadgeRenderer.draw(canvas, mBadgePalette, mBadgeInfo, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800397 mTempSpaceForBadgeOffset);
398 canvas.translate(-scrollX, -scrollY);
399 }
400 }
401
402 public void forceHideBadge(boolean forceHideBadge) {
403 if (mForceHideBadge == forceHideBadge) {
404 return;
405 }
406 mForceHideBadge = forceHideBadge;
407
408 if (forceHideBadge) {
409 invalidate();
410 } else if (hasBadge()) {
411 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
412 }
413 }
414
415 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700416 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800417 }
418
419 public void getIconBounds(Rect outBounds) {
420 int top = getPaddingTop();
421 int left = (getWidth() - mIconSize) / 2;
422 int right = left + mIconSize;
423 int bottom = top + mIconSize;
424 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400426
427 @Override
Winson1f064272016-07-18 17:18:02 -0700428 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
429 if (mCenterVertically) {
430 Paint.FontMetrics fm = getPaint().getFontMetrics();
431 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
432 (int) Math.ceil(fm.bottom - fm.top);
433 int height = MeasureSpec.getSize(heightMeasureSpec);
434 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
435 getPaddingBottom());
436 }
437 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
438 }
439
440 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700441 public void setTextColor(int color) {
442 mTextColor = color;
443 super.setTextColor(color);
444 }
445
Adam Cohen96bb7982014-07-07 11:58:56 -0700446 @Override
447 public void setTextColor(ColorStateList colors) {
448 mTextColor = colors.getDefaultColor();
449 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700450 }
451
Tony7308cde2017-06-27 22:38:33 -0700452 public boolean shouldTextBeVisible() {
453 // Text should be visible everywhere but the hotseat.
454 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
455 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
456 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
457 }
458
Winson Chung5f8afe62013-08-12 16:19:28 -0700459 public void setTextVisibility(boolean visible) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700460 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700461 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700462 } else {
Tony8f402802017-06-16 17:24:54 -0700463 setTextAlpha(0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700464 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700465 }
466
Tony8f402802017-06-16 17:24:54 -0700467 private void setTextAlpha(int alpha) {
468 super.setTextColor(ColorUtils.setAlphaComponent(mTextColor, alpha));
469 }
470
471 private int getTextAlpha() {
472 return Color.alpha(getCurrentTextColor());
473 }
474
475 /**
476 * Creates an animator to fade the text in or out.
477 * @param fadeIn Whether the text should fade in or fade out.
478 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700479 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Tony7308cde2017-06-27 22:38:33 -0700480 int toAlpha = shouldTextBeVisible() && fadeIn ? Color.alpha(mTextColor) : 0;
481 return ObjectAnimator.ofInt(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700482 }
483
Winson Chungaffd7b42010-08-20 15:11:56 -0700484 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800485 public void cancelLongPress() {
486 super.cancelLongPress();
487
488 mLongPressHelper.cancelLongPress();
489 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500490
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800491 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700492 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400493 ShortcutInfo info = (ShortcutInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700494 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700495 final int progressLevel = isPromise ?
496 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
497 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700498
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700499 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
500 if (preloadDrawable != null && promiseStateChanged) {
501 preloadDrawable.maybePerformFinishedAnimation();
502 }
503 }
504 }
505
506 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
507 if (getTag() instanceof ItemInfoWithIcon) {
508 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
509 setContentDescription(progressLevel > 0
510 ? getContext().getString(R.string.app_downloading_title, info.title,
511 NumberFormat.getPercentInstance().format(progressLevel * 0.01))
512 : getContext().getString(R.string.app_waiting_download_title, info.title));
Sunny Goyalc469aad2015-10-01 11:24:23 -0700513
Winson Chungb745afb2015-03-02 11:51:23 -0800514 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700515 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800516 if (mIcon instanceof PreloadIconDrawable) {
517 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700518 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700519 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800520 preloadDrawable = DrawableFactory.get(getContext())
521 .newPendingIcon(info.iconBitmap, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700522 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700523 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700524 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700525 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700526 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400527 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700528 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400529 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700530
Tony Wickham1e618492017-02-02 12:57:18 -0800531 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800532 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800533 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700534 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
535 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800536 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700537 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800538 if (wasBadged || isBadged) {
Tony Wickham7092db02017-06-07 14:32:23 -0700539 mBadgePalette = IconPalette.getBadgePalette(getResources());
540 if (mBadgePalette == null) {
541 mBadgePalette = ((FastBitmapDrawable) mIcon).getIconPalette();
542 }
Tony Wickham1237df02017-02-24 08:59:36 -0800543 // Animate when a badge is first added or when it is removed.
544 if (animate && (wasBadged ^ isBadged) && isShown()) {
545 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
546 } else {
547 mBadgeScale = newBadgeScale;
548 invalidate();
549 }
550 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800551 }
552 }
553
Tony Wickham7092db02017-06-07 14:32:23 -0700554 public IconPalette getBadgePalette() {
555 return mBadgePalette;
556 }
557
Winson Chungb745afb2015-03-02 11:51:23 -0800558 /**
559 * Sets the icon for this view based on the layout direction.
560 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700561 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800562 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800563 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700564 if (mIsIconVisible) {
565 applyCompoundDrawables(mIcon);
566 }
567 }
568
569 public void setIconVisible(boolean visible) {
570 mIsIconVisible = visible;
571 mDisableRelayout = true;
572 Drawable icon = mIcon;
573 if (!visible) {
574 icon = new ColorDrawable(Color.TRANSPARENT);
575 icon.setBounds(0, 0, mIconSize, mIconSize);
576 }
577 applyCompoundDrawables(icon);
578 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700579 }
580
581 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800582 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800583 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800584 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700585 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800586 }
Winson Chungb745afb2015-03-02 11:51:23 -0800587 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700588
Sunny Goyal69b75642015-05-15 17:00:24 -0700589 @Override
590 public void requestLayout() {
591 if (!mDisableRelayout) {
592 super.requestLayout();
593 }
594 }
595
Sunny Goyal34b65272015-03-11 16:56:52 -0700596 /**
597 * Applies the item info if it is same as what the view is pointing to currently.
598 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800599 @Override
600 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700601 if (getTag() == info) {
602 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700603 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700604
Jon Miranda7f522a22017-07-28 11:56:47 -0700605 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
606 info.iconBitmap.prepareToDraw();
607
Sunny Goyal34b65272015-03-11 16:56:52 -0700608 if (info instanceof AppInfo) {
609 applyFromApplicationInfo((AppInfo) info);
610 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800611 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800612 FolderIconPreviewVerifier verifier =
613 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
614 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700615 View folderIcon =
616 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
617 if (folderIcon != null) {
618 folderIcon.invalidate();
619 }
620 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700621 } else if (info instanceof PackageItemInfo) {
622 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700623 }
Winsonc0880492015-08-21 11:16:27 -0700624
Sunny Goyal69b75642015-05-15 17:00:24 -0700625 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700626 }
627 }
628
629 /**
630 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
631 */
632 public void verifyHighRes() {
633 if (mIconLoadRequest != null) {
634 mIconLoadRequest.cancel();
635 mIconLoadRequest = null;
636 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800637 if (getTag() instanceof ItemInfoWithIcon) {
638 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700639 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800640 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700641 .updateIconInBackground(BubbleTextView.this, info);
642 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700643 }
644 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700645
Jon Miranda47170112017-03-03 14:57:14 -0800646 public int getIconSize() {
647 return mIconSize;
648 }
649
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700650 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700651 * Interface to be implemented by the grand parent to allow click shadow effect.
652 */
Winsonc0880492015-08-21 11:16:27 -0700653 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700654 void setPressedIcon(BubbleTextView icon, Bitmap background);
655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656}