blob: fc61155d37c1db355f2078cd26e2af5d624e0292 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070024import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070025import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080026import android.graphics.Point;
27import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070028import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.drawable.Drawable;
Tony8f402802017-06-16 17:24:54 -070030import android.support.v4.graphics.ColorUtils;
Winson Chung656d11c2010-11-29 17:15:47 -080031import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080032import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070033import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070034import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080035import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070036import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040037import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080038import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080039import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070040
Sunny Goyal34b65272015-03-11 16:56:52 -070041import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080042import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070043import com.android.launcher3.Launcher.OnResumeCallback;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080044import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080045import com.android.launcher3.badge.BadgeRenderer;
Tony7308cde2017-06-27 22:38:33 -070046import com.android.launcher3.folder.FolderIcon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080047import com.android.launcher3.graphics.DrawableFactory;
Tony Wickham1237df02017-02-24 08:59:36 -080048import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080049import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070050import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070051
Sunny Goyalc469aad2015-10-01 11:24:23 -070052import java.text.NumberFormat;
53
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054/**
55 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
56 * because we want to make the bubble taller than the text and TextView's clip is
57 * too aggressive.
58 */
Sunny Goyal3dce5f32017-10-05 11:40:05 -070059public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback {
Sunny Goyal95abbb32014-08-04 10:53:22 -070060
Sunny Goyaldfaccf62015-05-11 16:30:44 -070061 private static final int DISPLAY_WORKSPACE = 0;
62 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070063 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070064
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080065 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
66
Sunny Goyal0b0847b2018-03-14 12:30:11 -070067 private final BaseDraggingActivity mActivity;
Winson Chungb745afb2015-03-02 11:51:23 -080068 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070069 private final boolean mCenterVertically;
Sunny Goyal4e5a8782017-06-23 09:34:06 -070070
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080071 private final CheckLongPressHelper mLongPressHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070072 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal9314b7c2017-06-23 10:36:27 -070073 private final float mSlop;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080074
Winson Chungb745afb2015-03-02 11:51:23 -080075 private final boolean mLayoutHorizontal;
76 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080077 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080078 private int mTextColor;
Tony17b7f9b2017-05-23 12:19:09 -070079 private boolean mIsIconVisible = true;
Winson Chung5f8afe62013-08-12 16:19:28 -070080
Tony Wickham1237df02017-02-24 08:59:36 -080081 private BadgeInfo mBadgeInfo;
82 private BadgeRenderer mBadgeRenderer;
Sunny Goyal179249d2017-12-19 16:49:24 -080083 private int mBadgeColor;
Tony Wickham1237df02017-02-24 08:59:36 -080084 private float mBadgeScale;
85 private boolean mForceHideBadge;
86 private Point mTempSpaceForBadgeOffset = new Point();
87 private Rect mTempIconBounds = new Rect();
88
89 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
90 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
91 @Override
92 public Float get(BubbleTextView bubbleTextView) {
93 return bubbleTextView.mBadgeScale;
94 }
95
96 @Override
97 public void set(BubbleTextView bubbleTextView, Float value) {
98 bubbleTextView.mBadgeScale = value;
99 bubbleTextView.invalidate();
100 }
101 };
102
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700103 public static final Property<BubbleTextView, Integer> TEXT_ALPHA_PROPERTY
Tony8f402802017-06-16 17:24:54 -0700104 = new Property<BubbleTextView, Integer>(Integer.class, "textAlpha") {
105 @Override
106 public Integer get(BubbleTextView bubbleTextView) {
107 return bubbleTextView.getTextAlpha();
108 }
109
110 @Override
111 public void set(BubbleTextView bubbleTextView, Integer alpha) {
112 bubbleTextView.setTextAlpha(alpha);
113 }
114 };
115
Sunny Goyal4ffec482016-02-09 11:28:52 -0800116 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800117 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800118 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700119 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800120 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700121 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500122
Sunny Goyal34b65272015-03-11 16:56:52 -0700123 private IconLoadRequest mIconLoadRequest;
124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700126 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 }
128
129 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700130 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 }
132
133 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
134 super(context, attrs, defStyle);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700135 mActivity = BaseDraggingActivity.fromContext(context);
136 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700137 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700138
Adam Cohen96bb7982014-07-07 11:58:56 -0700139 TypedArray a = context.obtainStyledAttributes(attrs,
140 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800141 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700142
143 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
144 int defaultIconSize = grid.iconSizePx;
145 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700146 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700147 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700148 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700149 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
150 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700151 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700152 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700153 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700154 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700155 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700156 }
Winson1f064272016-07-18 17:18:02 -0700157 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700158
159 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
160 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700161 a.recycle();
162
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800163 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700164 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800165
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700166 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 }
169
Jon Miranda52549442017-10-26 11:28:06 -0700170 /**
171 * Resets the view so it can be recycled.
172 */
173 public void reset() {
174 mBadgeInfo = null;
Sunny Goyal179249d2017-12-19 16:49:24 -0800175 mBadgeColor = Color.TRANSPARENT;
Jon Miranda48043ee2017-11-01 15:40:22 -0700176 mBadgeScale = 0f;
Jon Miranda52549442017-10-26 11:28:06 -0700177 mForceHideBadge = false;
178 }
179
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800180 public void applyFromShortcutInfo(ShortcutInfo info) {
181 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700182 }
183
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800184 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800185 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800186 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700187 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800188 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500189 }
Tony Wickham010d2552017-01-20 08:15:28 -0800190
Tony Wickham1e618492017-02-02 12:57:18 -0800191 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800192 }
193
Sunny Goyal508da152014-08-14 10:53:27 -0700194 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800195 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700196
Winson Chung888b3a12015-03-13 11:14:16 -0700197 // We don't need to check the info since it's not a ShortcutInfo
198 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700199
200 // Verify high res immediately
201 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800202
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700203 if (info instanceof PromiseAppInfo) {
204 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
205 applyProgressLevel(promiseAppInfo.level);
206 }
Tony Wickham1e618492017-02-02 12:57:18 -0800207 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700208 }
209
Sunny Goyal0e08f162015-05-12 11:32:39 -0700210 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800211 applyIconAndLabel(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700212 // We don't need to check the info since it's not a ShortcutInfo
213 super.setTag(info);
214
215 // Verify high res immediately
216 verifyHighRes();
217 }
218
Sunny Goyal179249d2017-12-19 16:49:24 -0800219 private void applyIconAndLabel(ItemInfoWithIcon info) {
220 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(info);
221 mBadgeColor = IconPalette.getMutedColor(info.iconColor, 0.54f);
222
Sunny Goyalf4204382016-07-13 10:46:07 -0700223 setIcon(iconDrawable);
224 setText(info.title);
225 if (info.contentDescription != null) {
226 setContentDescription(info.isDisabled()
227 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
228 : info.contentDescription);
229 }
230 }
231
Winson Chung7501adf2015-06-02 11:24:28 -0700232 /**
233 * Overrides the default long press timeout.
234 */
235 public void setLongPressTimeout(int longPressTimeout) {
236 mLongPressHelper.setLongPressTimeout(longPressTimeout);
237 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700240 public void setTag(Object tag) {
241 if (tag != null) {
242 LauncherModel.checkItemInfo((ItemInfo) tag);
243 }
244 super.setTag(tag);
245 }
246
247 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800248 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700249 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800250 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800251 }
Sunny Goyal508da152014-08-14 10:53:27 -0700252 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800253
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800254 @Override
255 protected int[] onCreateDrawableState(int extraSpace) {
256 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
257 if (mStayPressed) {
258 mergeDrawableStates(drawableState, STATE_PRESSED);
259 }
260 return drawableState;
261 }
262
Winson Chungb745afb2015-03-02 11:51:23 -0800263 /** Returns the icon for this view. */
264 public Drawable getIcon() {
265 return mIcon;
266 }
267
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700268 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800269 public boolean onTouchEvent(MotionEvent event) {
270 // Call the superclass onTouchEvent first, because sometimes it changes the state to
271 // isPressed() on an ACTION_UP
272 boolean result = super.onTouchEvent(event);
273
Mady Melloref044dd2015-06-02 15:35:07 -0700274 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700275 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700276 mLongPressHelper.cancelLongPress();
277 result = true;
278 }
279
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800280 switch (event.getAction()) {
281 case MotionEvent.ACTION_DOWN:
Mady Melloref044dd2015-06-02 15:35:07 -0700282 // If we're in a stylus button press, don't check for long press.
283 if (!mStylusEventHelper.inStylusButtonPressed()) {
284 mLongPressHelper.postCheckForLongPress();
285 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800286 break;
287 case MotionEvent.ACTION_CANCEL:
288 case MotionEvent.ACTION_UP:
Winson Chung88f33452012-02-23 15:23:44 -0800289 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800290 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400291 case MotionEvent.ACTION_MOVE:
292 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
293 mLongPressHelper.cancelLongPress();
294 }
295 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800296 }
297 return result;
298 }
299
Michael Jurkaddd62e92011-02-16 17:49:14 -0800300 void setStayPressed(boolean stayPressed) {
301 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800302 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800303 }
304
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700305 @Override
306 public void onLauncherResume() {
307 // Reset the pressed state of icon that was locked in the press state while activity
308 // was launching
309 setStayPressed(false);
310 }
311
Sunny Goyal508da152014-08-14 10:53:27 -0700312 void clearPressedBackground() {
313 setPressed(false);
314 setStayPressed(false);
315 }
316
317 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700318 public boolean onKeyUp(int keyCode, KeyEvent event) {
319 // Unlike touch events, keypress event propagate pressed state change immediately,
320 // without waiting for onClickHandler to execute. Disable pressed state changes here
321 // to avoid flickering.
322 mIgnorePressedStateChange = true;
323 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700324 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800325 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700326 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800327 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800328
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700329 @SuppressWarnings("wrongcall")
330 protected void drawWithoutBadge(Canvas canvas) {
331 super.onDraw(canvas);
332 }
333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700335 public void onDraw(Canvas canvas) {
336 super.onDraw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800337 drawBadgeIfNecessary(canvas);
338 }
339
340 /**
341 * Draws the icon badge in the top right corner of the icon bounds.
342 * @param canvas The canvas to draw to.
343 */
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700344 protected void drawBadgeIfNecessary(Canvas canvas) {
Tony Wickham1237df02017-02-24 08:59:36 -0800345 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
346 getIconBounds(mTempIconBounds);
347 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
348 final int scrollX = getScrollX();
349 final int scrollY = getScrollY();
350 canvas.translate(scrollX, scrollY);
Sunny Goyal179249d2017-12-19 16:49:24 -0800351 mBadgeRenderer.draw(canvas, mBadgeColor, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800352 mTempSpaceForBadgeOffset);
353 canvas.translate(-scrollX, -scrollY);
354 }
355 }
356
357 public void forceHideBadge(boolean forceHideBadge) {
358 if (mForceHideBadge == forceHideBadge) {
359 return;
360 }
361 mForceHideBadge = forceHideBadge;
362
363 if (forceHideBadge) {
364 invalidate();
365 } else if (hasBadge()) {
366 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
367 }
368 }
369
370 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700371 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800372 }
373
374 public void getIconBounds(Rect outBounds) {
375 int top = getPaddingTop();
376 int left = (getWidth() - mIconSize) / 2;
377 int right = left + mIconSize;
378 int bottom = top + mIconSize;
379 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381
382 @Override
Winson1f064272016-07-18 17:18:02 -0700383 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
384 if (mCenterVertically) {
385 Paint.FontMetrics fm = getPaint().getFontMetrics();
386 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
387 (int) Math.ceil(fm.bottom - fm.top);
388 int height = MeasureSpec.getSize(heightMeasureSpec);
389 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
390 getPaddingBottom());
391 }
392 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
393 }
394
395 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700396 public void setTextColor(int color) {
397 mTextColor = color;
398 super.setTextColor(color);
399 }
400
Adam Cohen96bb7982014-07-07 11:58:56 -0700401 @Override
402 public void setTextColor(ColorStateList colors) {
403 mTextColor = colors.getDefaultColor();
404 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700405 }
406
Tony7308cde2017-06-27 22:38:33 -0700407 public boolean shouldTextBeVisible() {
408 // Text should be visible everywhere but the hotseat.
409 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
410 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
411 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
412 }
413
Winson Chung5f8afe62013-08-12 16:19:28 -0700414 public void setTextVisibility(boolean visible) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700415 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700416 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700417 } else {
Tony8f402802017-06-16 17:24:54 -0700418 setTextAlpha(0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700419 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700420 }
421
Tony8f402802017-06-16 17:24:54 -0700422 private void setTextAlpha(int alpha) {
423 super.setTextColor(ColorUtils.setAlphaComponent(mTextColor, alpha));
424 }
425
426 private int getTextAlpha() {
427 return Color.alpha(getCurrentTextColor());
428 }
429
430 /**
431 * Creates an animator to fade the text in or out.
432 * @param fadeIn Whether the text should fade in or fade out.
433 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700434 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Tony7308cde2017-06-27 22:38:33 -0700435 int toAlpha = shouldTextBeVisible() && fadeIn ? Color.alpha(mTextColor) : 0;
436 return ObjectAnimator.ofInt(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700437 }
438
Winson Chungaffd7b42010-08-20 15:11:56 -0700439 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800440 public void cancelLongPress() {
441 super.cancelLongPress();
442
443 mLongPressHelper.cancelLongPress();
444 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500445
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800446 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700447 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400448 ShortcutInfo info = (ShortcutInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700449 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700450 final int progressLevel = isPromise ?
451 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
452 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700453
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700454 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
455 if (preloadDrawable != null && promiseStateChanged) {
456 preloadDrawable.maybePerformFinishedAnimation();
457 }
458 }
459 }
460
461 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
462 if (getTag() instanceof ItemInfoWithIcon) {
463 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700464 if (progressLevel >= 100) {
465 setContentDescription(info.contentDescription != null
466 ? info.contentDescription : "");
467 } else if (progressLevel > 0) {
468 setContentDescription(getContext()
469 .getString(R.string.app_downloading_title, info.title,
470 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
471 } else {
472 setContentDescription(getContext()
473 .getString(R.string.app_waiting_download_title, info.title));
474 }
Winson Chungb745afb2015-03-02 11:51:23 -0800475 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700476 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800477 if (mIcon instanceof PreloadIconDrawable) {
478 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700479 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700480 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800481 preloadDrawable = DrawableFactory.get(getContext())
Sunny Goyal179249d2017-12-19 16:49:24 -0800482 .newPendingIcon(info, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700483 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700484 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700485 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700486 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700487 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400488 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700489 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400490 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700491
Tony Wickham1e618492017-02-02 12:57:18 -0800492 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800493 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800494 boolean wasBadged = mBadgeInfo != null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700495 mBadgeInfo = mActivity.getBadgeInfoForItem(itemInfo);
Tony Wickham2fe09f22017-04-25 12:46:04 -0700496 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800497 float newBadgeScale = isBadged ? 1f : 0;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700498 mBadgeRenderer = mActivity.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800499 if (wasBadged || isBadged) {
Tony Wickham1237df02017-02-24 08:59:36 -0800500 // Animate when a badge is first added or when it is removed.
501 if (animate && (wasBadged ^ isBadged) && isShown()) {
502 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
503 } else {
504 mBadgeScale = newBadgeScale;
505 invalidate();
506 }
507 }
Tony Wickham305e9202018-01-23 17:46:47 -0800508 if (itemInfo.contentDescription != null) {
509 if (hasBadge()) {
510 int count = mBadgeInfo.getNotificationCount();
511 setContentDescription(getContext().getResources().getQuantityString(
512 R.plurals.badged_app_label, count, itemInfo.contentDescription, count));
513 } else {
514 setContentDescription(itemInfo.contentDescription);
515 }
516 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800517 }
518 }
519
Winson Chungb745afb2015-03-02 11:51:23 -0800520 /**
521 * Sets the icon for this view based on the layout direction.
522 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700523 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800524 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800525 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700526 if (mIsIconVisible) {
527 applyCompoundDrawables(mIcon);
528 }
529 }
530
531 public void setIconVisible(boolean visible) {
532 mIsIconVisible = visible;
533 mDisableRelayout = true;
534 Drawable icon = mIcon;
535 if (!visible) {
536 icon = new ColorDrawable(Color.TRANSPARENT);
537 icon.setBounds(0, 0, mIconSize, mIconSize);
538 }
539 applyCompoundDrawables(icon);
540 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700541 }
542
543 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800544 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800545 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800546 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700547 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800548 }
Winson Chungb745afb2015-03-02 11:51:23 -0800549 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700550
Sunny Goyal69b75642015-05-15 17:00:24 -0700551 @Override
552 public void requestLayout() {
553 if (!mDisableRelayout) {
554 super.requestLayout();
555 }
556 }
557
Sunny Goyal34b65272015-03-11 16:56:52 -0700558 /**
559 * Applies the item info if it is same as what the view is pointing to currently.
560 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800561 @Override
562 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700563 if (getTag() == info) {
564 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700565 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700566
Jon Miranda7f522a22017-07-28 11:56:47 -0700567 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
568 info.iconBitmap.prepareToDraw();
569
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 Goyal0b0847b2018-03-14 12:30:11 -0700574 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700575 } else if (info instanceof PackageItemInfo) {
576 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700577 }
Winsonc0880492015-08-21 11:16:27 -0700578
Sunny Goyal69b75642015-05-15 17:00:24 -0700579 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700580 }
581 }
582
583 /**
584 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
585 */
586 public void verifyHighRes() {
587 if (mIconLoadRequest != null) {
588 mIconLoadRequest.cancel();
589 mIconLoadRequest = null;
590 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800591 if (getTag() instanceof ItemInfoWithIcon) {
592 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700593 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800594 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700595 .updateIconInBackground(BubbleTextView.this, info);
596 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700597 }
598 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700599
Jon Miranda47170112017-03-03 14:57:14 -0800600 public int getIconSize() {
601 return mIconSize;
602 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603}