blob: fb7c0ced4aef37b6d2cd6948d3ea2068ffab38d2 [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
Tony Wickham1237df02017-02-24 08:59:36 -080068
69 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
70 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
71 @Override
72 public Float get(BubbleTextView bubbleTextView) {
73 return bubbleTextView.mBadgeScale;
74 }
75
76 @Override
77 public void set(BubbleTextView bubbleTextView, Float value) {
78 bubbleTextView.mBadgeScale = value;
79 bubbleTextView.invalidate();
80 }
81 };
82
Sunny Goyaled7a6932018-04-13 11:41:50 -070083 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
84 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -070085 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070086 public Float get(BubbleTextView bubbleTextView) {
87 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -070088 }
89
90 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070091 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -070092 bubbleTextView.setTextAlpha(alpha);
93 }
94 };
95
Sunny Goyaled7a6932018-04-13 11:41:50 -070096 private final BaseDraggingActivity mActivity;
97 private Drawable mIcon;
98 private final boolean mCenterVertically;
99
100 private final CheckLongPressHelper mLongPressHelper;
101 private final StylusEventHelper mStylusEventHelper;
102 private final float mSlop;
103
104 private final boolean mLayoutHorizontal;
105 private final int mIconSize;
106
107 @ViewDebug.ExportedProperty(category = "launcher")
108 private boolean mIsIconVisible = true;
109 @ViewDebug.ExportedProperty(category = "launcher")
110 private int mTextColor;
111 @ViewDebug.ExportedProperty(category = "launcher")
112 private float mTextAlpha = 1;
113
114 private BadgeInfo mBadgeInfo;
115 private BadgeRenderer mBadgeRenderer;
116 private int mBadgeColor;
117 private float mBadgeScale;
118 private boolean mForceHideBadge;
119 private Point mTempSpaceForBadgeOffset = new Point();
120 private Rect mTempIconBounds = new Rect();
121
Sunny Goyal4ffec482016-02-09 11:28:52 -0800122 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800123 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800124 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700125 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800126 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700127 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500128
Sunny Goyal34b65272015-03-11 16:56:52 -0700129 private IconLoadRequest mIconLoadRequest;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700132 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 }
134
135 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700136 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138
139 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
140 super(context, attrs, defStyle);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700141 mActivity = BaseDraggingActivity.fromContext(context);
142 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700143 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700144
Adam Cohen96bb7982014-07-07 11:58:56 -0700145 TypedArray a = context.obtainStyledAttributes(attrs,
146 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800147 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700148
149 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
150 int defaultIconSize = grid.iconSizePx;
151 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700152 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700153 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700154 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700155 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
156 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700157 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700158 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700159 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700160 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700161 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700162 }
Winson1f064272016-07-18 17:18:02 -0700163 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700164
165 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
166 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700167 a.recycle();
168
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800169 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700170 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800171
Sunny Goyal66dccad2018-03-19 12:00:51 -0700172 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700173 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700174 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 }
176
Sunny Goyal66dccad2018-03-19 12:00:51 -0700177 @Override
178 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
179 // Disable marques when not focused to that, so that updating text does not cause relayout.
180 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
181 super.onFocusChanged(focused, direction, previouslyFocusedRect);
182 }
183
Jon Miranda52549442017-10-26 11:28:06 -0700184 /**
185 * Resets the view so it can be recycled.
186 */
187 public void reset() {
188 mBadgeInfo = null;
Sunny Goyal179249d2017-12-19 16:49:24 -0800189 mBadgeColor = Color.TRANSPARENT;
Jon Miranda48043ee2017-11-01 15:40:22 -0700190 mBadgeScale = 0f;
Jon Miranda52549442017-10-26 11:28:06 -0700191 mForceHideBadge = false;
192 }
193
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800194 public void applyFromShortcutInfo(ShortcutInfo info) {
195 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700196 }
197
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800198 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800199 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800200 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700201 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800202 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500203 }
Tony Wickham010d2552017-01-20 08:15:28 -0800204
Tony Wickham1e618492017-02-02 12:57:18 -0800205 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800206 }
207
Sunny Goyal508da152014-08-14 10:53:27 -0700208 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800209 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700210
Winson Chung888b3a12015-03-13 11:14:16 -0700211 // We don't need to check the info since it's not a ShortcutInfo
212 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700213
214 // Verify high res immediately
215 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800216
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700217 if (info instanceof PromiseAppInfo) {
218 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
219 applyProgressLevel(promiseAppInfo.level);
220 }
Tony Wickham1e618492017-02-02 12:57:18 -0800221 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700222 }
223
Sunny Goyal0e08f162015-05-12 11:32:39 -0700224 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800225 applyIconAndLabel(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700226 // We don't need to check the info since it's not a ShortcutInfo
227 super.setTag(info);
228
229 // Verify high res immediately
230 verifyHighRes();
231 }
232
Sunny Goyal179249d2017-12-19 16:49:24 -0800233 private void applyIconAndLabel(ItemInfoWithIcon info) {
234 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(info);
235 mBadgeColor = IconPalette.getMutedColor(info.iconColor, 0.54f);
236
Sunny Goyalf4204382016-07-13 10:46:07 -0700237 setIcon(iconDrawable);
238 setText(info.title);
239 if (info.contentDescription != null) {
240 setContentDescription(info.isDisabled()
241 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
242 : info.contentDescription);
243 }
244 }
245
Winson Chung7501adf2015-06-02 11:24:28 -0700246 /**
247 * Overrides the default long press timeout.
248 */
249 public void setLongPressTimeout(int longPressTimeout) {
250 mLongPressHelper.setLongPressTimeout(longPressTimeout);
251 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700254 public void setTag(Object tag) {
255 if (tag != null) {
256 LauncherModel.checkItemInfo((ItemInfo) tag);
257 }
258 super.setTag(tag);
259 }
260
261 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800262 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700263 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800264 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800265 }
Sunny Goyal508da152014-08-14 10:53:27 -0700266 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800267
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800268 @Override
269 protected int[] onCreateDrawableState(int extraSpace) {
270 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
271 if (mStayPressed) {
272 mergeDrawableStates(drawableState, STATE_PRESSED);
273 }
274 return drawableState;
275 }
276
Winson Chungb745afb2015-03-02 11:51:23 -0800277 /** Returns the icon for this view. */
278 public Drawable getIcon() {
279 return mIcon;
280 }
281
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700282 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800283 public boolean onTouchEvent(MotionEvent event) {
284 // Call the superclass onTouchEvent first, because sometimes it changes the state to
285 // isPressed() on an ACTION_UP
286 boolean result = super.onTouchEvent(event);
287
Mady Melloref044dd2015-06-02 15:35:07 -0700288 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700289 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700290 mLongPressHelper.cancelLongPress();
291 result = true;
292 }
293
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800294 switch (event.getAction()) {
295 case MotionEvent.ACTION_DOWN:
Mady Melloref044dd2015-06-02 15:35:07 -0700296 // If we're in a stylus button press, don't check for long press.
297 if (!mStylusEventHelper.inStylusButtonPressed()) {
298 mLongPressHelper.postCheckForLongPress();
299 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800300 break;
301 case MotionEvent.ACTION_CANCEL:
302 case MotionEvent.ACTION_UP:
Winson Chung88f33452012-02-23 15:23:44 -0800303 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800304 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400305 case MotionEvent.ACTION_MOVE:
306 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
307 mLongPressHelper.cancelLongPress();
308 }
309 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800310 }
311 return result;
312 }
313
Michael Jurkaddd62e92011-02-16 17:49:14 -0800314 void setStayPressed(boolean stayPressed) {
315 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800316 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800317 }
318
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700319 @Override
320 public void onLauncherResume() {
321 // Reset the pressed state of icon that was locked in the press state while activity
322 // was launching
323 setStayPressed(false);
324 }
325
Sunny Goyal508da152014-08-14 10:53:27 -0700326 void clearPressedBackground() {
327 setPressed(false);
328 setStayPressed(false);
329 }
330
331 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700332 public boolean onKeyUp(int keyCode, KeyEvent event) {
333 // Unlike touch events, keypress event propagate pressed state change immediately,
334 // without waiting for onClickHandler to execute. Disable pressed state changes here
335 // to avoid flickering.
336 mIgnorePressedStateChange = true;
337 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700338 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800339 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700340 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800341 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800342
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700343 @SuppressWarnings("wrongcall")
344 protected void drawWithoutBadge(Canvas canvas) {
345 super.onDraw(canvas);
346 }
347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700349 public void onDraw(Canvas canvas) {
350 super.onDraw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800351 drawBadgeIfNecessary(canvas);
352 }
353
354 /**
355 * Draws the icon badge in the top right corner of the icon bounds.
356 * @param canvas The canvas to draw to.
357 */
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700358 protected void drawBadgeIfNecessary(Canvas canvas) {
Tony Wickham1237df02017-02-24 08:59:36 -0800359 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
360 getIconBounds(mTempIconBounds);
361 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
362 final int scrollX = getScrollX();
363 final int scrollY = getScrollY();
364 canvas.translate(scrollX, scrollY);
Sunny Goyal179249d2017-12-19 16:49:24 -0800365 mBadgeRenderer.draw(canvas, mBadgeColor, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800366 mTempSpaceForBadgeOffset);
367 canvas.translate(-scrollX, -scrollY);
368 }
369 }
370
371 public void forceHideBadge(boolean forceHideBadge) {
372 if (mForceHideBadge == forceHideBadge) {
373 return;
374 }
375 mForceHideBadge = forceHideBadge;
376
377 if (forceHideBadge) {
378 invalidate();
379 } else if (hasBadge()) {
380 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
381 }
382 }
383
384 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700385 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800386 }
387
388 public void getIconBounds(Rect outBounds) {
389 int top = getPaddingTop();
390 int left = (getWidth() - mIconSize) / 2;
391 int right = left + mIconSize;
392 int bottom = top + mIconSize;
393 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395
396 @Override
Winson1f064272016-07-18 17:18:02 -0700397 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
398 if (mCenterVertically) {
399 Paint.FontMetrics fm = getPaint().getFontMetrics();
400 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
401 (int) Math.ceil(fm.bottom - fm.top);
402 int height = MeasureSpec.getSize(heightMeasureSpec);
403 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
404 getPaddingBottom());
405 }
406 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
407 }
408
409 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700410 public void setTextColor(int color) {
411 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700412 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700413 }
414
Adam Cohen96bb7982014-07-07 11:58:56 -0700415 @Override
416 public void setTextColor(ColorStateList colors) {
417 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700418 if (Float.compare(mTextAlpha, 1) == 0) {
419 super.setTextColor(colors);
420 } else {
421 super.setTextColor(getModifiedColor());
422 }
Adam Cohen477828c2013-09-20 12:05:49 -0700423 }
424
Tony7308cde2017-06-27 22:38:33 -0700425 public boolean shouldTextBeVisible() {
426 // Text should be visible everywhere but the hotseat.
427 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
428 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
429 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
430 }
431
Winson Chung5f8afe62013-08-12 16:19:28 -0700432 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700433 setTextAlpha(visible ? 1 : 0);
434 }
435
436 private void setTextAlpha(float alpha) {
437 mTextAlpha = alpha;
438 super.setTextColor(getModifiedColor());
439 }
440
441 private int getModifiedColor() {
442 if (mTextAlpha == 0) {
443 // Special case to prevent text shadows in high contrast mode
444 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700445 }
Sunny Goyaled7a6932018-04-13 11:41:50 -0700446 return ColorUtils.setAlphaComponent(
447 mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700448 }
449
450 /**
451 * Creates an animator to fade the text in or out.
452 * @param fadeIn Whether the text should fade in or fade out.
453 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700454 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700455 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
456 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700457 }
458
Winson Chungaffd7b42010-08-20 15:11:56 -0700459 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800460 public void cancelLongPress() {
461 super.cancelLongPress();
462
463 mLongPressHelper.cancelLongPress();
464 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500465
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800466 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700467 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400468 ShortcutInfo info = (ShortcutInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700469 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700470 final int progressLevel = isPromise ?
471 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
472 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700473
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700474 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
475 if (preloadDrawable != null && promiseStateChanged) {
476 preloadDrawable.maybePerformFinishedAnimation();
477 }
478 }
479 }
480
481 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
482 if (getTag() instanceof ItemInfoWithIcon) {
483 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700484 if (progressLevel >= 100) {
485 setContentDescription(info.contentDescription != null
486 ? info.contentDescription : "");
487 } else if (progressLevel > 0) {
488 setContentDescription(getContext()
489 .getString(R.string.app_downloading_title, info.title,
490 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
491 } else {
492 setContentDescription(getContext()
493 .getString(R.string.app_waiting_download_title, info.title));
494 }
Winson Chungb745afb2015-03-02 11:51:23 -0800495 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700496 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800497 if (mIcon instanceof PreloadIconDrawable) {
498 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700499 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700500 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800501 preloadDrawable = DrawableFactory.get(getContext())
Sunny Goyal179249d2017-12-19 16:49:24 -0800502 .newPendingIcon(info, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700503 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700504 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700505 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700506 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700507 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400508 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700509 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400510 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700511
Tony Wickham1e618492017-02-02 12:57:18 -0800512 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800513 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800514 boolean wasBadged = mBadgeInfo != null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700515 mBadgeInfo = mActivity.getBadgeInfoForItem(itemInfo);
Tony Wickham2fe09f22017-04-25 12:46:04 -0700516 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800517 float newBadgeScale = isBadged ? 1f : 0;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700518 mBadgeRenderer = mActivity.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800519 if (wasBadged || isBadged) {
Tony Wickham1237df02017-02-24 08:59:36 -0800520 // Animate when a badge is first added or when it is removed.
521 if (animate && (wasBadged ^ isBadged) && isShown()) {
522 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
523 } else {
524 mBadgeScale = newBadgeScale;
525 invalidate();
526 }
527 }
Tony Wickham305e9202018-01-23 17:46:47 -0800528 if (itemInfo.contentDescription != null) {
529 if (hasBadge()) {
530 int count = mBadgeInfo.getNotificationCount();
531 setContentDescription(getContext().getResources().getQuantityString(
532 R.plurals.badged_app_label, count, itemInfo.contentDescription, count));
533 } else {
534 setContentDescription(itemInfo.contentDescription);
535 }
536 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800537 }
538 }
539
Winson Chungb745afb2015-03-02 11:51:23 -0800540 /**
541 * Sets the icon for this view based on the layout direction.
542 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700543 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700544 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700545 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700546 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700547 mIcon = icon;
Tony17b7f9b2017-05-23 12:19:09 -0700548 }
549
550 public void setIconVisible(boolean visible) {
551 mIsIconVisible = visible;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700552 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700553 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700554 }
555
556 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700557 // If we had already set an icon before, disable relayout as the icon size is the
558 // same as before.
559 mDisableRelayout = mIcon != null;
560
561 icon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800562 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800563 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800564 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700565 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800566 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700567 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800568 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700569
Sunny Goyal69b75642015-05-15 17:00:24 -0700570 @Override
571 public void requestLayout() {
572 if (!mDisableRelayout) {
573 super.requestLayout();
574 }
575 }
576
Sunny Goyal34b65272015-03-11 16:56:52 -0700577 /**
578 * Applies the item info if it is same as what the view is pointing to currently.
579 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800580 @Override
581 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700582 if (getTag() == info) {
583 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700584 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700585
Jon Miranda7f522a22017-07-28 11:56:47 -0700586 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
587 info.iconBitmap.prepareToDraw();
588
Sunny Goyal34b65272015-03-11 16:56:52 -0700589 if (info instanceof AppInfo) {
590 applyFromApplicationInfo((AppInfo) info);
591 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800592 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700593 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700594 } else if (info instanceof PackageItemInfo) {
595 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700596 }
Winsonc0880492015-08-21 11:16:27 -0700597
Sunny Goyal69b75642015-05-15 17:00:24 -0700598 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700599 }
600 }
601
602 /**
603 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
604 */
605 public void verifyHighRes() {
606 if (mIconLoadRequest != null) {
607 mIconLoadRequest.cancel();
608 mIconLoadRequest = null;
609 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800610 if (getTag() instanceof ItemInfoWithIcon) {
611 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700612 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800613 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700614 .updateIconInBackground(BubbleTextView.this, info);
615 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700616 }
617 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700618
Jon Miranda47170112017-03-03 14:57:14 -0800619 public int getIconSize() {
620 return mIconSize;
621 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622}