blob: 4fd39b08384cae9990196cf6755a9539ea6b460d [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;
Sunny Goyal66dccad2018-03-19 12:00:51 -070031import android.text.TextUtils.TruncateAt;
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;
Michael Jurkabdb5c532011-02-01 15:05:06 -080040import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070041
Sunny Goyal34b65272015-03-11 16:56:52 -070042import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080043import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070044import com.android.launcher3.Launcher.OnResumeCallback;
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;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080048import com.android.launcher3.graphics.DrawableFactory;
Tony Wickham1237df02017-02-24 08:59:36 -080049import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080050import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070051import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070052
Sunny Goyalc469aad2015-10-01 11:24:23 -070053import java.text.NumberFormat;
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055/**
56 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
57 * because we want to make the bubble taller than the text and TextView's clip is
58 * too aggressive.
59 */
Sunny Goyal3dce5f32017-10-05 11:40:05 -070060public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback {
Sunny Goyal95abbb32014-08-04 10:53:22 -070061
Sunny Goyaldfaccf62015-05-11 16:30:44 -070062 private static final int DISPLAY_WORKSPACE = 0;
63 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070064 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070065
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080066 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
67
Sunny Goyal0b0847b2018-03-14 12:30:11 -070068 private final BaseDraggingActivity mActivity;
Winson Chungb745afb2015-03-02 11:51:23 -080069 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070070 private final boolean mCenterVertically;
Sunny Goyal4e5a8782017-06-23 09:34:06 -070071
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080072 private final CheckLongPressHelper mLongPressHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070073 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal9314b7c2017-06-23 10:36:27 -070074 private final float mSlop;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075
Winson Chungb745afb2015-03-02 11:51:23 -080076 private final boolean mLayoutHorizontal;
77 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080078 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080079 private int mTextColor;
Tony17b7f9b2017-05-23 12:19:09 -070080 private boolean mIsIconVisible = true;
Winson Chung5f8afe62013-08-12 16:19:28 -070081
Tony Wickham1237df02017-02-24 08:59:36 -080082 private BadgeInfo mBadgeInfo;
83 private BadgeRenderer mBadgeRenderer;
Sunny Goyal179249d2017-12-19 16:49:24 -080084 private int mBadgeColor;
Tony Wickham1237df02017-02-24 08:59:36 -080085 private float mBadgeScale;
86 private boolean mForceHideBadge;
87 private Point mTempSpaceForBadgeOffset = new Point();
88 private Rect mTempIconBounds = new Rect();
89
90 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
91 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
92 @Override
93 public Float get(BubbleTextView bubbleTextView) {
94 return bubbleTextView.mBadgeScale;
95 }
96
97 @Override
98 public void set(BubbleTextView bubbleTextView, Float value) {
99 bubbleTextView.mBadgeScale = value;
100 bubbleTextView.invalidate();
101 }
102 };
103
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700104 public static final Property<BubbleTextView, Integer> TEXT_ALPHA_PROPERTY
Tony8f402802017-06-16 17:24:54 -0700105 = new Property<BubbleTextView, Integer>(Integer.class, "textAlpha") {
106 @Override
107 public Integer get(BubbleTextView bubbleTextView) {
108 return bubbleTextView.getTextAlpha();
109 }
110
111 @Override
112 public void set(BubbleTextView bubbleTextView, Integer alpha) {
113 bubbleTextView.setTextAlpha(alpha);
114 }
115 };
116
Sunny Goyal4ffec482016-02-09 11:28:52 -0800117 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800118 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800119 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700120 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800121 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700122 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500123
Sunny Goyal34b65272015-03-11 16:56:52 -0700124 private IconLoadRequest mIconLoadRequest;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700127 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 }
129
130 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700131 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 }
133
134 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
135 super(context, attrs, defStyle);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700136 mActivity = BaseDraggingActivity.fromContext(context);
137 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700138 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700139
Adam Cohen96bb7982014-07-07 11:58:56 -0700140 TypedArray a = context.obtainStyledAttributes(attrs,
141 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800142 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700143
144 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
145 int defaultIconSize = grid.iconSizePx;
146 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700147 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700148 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700149 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700150 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
151 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700152 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700153 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700154 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700155 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700156 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700157 }
Winson1f064272016-07-18 17:18:02 -0700158 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700159
160 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
161 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700162 a.recycle();
163
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800164 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700165 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800166
Sunny Goyal66dccad2018-03-19 12:00:51 -0700167 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700168 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 }
171
Sunny Goyal66dccad2018-03-19 12:00:51 -0700172 @Override
173 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
174 // Disable marques when not focused to that, so that updating text does not cause relayout.
175 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
176 super.onFocusChanged(focused, direction, previouslyFocusedRect);
177 }
178
Jon Miranda52549442017-10-26 11:28:06 -0700179 /**
180 * Resets the view so it can be recycled.
181 */
182 public void reset() {
183 mBadgeInfo = null;
Sunny Goyal179249d2017-12-19 16:49:24 -0800184 mBadgeColor = Color.TRANSPARENT;
Jon Miranda48043ee2017-11-01 15:40:22 -0700185 mBadgeScale = 0f;
Jon Miranda52549442017-10-26 11:28:06 -0700186 mForceHideBadge = false;
187 }
188
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800189 public void applyFromShortcutInfo(ShortcutInfo info) {
190 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700191 }
192
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800193 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800194 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800195 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700196 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800197 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500198 }
Tony Wickham010d2552017-01-20 08:15:28 -0800199
Tony Wickham1e618492017-02-02 12:57:18 -0800200 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800201 }
202
Sunny Goyal508da152014-08-14 10:53:27 -0700203 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800204 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700205
Winson Chung888b3a12015-03-13 11:14:16 -0700206 // We don't need to check the info since it's not a ShortcutInfo
207 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700208
209 // Verify high res immediately
210 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800211
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700212 if (info instanceof PromiseAppInfo) {
213 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
214 applyProgressLevel(promiseAppInfo.level);
215 }
Tony Wickham1e618492017-02-02 12:57:18 -0800216 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700217 }
218
Sunny Goyal0e08f162015-05-12 11:32:39 -0700219 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800220 applyIconAndLabel(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700221 // We don't need to check the info since it's not a ShortcutInfo
222 super.setTag(info);
223
224 // Verify high res immediately
225 verifyHighRes();
226 }
227
Sunny Goyal179249d2017-12-19 16:49:24 -0800228 private void applyIconAndLabel(ItemInfoWithIcon info) {
229 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(info);
230 mBadgeColor = IconPalette.getMutedColor(info.iconColor, 0.54f);
231
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:
Mady Melloref044dd2015-06-02 15:35:07 -0700291 // If we're in a stylus button press, don't check for long press.
292 if (!mStylusEventHelper.inStylusButtonPressed()) {
293 mLongPressHelper.postCheckForLongPress();
294 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800295 break;
296 case MotionEvent.ACTION_CANCEL:
297 case MotionEvent.ACTION_UP:
Winson Chung88f33452012-02-23 15:23:44 -0800298 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800299 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400300 case MotionEvent.ACTION_MOVE:
301 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
302 mLongPressHelper.cancelLongPress();
303 }
304 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800305 }
306 return result;
307 }
308
Michael Jurkaddd62e92011-02-16 17:49:14 -0800309 void setStayPressed(boolean stayPressed) {
310 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800311 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800312 }
313
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700314 @Override
315 public void onLauncherResume() {
316 // Reset the pressed state of icon that was locked in the press state while activity
317 // was launching
318 setStayPressed(false);
319 }
320
Sunny Goyal508da152014-08-14 10:53:27 -0700321 void clearPressedBackground() {
322 setPressed(false);
323 setStayPressed(false);
324 }
325
326 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700327 public boolean onKeyUp(int keyCode, KeyEvent event) {
328 // Unlike touch events, keypress event propagate pressed state change immediately,
329 // without waiting for onClickHandler to execute. Disable pressed state changes here
330 // to avoid flickering.
331 mIgnorePressedStateChange = true;
332 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700333 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800334 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700335 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800336 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800337
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700338 @SuppressWarnings("wrongcall")
339 protected void drawWithoutBadge(Canvas canvas) {
340 super.onDraw(canvas);
341 }
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700344 public void onDraw(Canvas canvas) {
345 super.onDraw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800346 drawBadgeIfNecessary(canvas);
347 }
348
349 /**
350 * Draws the icon badge in the top right corner of the icon bounds.
351 * @param canvas The canvas to draw to.
352 */
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700353 protected void drawBadgeIfNecessary(Canvas canvas) {
Tony Wickham1237df02017-02-24 08:59:36 -0800354 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
355 getIconBounds(mTempIconBounds);
356 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
357 final int scrollX = getScrollX();
358 final int scrollY = getScrollY();
359 canvas.translate(scrollX, scrollY);
Sunny Goyal179249d2017-12-19 16:49:24 -0800360 mBadgeRenderer.draw(canvas, mBadgeColor, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800361 mTempSpaceForBadgeOffset);
362 canvas.translate(-scrollX, -scrollY);
363 }
364 }
365
366 public void forceHideBadge(boolean forceHideBadge) {
367 if (mForceHideBadge == forceHideBadge) {
368 return;
369 }
370 mForceHideBadge = forceHideBadge;
371
372 if (forceHideBadge) {
373 invalidate();
374 } else if (hasBadge()) {
375 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
376 }
377 }
378
379 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700380 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800381 }
382
383 public void getIconBounds(Rect outBounds) {
384 int top = getPaddingTop();
385 int left = (getWidth() - mIconSize) / 2;
386 int right = left + mIconSize;
387 int bottom = top + mIconSize;
388 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390
391 @Override
Winson1f064272016-07-18 17:18:02 -0700392 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
393 if (mCenterVertically) {
394 Paint.FontMetrics fm = getPaint().getFontMetrics();
395 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
396 (int) Math.ceil(fm.bottom - fm.top);
397 int height = MeasureSpec.getSize(heightMeasureSpec);
398 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
399 getPaddingBottom());
400 }
401 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
402 }
403
404 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700405 public void setTextColor(int color) {
406 mTextColor = color;
407 super.setTextColor(color);
408 }
409
Adam Cohen96bb7982014-07-07 11:58:56 -0700410 @Override
411 public void setTextColor(ColorStateList colors) {
412 mTextColor = colors.getDefaultColor();
413 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700414 }
415
Tony7308cde2017-06-27 22:38:33 -0700416 public boolean shouldTextBeVisible() {
417 // Text should be visible everywhere but the hotseat.
418 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
419 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
420 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
421 }
422
Winson Chung5f8afe62013-08-12 16:19:28 -0700423 public void setTextVisibility(boolean visible) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700424 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700425 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700426 } else {
Tony8f402802017-06-16 17:24:54 -0700427 setTextAlpha(0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700428 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700429 }
430
Tony8f402802017-06-16 17:24:54 -0700431 private void setTextAlpha(int alpha) {
432 super.setTextColor(ColorUtils.setAlphaComponent(mTextColor, alpha));
433 }
434
435 private int getTextAlpha() {
436 return Color.alpha(getCurrentTextColor());
437 }
438
439 /**
440 * Creates an animator to fade the text in or out.
441 * @param fadeIn Whether the text should fade in or fade out.
442 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700443 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Tony7308cde2017-06-27 22:38:33 -0700444 int toAlpha = shouldTextBeVisible() && fadeIn ? Color.alpha(mTextColor) : 0;
445 return ObjectAnimator.ofInt(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700446 }
447
Winson Chungaffd7b42010-08-20 15:11:56 -0700448 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800449 public void cancelLongPress() {
450 super.cancelLongPress();
451
452 mLongPressHelper.cancelLongPress();
453 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500454
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800455 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700456 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400457 ShortcutInfo info = (ShortcutInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700458 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700459 final int progressLevel = isPromise ?
460 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
461 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700462
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700463 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
464 if (preloadDrawable != null && promiseStateChanged) {
465 preloadDrawable.maybePerformFinishedAnimation();
466 }
467 }
468 }
469
470 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
471 if (getTag() instanceof ItemInfoWithIcon) {
472 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700473 if (progressLevel >= 100) {
474 setContentDescription(info.contentDescription != null
475 ? info.contentDescription : "");
476 } else if (progressLevel > 0) {
477 setContentDescription(getContext()
478 .getString(R.string.app_downloading_title, info.title,
479 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
480 } else {
481 setContentDescription(getContext()
482 .getString(R.string.app_waiting_download_title, info.title));
483 }
Winson Chungb745afb2015-03-02 11:51:23 -0800484 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700485 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800486 if (mIcon instanceof PreloadIconDrawable) {
487 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700488 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700489 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800490 preloadDrawable = DrawableFactory.get(getContext())
Sunny Goyal179249d2017-12-19 16:49:24 -0800491 .newPendingIcon(info, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700492 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700493 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700494 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700495 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700496 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400497 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700498 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400499 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700500
Tony Wickham1e618492017-02-02 12:57:18 -0800501 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800502 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800503 boolean wasBadged = mBadgeInfo != null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700504 mBadgeInfo = mActivity.getBadgeInfoForItem(itemInfo);
Tony Wickham2fe09f22017-04-25 12:46:04 -0700505 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800506 float newBadgeScale = isBadged ? 1f : 0;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700507 mBadgeRenderer = mActivity.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800508 if (wasBadged || isBadged) {
Tony Wickham1237df02017-02-24 08:59:36 -0800509 // Animate when a badge is first added or when it is removed.
510 if (animate && (wasBadged ^ isBadged) && isShown()) {
511 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
512 } else {
513 mBadgeScale = newBadgeScale;
514 invalidate();
515 }
516 }
Tony Wickham305e9202018-01-23 17:46:47 -0800517 if (itemInfo.contentDescription != null) {
518 if (hasBadge()) {
519 int count = mBadgeInfo.getNotificationCount();
520 setContentDescription(getContext().getResources().getQuantityString(
521 R.plurals.badged_app_label, count, itemInfo.contentDescription, count));
522 } else {
523 setContentDescription(itemInfo.contentDescription);
524 }
525 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800526 }
527 }
528
Winson Chungb745afb2015-03-02 11:51:23 -0800529 /**
530 * Sets the icon for this view based on the layout direction.
531 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700532 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700533 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700534 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700535 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700536 mIcon = icon;
Tony17b7f9b2017-05-23 12:19:09 -0700537 }
538
539 public void setIconVisible(boolean visible) {
540 mIsIconVisible = visible;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700541 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700542 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700543 }
544
545 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700546 // If we had already set an icon before, disable relayout as the icon size is the
547 // same as before.
548 mDisableRelayout = mIcon != null;
549
550 icon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800551 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800552 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800553 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700554 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800555 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700556 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800557 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700558
Sunny Goyal69b75642015-05-15 17:00:24 -0700559 @Override
560 public void requestLayout() {
561 if (!mDisableRelayout) {
562 super.requestLayout();
563 }
564 }
565
Sunny Goyal34b65272015-03-11 16:56:52 -0700566 /**
567 * Applies the item info if it is same as what the view is pointing to currently.
568 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800569 @Override
570 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700571 if (getTag() == info) {
572 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700573 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700574
Jon Miranda7f522a22017-07-28 11:56:47 -0700575 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
576 info.iconBitmap.prepareToDraw();
577
Sunny Goyal34b65272015-03-11 16:56:52 -0700578 if (info instanceof AppInfo) {
579 applyFromApplicationInfo((AppInfo) info);
580 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800581 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700582 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700583 } else if (info instanceof PackageItemInfo) {
584 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700585 }
Winsonc0880492015-08-21 11:16:27 -0700586
Sunny Goyal69b75642015-05-15 17:00:24 -0700587 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700588 }
589 }
590
591 /**
592 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
593 */
594 public void verifyHighRes() {
595 if (mIconLoadRequest != null) {
596 mIconLoadRequest.cancel();
597 mIconLoadRequest = null;
598 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800599 if (getTag() instanceof ItemInfoWithIcon) {
600 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700601 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800602 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700603 .updateIconInBackground(BubbleTextView.this, info);
604 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700605 }
606 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700607
Jon Miranda47170112017-03-03 14:57:14 -0800608 public int getIconSize() {
609 return mIconSize;
610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611}