blob: edbce1068e1da6266c54b290530d3b5e00d5881a [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
Federico Baron4eb502a2022-12-14 00:04:53 -080019import static com.android.launcher3.config.FeatureFlags.ENABLE_DOWNLOAD_APP_UX_V2;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070020import static com.android.launcher3.config.FeatureFlags.ENABLE_ICON_LABEL_AUTO_SCALING;
Sunny Goyal14168432019-10-24 15:59:49 -070021import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon;
Sunny Goyald872a972021-11-24 18:07:04 -080022import static com.android.launcher3.icons.BitmapInfo.FLAG_NO_BADGE;
23import static com.android.launcher3.icons.BitmapInfo.FLAG_THEMED;
Sunny Goyal066ace12018-11-05 11:08:31 -080024import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
Federico Baron4eb502a2022-12-14 00:04:53 -080025import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE;
26import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal066ace12018-11-05 11:08:31 -080027
Tony4043b052018-08-17 13:47:39 -070028import android.animation.Animator;
29import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080030import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070032import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070033import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070035import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070036import android.graphics.Paint;
Adam Cohend9162062020-03-24 16:35:35 -070037import android.graphics.PointF;
Tony Wickham1237df02017-02-24 08:59:36 -080038import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070039import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.drawable.Drawable;
Steven Ng707b1182021-09-13 11:54:58 +010041import android.icu.text.MessageFormat;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070042import android.text.TextPaint;
Andy Wickham56be73b2022-01-18 09:13:43 -080043import android.text.TextUtils;
Sunny Goyal66dccad2018-03-19 12:00:51 -070044import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080045import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080046import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070047import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070048import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080049import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070050import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080051import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080052import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070053
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000054import androidx.annotation.Nullable;
Hyunyoung Song5809a402021-02-02 01:19:18 -080055import androidx.annotation.UiThread;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070056
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -070057import com.android.launcher3.accessibility.BaseAccessibilityDelegate;
Tony Wickhamf34bee82018-12-03 18:11:39 -080058import com.android.launcher3.dot.DotInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070059import com.android.launcher3.dragndrop.DragOptions.PreDragCondition;
Adam Cohen65086992020-02-19 08:40:49 -080060import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070061import com.android.launcher3.folder.FolderIcon;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070062import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080063import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080064import com.android.launcher3.icons.DotRenderer;
Sunny Goyal572aca42021-03-24 15:21:39 -070065import com.android.launcher3.icons.FastBitmapDrawable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080066import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal572aca42021-03-24 15:21:39 -070067import com.android.launcher3.icons.PlaceHolderIconDrawable;
Sunny Goyal79e52fc2021-02-03 10:22:28 -080068import com.android.launcher3.icons.cache.HandlerRunnable;
Sunny Goyale396abf2020-04-06 15:11:17 -070069import com.android.launcher3.model.data.AppInfo;
70import com.android.launcher3.model.data.ItemInfo;
71import com.android.launcher3.model.data.ItemInfoWithIcon;
Sunny Goyale396abf2020-04-06 15:11:17 -070072import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070073import com.android.launcher3.popup.PopupContainerWithArrow;
Samuel Fufa167210a2020-05-12 18:40:11 -070074import com.android.launcher3.util.SafeCloseable;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070075import com.android.launcher3.util.ShortcutUtil;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080076import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070077import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070078
Sunny Goyalc469aad2015-10-01 11:24:23 -070079import java.text.NumberFormat;
Steven Ng707b1182021-09-13 11:54:58 +010080import java.util.HashMap;
81import java.util.Locale;
Sunny Goyalc469aad2015-10-01 11:24:23 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083/**
84 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
85 * because we want to make the bubble taller than the text and TextView's clip is
86 * too aggressive.
87 */
Sunny Goyalb65d7662021-03-07 15:09:11 -080088public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver,
Hyunyoung Songdb9a53d2021-02-17 00:35:10 -080089 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070090
Sunny Goyaldfaccf62015-05-11 16:30:44 -070091 private static final int DISPLAY_WORKSPACE = 0;
92 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070093 private static final int DISPLAY_FOLDER = 2;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -070094 protected static final int DISPLAY_TASKBAR = 5;
95 private static final int DISPLAY_SEARCH_RESULT = 6;
96 private static final int DISPLAY_SEARCH_RESULT_SMALL = 7;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070097
Sunny Goyal486adb02021-10-21 11:29:52 -070098 private static final float MIN_LETTER_SPACING = -0.05f;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070099 private static final int MAX_SEARCH_LOOP_COUNT = 20;
100
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700101 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -0700102
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700103 private final PointF mTranslationForReorderBounce = new PointF(0, 0);
104 private final PointF mTranslationForReorderPreview = new PointF(0, 0);
105
Nick Chameyev4dd41592021-08-27 14:11:53 +0100106 private float mTranslationXForTaskbarAlignmentAnimation = 0f;
107
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100108 private final PointF mTranslationForMoveFromCenterAnimation = new PointF(0, 0);
109
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700110 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800111
Tony Wickhamf34bee82018-12-03 18:11:39 -0800112 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
113 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800114 @Override
115 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800116 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800117 }
118
119 @Override
120 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800121 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800122 bubbleTextView.invalidate();
123 }
124 };
125
Sunny Goyaled7a6932018-04-13 11:41:50 -0700126 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
127 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700128 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700129 public Float get(BubbleTextView bubbleTextView) {
130 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700131 }
132
133 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700134 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700135 bubbleTextView.setTextAlpha(alpha);
136 }
137 };
138
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800139 private final ActivityContext mActivity;
Sunny Goyalc4c32692021-05-21 14:23:29 -0700140 private FastBitmapDrawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800141 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700142
Tony Wickham794fe4f2021-01-11 14:54:23 -0600143 protected final int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700144
Sunny Goyaled7a6932018-04-13 11:41:50 -0700145 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700146
147 private final boolean mLayoutHorizontal;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700148 private final boolean mIsRtl;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700149 private final int mIconSize;
150
151 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyald872a972021-11-24 18:07:04 -0800152 private boolean mHideBadge = false;
153 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700154 private boolean mIsIconVisible = true;
155 @ViewDebug.ExportedProperty(category = "launcher")
156 private int mTextColor;
157 @ViewDebug.ExportedProperty(category = "launcher")
Jon Spivack68862142021-10-28 18:37:19 -0700158 private ColorStateList mTextColorStateList;
159 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700160 private float mTextAlpha = 1;
161
Tony4043b052018-08-17 13:47:39 -0700162 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800163 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800164 private DotRenderer mDotRenderer;
165 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
Sunny Goyal67555872021-05-21 12:56:55 -0700166 protected DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800167 private Animator mDotScaleAnim;
168 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700169
Sunny Goyal4ffec482016-02-09 11:28:52 -0800170 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800171 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800172 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700173 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800174 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700175 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500176
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800177 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700178
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400179 private boolean mEnableIconUpdateAnimation = false;
180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700182 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 }
184
185 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700186 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 }
188
189 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
190 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800191 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700192
Adam Cohen96bb7982014-07-07 11:58:56 -0700193 TypedArray a = context.obtainStyledAttributes(attrs,
194 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800195 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700196 mIsRtl = (getResources().getConfiguration().getLayoutDirection()
197 == View.LAYOUT_DIRECTION_RTL);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700198 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700199
Samuel Fufac96fa242019-09-26 23:06:32 -0700200 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700201 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700202 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700203 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700204 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700205 defaultIconSize = grid.iconSizePx;
Jon Mirandae126d722021-02-25 10:45:20 -0500206 setCenterVertically(grid.isScalableGrid);
Samuel Fufac96fa242019-09-26 23:06:32 -0700207 } else if (mDisplay == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700208 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
209 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700210 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700211 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700212 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700213 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700214 defaultIconSize = grid.folderChildIconSizePx;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700215 } else if (mDisplay == DISPLAY_SEARCH_RESULT) {
216 defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size);
217 } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
218 defaultIconSize = getResources().getDimensionPixelSize(
219 R.dimen.search_row_small_icon_size);
Tony Wickham794fe4f2021-01-11 14:54:23 -0600220 } else if (mDisplay == DISPLAY_TASKBAR) {
221 defaultIconSize = grid.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700222 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700223 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700224 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700225 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700226
Winson1f064272016-07-18 17:18:02 -0700227 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700228
229 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
230 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700231 a.recycle();
232
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800233 mLongPressHelper = new CheckLongPressHelper(this);
234
Tony Wickham8912b042018-11-29 15:28:53 -0800235 mDotParams = new DotRenderer.DrawParams();
236
Sunny Goyal66dccad2018-03-19 12:00:51 -0700237 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700238 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700239 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 }
241
Sunny Goyal66dccad2018-03-19 12:00:51 -0700242 @Override
243 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
244 // Disable marques when not focused to that, so that updating text does not cause relayout.
245 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
246 super.onFocusChanged(focused, direction, previouslyFocusedRect);
247 }
248
Sunny Goyald872a972021-11-24 18:07:04 -0800249 public void setHideBadge(boolean hideBadge) {
250 mHideBadge = hideBadge;
251 }
252
Jon Miranda52549442017-10-26 11:28:06 -0700253 /**
254 * Resets the view so it can be recycled.
255 */
256 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800257 mDotInfo = null;
James O'Leary1557ba32022-05-04 16:49:03 +0000258 mDotParams.dotColor = Color.TRANSPARENT;
259 mDotParams.appColor = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800260 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800261 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800262 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800263 setBackground(null);
Sunny Goyal52f4c162022-04-23 16:02:03 +0000264
265 setTag(null);
266 if (mIconLoadRequest != null) {
267 mIconLoadRequest.cancel();
268 mIconLoadRequest = null;
269 }
Jon Miranda52549442017-10-26 11:28:06 -0700270 }
271
Tony Wickhamf34bee82018-12-03 18:11:39 -0800272 private void cancelDotScaleAnim() {
273 if (mDotScaleAnim != null) {
274 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700275 }
276 }
277
Tony Wickhamf34bee82018-12-03 18:11:39 -0800278 private void animateDotScale(float... dotScales) {
279 cancelDotScaleAnim();
280 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
281 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700282 @Override
283 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800284 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700285 }
286 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800287 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700288 }
289
Hyunyoung Song5809a402021-02-02 01:19:18 -0800290 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700291 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
Tony Wickham175d1522021-08-25 09:18:41 -0700292 applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0);
293 }
294
295 @UiThread
296 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) {
Federico Baron4eb502a2022-12-14 00:04:53 -0800297 applyFromWorkspaceItem(info, null);
Winson Chung5f8afe62013-08-12 16:19:28 -0700298 }
299
Tony Wickham175d1522021-08-25 09:18:41 -0700300 /**
301 * Returns whether the newInfo differs from the current getTag().
302 */
303 public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) {
304 WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo
305 ? (WorkspaceItemInfo) getTag()
306 : null;
307 boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null
308 && newInfo.getTargetComponent() != null
309 && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent());
310 return changedIcons && isShown();
311 }
312
Adam Cohen29794c52019-06-21 12:50:30 -0700313 @Override
314 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -0700315 if (delegate instanceof BaseAccessibilityDelegate) {
Adam Cohen29794c52019-06-21 12:50:30 -0700316 super.setAccessibilityDelegate(delegate);
317 } else {
318 // NO-OP
319 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
320 // delegate incorrectly. There are no cases when we shouldn't be using the
321 // LauncherAccessibilityDelegate for BubbleTextView.
322 }
323 }
324
Hyunyoung Song5809a402021-02-02 01:19:18 -0800325 @UiThread
Federico Baron4eb502a2022-12-14 00:04:53 -0800326 public void applyFromWorkspaceItem(WorkspaceItemInfo info, PreloadIconDrawable icon) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800327 applyIconAndLabel(info);
sfufa@google.comca76de02021-09-24 08:55:48 -0700328 setItemInfo(info);
Federico Baron4eb502a2022-12-14 00:04:53 -0800329 applyLoadingState(icon);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800330 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800331 setDownloadStateContentDescription(info, info.getProgressLevel());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800332 }
333
Hyunyoung Song5809a402021-02-02 01:19:18 -0800334 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700335 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800336 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700337
Sunny Goyal95899162019-03-27 16:03:06 -0700338 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700339 setItemInfo(info);
340
Sunny Goyal34b65272015-03-11 16:56:52 -0700341
342 // Verify high res immediately
343 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800344
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000345 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800346 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700347 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800348 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800349 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal508da152014-08-14 10:53:27 -0700350 }
351
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700352 /**
353 * Apply label and tag using a generic {@link ItemInfoWithIcon}
354 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800355 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700356 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800357 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700358 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700359 setItemInfo(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700360
361 // Verify high res immediately
362 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800363
364 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700365 }
366
Sunny Goyalf0ff70a2022-05-11 07:23:10 -0700367 protected void setItemInfo(ItemInfoWithIcon itemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700368 setTag(itemInfo);
sfufa@google.com0fe464d2021-10-14 14:34:50 -0700369 }
370
Hyunyoung Song5809a402021-02-02 01:19:18 -0800371 @UiThread
Samuel Fufac2820182021-01-22 11:47:25 -0600372 protected void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal59f19152021-08-03 12:25:02 -0700373 boolean useTheme = mDisplay == DISPLAY_WORKSPACE || mDisplay == DISPLAY_FOLDER
374 || mDisplay == DISPLAY_TASKBAR;
Sunny Goyald872a972021-11-24 18:07:04 -0800375 int flags = useTheme ? FLAG_THEMED : 0;
376 if (mHideBadge) {
377 flags |= FLAG_NO_BADGE;
378 }
379 FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags);
James O'Leary1557ba32022-05-04 16:49:03 +0000380 mDotParams.appColor = iconDrawable.getIconColor();
381 mDotParams.dotColor = getContext().getResources()
James O'Learyede514f2022-06-06 16:01:45 -0400382 .getColor(android.R.color.system_accent3_200, getContext().getTheme());
Sunny Goyalf4204382016-07-13 10:46:07 -0700383 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000384 applyLabel(info);
385 }
386
Hyunyoung Song5809a402021-02-02 01:19:18 -0800387 @UiThread
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000388 private void applyLabel(ItemInfoWithIcon info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700389 setText(info.title);
390 if (info.contentDescription != null) {
391 setContentDescription(info.isDisabled()
392 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
393 : info.contentDescription);
394 }
395 }
396
Winson Chung7501adf2015-06-02 11:24:28 -0700397 /**
398 * Overrides the default long press timeout.
399 */
Tony2ca999c2018-09-24 17:24:51 -0400400 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
401 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700402 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800405 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700406 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800407 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800408 }
Sunny Goyal508da152014-08-14 10:53:27 -0700409 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800410
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800411 @Override
412 protected int[] onCreateDrawableState(int extraSpace) {
413 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
414 if (mStayPressed) {
415 mergeDrawableStates(drawableState, STATE_PRESSED);
416 }
417 return drawableState;
418 }
419
Winson Chungb745afb2015-03-02 11:51:23 -0800420 /** Returns the icon for this view. */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700421 public FastBitmapDrawable getIcon() {
Winson Chungb745afb2015-03-02 11:51:23 -0800422 return mIcon;
423 }
424
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700425 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800426 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700427 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700428 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700429 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700430 return false;
431 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700432 if (isLongClickable()) {
433 super.onTouchEvent(event);
434 mLongPressHelper.onTouchEvent(event);
435 // Keep receiving the rest of the events
436 return true;
437 } else {
438 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700439 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800440 }
441
Sunny Goyal44a3b202020-07-09 08:27:06 -0700442 /**
443 * Returns true if the touch down at the provided position be ignored
444 */
445 protected boolean shouldIgnoreTouchDown(float x, float y) {
Tony Wickham42b03952021-08-31 10:41:36 -0700446 if (mDisplay == DISPLAY_TASKBAR) {
447 // Allow touching within padding on taskbar, given icon sizes are smaller.
448 return false;
449 }
Sunny Goyal44a3b202020-07-09 08:27:06 -0700450 return y < getPaddingTop()
451 || x < getPaddingLeft()
452 || y > getHeight() - getPaddingBottom()
453 || x > getWidth() - getPaddingRight();
454 }
455
Michael Jurkaddd62e92011-02-16 17:49:14 -0800456 void setStayPressed(boolean stayPressed) {
457 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800458 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800459 }
460
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700461 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800462 public void onVisibilityAggregated(boolean isVisible) {
463 super.onVisibilityAggregated(isVisible);
464 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800465 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800466 }
467 }
468
Tony Wickham8ac277e2021-05-24 15:47:38 -0700469 public void clearPressedBackground() {
Sunny Goyal508da152014-08-14 10:53:27 -0700470 setPressed(false);
471 setStayPressed(false);
472 }
473
474 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700475 public boolean onKeyUp(int keyCode, KeyEvent event) {
476 // Unlike touch events, keypress event propagate pressed state change immediately,
477 // without waiting for onClickHandler to execute. Disable pressed state changes here
478 // to avoid flickering.
479 mIgnorePressedStateChange = true;
480 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700481 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800482 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700483 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800484 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800485
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700486 @Override
487 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
488 super.onSizeChanged(w, h, oldw, oldh);
489 checkForEllipsis();
490 }
491
492 @Override
493 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
494 super.onTextChanged(text, start, lengthBefore, lengthAfter);
495 checkForEllipsis();
496 }
497
498 private void checkForEllipsis() {
499 if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) {
500 return;
501 }
502 float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
503 if (width <= 0) {
504 return;
505 }
506 setLetterSpacing(0);
507
508 String text = getText().toString();
509 TextPaint paint = getPaint();
510 if (paint.measureText(text) < width) {
511 return;
512 }
513
514 float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING);
515 // Reset the paint value so that the call to TextView does appropriate diff.
516 paint.setLetterSpacing(0);
517 setLetterSpacing(spacing);
518 }
519
520 /**
521 * Find the appropriate text spacing to display the provided text
522 * @param paint the paint used by the text view
523 * @param text the text to display
524 * @param allowedWidthPx available space to render the text
525 * @param minSpacingEm minimum spacing allowed between characters
526 * @return the final textSpacing value
527 *
528 * @see #setLetterSpacing(float)
529 */
530 private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx,
531 float minSpacingEm) {
532 paint.setLetterSpacing(minSpacingEm);
533 if (paint.measureText(text) > allowedWidthPx) {
534 // If there is no result at high limit, we can do anything more
535 return minSpacingEm;
536 }
537
538 float lowLimit = 0;
539 float highLimit = minSpacingEm;
540
541 for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) {
542 float value = (lowLimit + highLimit) / 2;
543 paint.setLetterSpacing(value);
544 if (paint.measureText(text) < allowedWidthPx) {
545 highLimit = value;
546 } else {
547 lowLimit = value;
548 }
549 }
550
551 // At the end error on the higher side
552 return highLimit;
553 }
554
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700555 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800556 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700557 super.onDraw(canvas);
558 }
559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700561 public void onDraw(Canvas canvas) {
562 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800563 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700564 }
565
Tony Wickham1237df02017-02-24 08:59:36 -0800566 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800567 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700568 *
Tony Wickham1237df02017-02-24 08:59:36 -0800569 * @param canvas The canvas to draw to.
570 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800571 protected void drawDotIfNecessary(Canvas canvas) {
572 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800573 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700574 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
575 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800576 final int scrollX = getScrollX();
577 final int scrollY = getScrollY();
578 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800579 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800580 canvas.translate(-scrollX, -scrollY);
581 }
582 }
583
Jon Miranda9b78e192019-08-30 18:42:01 -0700584 @Override
585 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800586 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800587 return;
588 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800589 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800590
Tony Wickhamf34bee82018-12-03 18:11:39 -0800591 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800592 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800593 } else if (hasDot()) {
594 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800595 }
596 }
597
Tony Wickhamf34bee82018-12-03 18:11:39 -0800598 private boolean hasDot() {
599 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800600 }
601
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700602 /**
603 * Get the icon bounds on the view depending on the layout type.
604 */
Tony Wickham1237df02017-02-24 08:59:36 -0800605 public void getIconBounds(Rect outBounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700606 getIconBounds(mIconSize, outBounds);
Tony05d98c22018-07-23 08:01:15 -0700607 }
608
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700609 /**
610 * Get the icon bounds on the view depending on the layout type.
611 */
612 public void getIconBounds(int iconSize, Rect outBounds) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700613 outBounds.set(0, 0, iconSize, iconSize);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700614 if (mLayoutHorizontal) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700615 int top = (getHeight() - iconSize) / 2;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700616 if (mIsRtl) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700617 outBounds.offsetTo(getWidth() - iconSize - getPaddingRight(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700618 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700619 outBounds.offsetTo(getPaddingLeft(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700620 }
621 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700622 outBounds.offset((getWidth() - iconSize) / 2, getPaddingTop());
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700623 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625
Jon Miranda228877d2021-02-09 11:05:00 -0500626 /**
627 * Sets whether to vertically center the content.
628 */
629 public void setCenterVertically(boolean centerVertically) {
630 mCenterVertically = centerVertically;
631 }
632
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 @Override
Winson1f064272016-07-18 17:18:02 -0700634 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
635 if (mCenterVertically) {
636 Paint.FontMetrics fm = getPaint().getFontMetrics();
637 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
638 (int) Math.ceil(fm.bottom - fm.top);
639 int height = MeasureSpec.getSize(heightMeasureSpec);
640 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
641 getPaddingBottom());
642 }
643 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
644 }
645
646 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700647 public void setTextColor(int color) {
648 mTextColor = color;
Jon Spivack68862142021-10-28 18:37:19 -0700649 mTextColorStateList = null;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700650 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700651 }
652
Adam Cohen96bb7982014-07-07 11:58:56 -0700653 @Override
654 public void setTextColor(ColorStateList colors) {
655 mTextColor = colors.getDefaultColor();
Jon Spivack68862142021-10-28 18:37:19 -0700656 mTextColorStateList = colors;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700657 if (Float.compare(mTextAlpha, 1) == 0) {
658 super.setTextColor(colors);
659 } else {
660 super.setTextColor(getModifiedColor());
661 }
Adam Cohen477828c2013-09-20 12:05:49 -0700662 }
663
Tony7308cde2017-06-27 22:38:33 -0700664 public boolean shouldTextBeVisible() {
665 // Text should be visible everywhere but the hotseat.
666 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
667 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700668 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
669 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700670 }
671
Winson Chung5f8afe62013-08-12 16:19:28 -0700672 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700673 setTextAlpha(visible ? 1 : 0);
674 }
675
676 private void setTextAlpha(float alpha) {
677 mTextAlpha = alpha;
Jon Spivack68862142021-10-28 18:37:19 -0700678 if (mTextColorStateList != null) {
679 setTextColor(mTextColorStateList);
680 } else {
681 super.setTextColor(getModifiedColor());
682 }
Sunny Goyaled7a6932018-04-13 11:41:50 -0700683 }
684
685 private int getModifiedColor() {
686 if (mTextAlpha == 0) {
687 // Special case to prevent text shadows in high contrast mode
688 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700689 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800690 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700691 }
692
693 /**
694 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700695 *
Tony8f402802017-06-16 17:24:54 -0700696 * @param fadeIn Whether the text should fade in or fade out.
697 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700698 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700699 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700700 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700701 }
702
Winson Chungaffd7b42010-08-20 15:11:56 -0700703 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800704 public void cancelLongPress() {
705 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800706 mLongPressHelper.cancelLongPress();
707 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500708
Samuel Fufac2820182021-01-22 11:47:25 -0600709 /**
710 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000711 *
712 * If this app is installing, the progress bar will be updated with the installation progress.
713 * If this app is installed and downloading incrementally, the progress bar will be updated
714 * with the total download progress.
715 */
Federico Baron4eb502a2022-12-14 00:04:53 -0800716 public void applyLoadingState(PreloadIconDrawable icon) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800717 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700718 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Federico Baron4eb502a2022-12-14 00:04:53 -0800719 if ((info.runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0
720 || info.hasPromiseIconUi()
721 || (info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0
722 || (ENABLE_DOWNLOAD_APP_UX_V2.get() && icon != null)) {
723 updateProgressBarUi(icon);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700724 }
725 }
726 }
727
Federico Baron4eb502a2022-12-14 00:04:53 -0800728 private void updateProgressBarUi(PreloadIconDrawable oldIcon) {
729 FastBitmapDrawable originalIcon = mIcon;
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800730 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Federico Baron4eb502a2022-12-14 00:04:53 -0800731 if (preloadDrawable != null && oldIcon != null) {
732 preloadDrawable.maybePerformFinishedAnimation(oldIcon, () -> setIcon(originalIcon));
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000733 }
734 }
735
736 /** Applies the given progress level to the this icon's progress bar. */
737 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800738 public PreloadIconDrawable applyProgressLevel() {
739 if (!(getTag() instanceof ItemInfoWithIcon)) {
740 return null;
741 }
742
743 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
744 int progressLevel = info.getProgressLevel();
745 if (progressLevel >= 100) {
746 setContentDescription(info.contentDescription != null
747 ? info.contentDescription : "");
748 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800749 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800750 } else {
751 setContentDescription(getContext()
752 .getString(R.string.app_waiting_download_title, info.title));
753 }
754 if (mIcon != null) {
755 PreloadIconDrawable preloadIconDrawable;
756 if (mIcon instanceof PreloadIconDrawable) {
757 preloadIconDrawable = (PreloadIconDrawable) mIcon;
758 preloadIconDrawable.setLevel(progressLevel);
759 preloadIconDrawable.setIsDisabled(!info.isAppStartable());
Mario Bertschler230612f2017-09-27 17:05:21 -0700760 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800761 preloadIconDrawable = makePreloadIcon();
762 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700763 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800764 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400765 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700766 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400767 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700768
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800769 /**
770 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
771 * object.
772 */
773 @Nullable
774 public PreloadIconDrawable makePreloadIcon() {
775 if (!(getTag() instanceof ItemInfoWithIcon)) {
776 return null;
777 }
778
779 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
780 int progressLevel = info.getProgressLevel();
781 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
782
783 preloadDrawable.setLevel(progressLevel);
784 preloadDrawable.setIsDisabled(!info.isAppStartable());
785
786 return preloadDrawable;
787 }
788
Tony Wickhamf34bee82018-12-03 18:11:39 -0800789 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800790 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800791 boolean wasDotted = mDotInfo != null;
792 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
793 boolean isDotted = mDotInfo != null;
794 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700795 if (mDisplay == DISPLAY_ALL_APPS) {
796 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
797 } else {
798 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
799 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800800 if (wasDotted || isDotted) {
801 // Animate when a dot is first added or when it is removed.
802 if (animate && (wasDotted ^ isDotted) && isShown()) {
803 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800804 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800805 cancelDotScaleAnim();
806 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800807 invalidate();
808 }
809 }
Andy Wickham56be73b2022-01-18 09:13:43 -0800810 if (!TextUtils.isEmpty(itemInfo.contentDescription)) {
vadimtda6dad52019-05-21 13:24:23 -0700811 if (itemInfo.isDisabled()) {
812 setContentDescription(getContext().getString(R.string.disabled_app_label,
813 itemInfo.contentDescription));
814 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800815 int count = mDotInfo.getNotificationCount();
Steven Ng707b1182021-09-13 11:54:58 +0100816 setContentDescription(
817 getAppLabelPluralString(itemInfo.contentDescription.toString(), count));
Tony Wickham305e9202018-01-23 17:46:47 -0800818 } else {
819 setContentDescription(itemInfo.contentDescription);
820 }
821 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800822 }
823 }
824
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800825 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
826 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
827 != 0) {
828 String percentageString = NumberFormat.getPercentInstance()
829 .format(progressLevel * 0.01);
Federico Baron4eb502a2022-12-14 00:04:53 -0800830 if ((info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800831 setContentDescription(getContext()
832 .getString(
833 R.string.app_installing_title, info.title, percentageString));
834 } else if ((info.runtimeStatusFlags
Federico Baron4eb502a2022-12-14 00:04:53 -0800835 & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800836 setContentDescription(getContext()
837 .getString(
838 R.string.app_downloading_title, info.title, percentageString));
839 }
840 }
841 }
842
Winson Chungb745afb2015-03-02 11:51:23 -0800843 /**
844 * Sets the icon for this view based on the layout direction.
845 */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700846 protected void setIcon(FastBitmapDrawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700847 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700848 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700849 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700850 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800851 if (mIcon != null) {
852 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
853 }
Tony17b7f9b2017-05-23 12:19:09 -0700854 }
855
Jon Miranda9b78e192019-08-30 18:42:01 -0700856 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700857 public void setIconVisible(boolean visible) {
858 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700859 if (!mIsIconVisible) {
860 resetIconScale();
861 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700862 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700863 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700864 }
865
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400866 protected boolean iconUpdateAnimationEnabled() {
867 return mEnableIconUpdateAnimation;
868 }
869
Tony Wickham377ed3f2016-07-20 15:21:04 -0700870 protected void applyCompoundDrawables(Drawable icon) {
Jon Mirandafe749972022-03-15 10:02:39 -0700871 if (icon == null) {
872 // Icon can be null when we use the BubbleTextView for text only.
873 return;
874 }
875
Sunny Goyal66dccad2018-03-19 12:00:51 -0700876 // If we had already set an icon before, disable relayout as the icon size is the
877 // same as before.
878 mDisableRelayout = mIcon != null;
879
880 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700881
882 updateIcon(icon);
883
884 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -0400885 if (mIcon != null
886 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400887 && iconUpdateAnimationEnabled()) {
Steven Nge92bc552021-02-10 17:10:15 +0000888 ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon);
Winson Chungb745afb2015-03-02 11:51:23 -0800889 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700890
Sunny Goyal66dccad2018-03-19 12:00:51 -0700891 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800892 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700893
Sunny Goyal69b75642015-05-15 17:00:24 -0700894 @Override
895 public void requestLayout() {
896 if (!mDisableRelayout) {
897 super.requestLayout();
898 }
899 }
900
Sunny Goyal34b65272015-03-11 16:56:52 -0700901 /**
902 * Applies the item info if it is same as what the view is pointing to currently.
903 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800904 @Override
905 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700906 if (getTag() == info) {
907 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700908 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400909 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -0700910
Jon Miranda7f522a22017-07-28 11:56:47 -0700911 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -0700912 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -0700913
Sunny Goyal34b65272015-03-11 16:56:52 -0700914 if (info instanceof AppInfo) {
915 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700916 } else if (info instanceof WorkspaceItemInfo) {
917 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700918 mActivity.invalidateParent(info);
Sunny Goyalf0ff70a2022-05-11 07:23:10 -0700919 } else if (info != null) {
920 applyFromItemInfoWithIcon(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700921 }
Winsonc0880492015-08-21 11:16:27 -0700922
Sunny Goyal69b75642015-05-15 17:00:24 -0700923 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400924 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700925 }
926 }
927
928 /**
929 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
930 */
931 public void verifyHighRes() {
932 if (mIconLoadRequest != null) {
933 mIconLoadRequest.cancel();
934 mIconLoadRequest = null;
935 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800936 if (getTag() instanceof ItemInfoWithIcon) {
937 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700938 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800939 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700940 .updateIconInBackground(BubbleTextView.this, info);
941 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700942 }
943 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700944
Jon Miranda47170112017-03-03 14:57:14 -0800945 public int getIconSize() {
946 return mIconSize;
947 }
Adam Cohen65086992020-02-19 08:40:49 -0800948
Brandon Dayauond3755f42022-08-29 11:48:27 -0700949 public boolean isDisplaySearchResult() {
950 return mDisplay == DISPLAY_SEARCH_RESULT ||
951 mDisplay == DISPLAY_SEARCH_RESULT_SMALL;
952 }
953
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700954 private void updateTranslation() {
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100955 super.setTranslationX(mTranslationForReorderBounce.x + mTranslationForReorderPreview.x
Nick Chameyev4dd41592021-08-27 14:11:53 +0100956 + mTranslationForMoveFromCenterAnimation.x
957 + mTranslationXForTaskbarAlignmentAnimation);
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100958 super.setTranslationY(mTranslationForReorderBounce.y + mTranslationForReorderPreview.y
959 + mTranslationForMoveFromCenterAnimation.y);
Adam Cohend9162062020-03-24 16:35:35 -0700960 }
961
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700962 public void setReorderBounceOffset(float x, float y) {
963 mTranslationForReorderBounce.set(x, y);
964 updateTranslation();
Adam Cohend9162062020-03-24 16:35:35 -0700965 }
966
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700967 public void getReorderBounceOffset(PointF offset) {
968 offset.set(mTranslationForReorderBounce);
969 }
970
971 @Override
972 public void setReorderPreviewOffset(float x, float y) {
973 mTranslationForReorderPreview.set(x, y);
974 updateTranslation();
975 }
976
977 @Override
978 public void getReorderPreviewOffset(PointF offset) {
979 offset.set(mTranslationForReorderPreview);
980 }
981
982 public void setReorderBounceScale(float scale) {
983 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -0700984 super.setScaleX(scale);
985 super.setScaleY(scale);
986 }
987
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700988 public float getReorderBounceScale() {
989 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -0700990 }
991
Nick Chameyev4dd41592021-08-27 14:11:53 +0100992 /**
993 * Sets translation values for move from center animation
994 */
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100995 public void setTranslationForMoveFromCenterAnimation(float x, float y) {
996 mTranslationForMoveFromCenterAnimation.set(x, y);
997 updateTranslation();
998 }
999
Nick Chameyev4dd41592021-08-27 14:11:53 +01001000 /**
1001 * Sets translationX for taskbar to launcher alignment animation
1002 */
1003 public void setTranslationXForTaskbarAlignmentAnimation(float translationX) {
1004 mTranslationXForTaskbarAlignmentAnimation = translationX;
1005 updateTranslation();
1006 }
1007
1008 /**
1009 * Returns translationX value for taskbar to launcher alignment animation
1010 */
1011 public float getTranslationXForTaskbarAlignmentAnimation() {
1012 return mTranslationXForTaskbarAlignmentAnimation;
1013 }
1014
Adam Cohend9162062020-03-24 16:35:35 -07001015 public View getView() {
1016 return this;
1017 }
1018
Adam Cohen65086992020-02-19 08:40:49 -08001019 @Override
1020 public int getViewType() {
1021 return DRAGGABLE_ICON;
1022 }
1023
1024 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -07001025 public void getWorkspaceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001026 getIconBounds(mIconSize, bounds);
Adam Cohen65086992020-02-19 08:40:49 -08001027 }
1028
Adam Cohenc77bc452020-05-07 11:55:19 -07001029 public void getSourceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001030 getIconBounds(mIconSize, bounds);
Adam Cohenc77bc452020-05-07 11:55:19 -07001031 }
1032
Adam Cohen65086992020-02-19 08:40:49 -08001033 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -07001034 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001035 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -08001036 setForceHideDot(true);
Sunny Goyal8b9919d2021-04-07 14:45:17 -07001037 return () -> { };
Adam Cohen65086992020-02-19 08:40:49 -08001038 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001039
1040 private void resetIconScale() {
Sunny Goyal52f4c162022-04-23 16:02:03 +00001041 if (mIcon != null) {
1042 mIcon.resetScale();
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001043 }
1044 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001045
1046 private void updateIcon(Drawable newIcon) {
1047 if (mLayoutHorizontal) {
1048 setCompoundDrawablesRelative(newIcon, null, null, null);
1049 } else {
1050 setCompoundDrawables(null, newIcon, null, null);
1051 }
1052 }
Steven Ng707b1182021-09-13 11:54:58 +01001053
1054 private String getAppLabelPluralString(String appName, int notificationCount) {
1055 MessageFormat icuCountFormat = new MessageFormat(
1056 getResources().getString(R.string.dotted_app_label),
1057 Locale.getDefault());
1058 HashMap<String, Object> args = new HashMap();
1059 args.put("app_name", appName);
1060 args.put("count", notificationCount);
1061 return icuCountFormat.format(args);
1062 }
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001063
1064 /**
1065 * Starts a long press action and returns the corresponding pre-drag condition
1066 */
1067 public PreDragCondition startLongPressAction() {
1068 PopupContainerWithArrow popup = PopupContainerWithArrow.showForIcon(this);
1069 return popup != null ? popup.createPreDragCondition(true) : null;
1070 }
1071
1072 /**
1073 * Returns true if the view can show long-press popup
1074 */
1075 public boolean canShowLongPressPopup() {
1076 return getTag() instanceof ItemInfo && ShortcutUtil.supportsShortcuts((ItemInfo) getTag());
1077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078}