blob: b3230983f59f34ee1156550752e88515ead942b4 [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
Pat Manningd0f729d2023-01-09 12:04:25 +000019import static com.android.launcher3.config.FeatureFlags.ENABLE_CURSOR_HOVER_STATES;
Federico Baron4eb502a2022-12-14 00:04:53 -080020import static com.android.launcher3.config.FeatureFlags.ENABLE_DOWNLOAD_APP_UX_V2;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070021import static com.android.launcher3.config.FeatureFlags.ENABLE_ICON_LABEL_AUTO_SCALING;
Sunny Goyal14168432019-10-24 15:59:49 -070022import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon;
Sunny Goyald872a972021-11-24 18:07:04 -080023import static com.android.launcher3.icons.BitmapInfo.FLAG_NO_BADGE;
24import static com.android.launcher3.icons.BitmapInfo.FLAG_THEMED;
Sunny Goyal066ace12018-11-05 11:08:31 -080025import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
Federico Baron4eb502a2022-12-14 00:04:53 -080026import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE;
27import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal066ace12018-11-05 11:08:31 -080028
Tony4043b052018-08-17 13:47:39 -070029import android.animation.Animator;
30import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080031import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070033import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070034import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070036import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070037import android.graphics.Paint;
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;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080056import androidx.annotation.VisibleForTesting;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070057
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -070058import com.android.launcher3.accessibility.BaseAccessibilityDelegate;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080059import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf34bee82018-12-03 18:11:39 -080060import com.android.launcher3.dot.DotInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070061import com.android.launcher3.dragndrop.DragOptions.PreDragCondition;
Adam Cohen65086992020-02-19 08:40:49 -080062import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070063import com.android.launcher3.folder.FolderIcon;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070064import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080065import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080066import com.android.launcher3.icons.DotRenderer;
Sunny Goyal572aca42021-03-24 15:21:39 -070067import com.android.launcher3.icons.FastBitmapDrawable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080068import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal572aca42021-03-24 15:21:39 -070069import com.android.launcher3.icons.PlaceHolderIconDrawable;
Sunny Goyal79e52fc2021-02-03 10:22:28 -080070import com.android.launcher3.icons.cache.HandlerRunnable;
Sunny Goyale396abf2020-04-06 15:11:17 -070071import com.android.launcher3.model.data.AppInfo;
72import com.android.launcher3.model.data.ItemInfo;
73import com.android.launcher3.model.data.ItemInfoWithIcon;
Sunny Goyale396abf2020-04-06 15:11:17 -070074import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070075import com.android.launcher3.popup.PopupContainerWithArrow;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080076import com.android.launcher3.search.StringMatcherUtility;
77import com.android.launcher3.util.IntArray;
Tony Wickham1e29b042023-03-16 19:58:30 +000078import com.android.launcher3.util.MultiTranslateDelegate;
Samuel Fufa167210a2020-05-12 18:40:11 -070079import com.android.launcher3.util.SafeCloseable;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070080import com.android.launcher3.util.ShortcutUtil;
fbaron1406a0f2023-05-11 08:46:46 -070081import com.android.launcher3.util.Themes;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080082import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070083import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070084
Sunny Goyalc469aad2015-10-01 11:24:23 -070085import java.text.NumberFormat;
Steven Ng707b1182021-09-13 11:54:58 +010086import java.util.HashMap;
87import java.util.Locale;
Sunny Goyalc469aad2015-10-01 11:24:23 -070088
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089/**
90 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
91 * because we want to make the bubble taller than the text and TextView's clip is
92 * too aggressive.
93 */
Sunny Goyalb65d7662021-03-07 15:09:11 -080094public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver,
Hyunyoung Songdb9a53d2021-02-17 00:35:10 -080095 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070096
Sunny Goyaldfaccf62015-05-11 16:30:44 -070097 private static final int DISPLAY_WORKSPACE = 0;
Brandon Dayauonf2ea3992023-06-26 12:13:30 -070098 public static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070099 private static final int DISPLAY_FOLDER = 2;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700100 protected static final int DISPLAY_TASKBAR = 5;
Brandon Dayauon7e376162023-07-10 16:17:26 -0700101 public static final int DISPLAY_SEARCH_RESULT = 6;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700102 private static final int DISPLAY_SEARCH_RESULT_SMALL = 7;
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700103 public static final int DISPLAY_PREDICTION_ROW = 8;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700104
Sunny Goyal486adb02021-10-21 11:29:52 -0700105 private static final float MIN_LETTER_SPACING = -0.05f;
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700106 private static final int MAX_SEARCH_LOOP_COUNT = 20;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800107 private static final Character NEW_LINE = '\n';
108 private static final String EMPTY = "";
109 private static final StringMatcherUtility.StringMatcher MATCHER =
110 StringMatcherUtility.StringMatcher.getInstance();
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700111
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700112 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -0700113
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700114 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800115
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800116 private IntArray mBreakPointsIntArray;
117 private CharSequence mLastOriginalText;
118 private CharSequence mLastModifiedText;
119
Tony Wickhamf34bee82018-12-03 18:11:39 -0800120 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
121 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800122 @Override
123 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800124 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800125 }
126
127 @Override
128 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800129 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800130 bubbleTextView.invalidate();
131 }
132 };
133
Sunny Goyaled7a6932018-04-13 11:41:50 -0700134 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
135 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700136 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700137 public Float get(BubbleTextView bubbleTextView) {
138 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700139 }
140
141 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700142 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700143 bubbleTextView.setTextAlpha(alpha);
144 }
145 };
146
Sunny Goyal82dfc152023-02-24 16:50:09 -0800147 private final MultiTranslateDelegate mTranslateDelegate = new MultiTranslateDelegate(this);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800148 private final ActivityContext mActivity;
Sunny Goyalc4c32692021-05-21 14:23:29 -0700149 private FastBitmapDrawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800150 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700151
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800152 protected int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700153
Sunny Goyaled7a6932018-04-13 11:41:50 -0700154 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700155
156 private final boolean mLayoutHorizontal;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700157 private final boolean mIsRtl;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700158 private final int mIconSize;
159
160 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyald872a972021-11-24 18:07:04 -0800161 private boolean mHideBadge = false;
162 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700163 private boolean mIsIconVisible = true;
164 @ViewDebug.ExportedProperty(category = "launcher")
165 private int mTextColor;
166 @ViewDebug.ExportedProperty(category = "launcher")
Jon Spivack68862142021-10-28 18:37:19 -0700167 private ColorStateList mTextColorStateList;
168 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700169 private float mTextAlpha = 1;
170
Tony4043b052018-08-17 13:47:39 -0700171 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800172 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800173 private DotRenderer mDotRenderer;
174 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
Sunny Goyal67555872021-05-21 12:56:55 -0700175 protected DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800176 private Animator mDotScaleAnim;
177 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700178
Sunny Goyal4ffec482016-02-09 11:28:52 -0800179 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800180 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800181 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700182 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800183 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700184 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500185
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800186 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700187
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400188 private boolean mEnableIconUpdateAnimation = false;
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700191 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 }
193
194 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700195 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 }
197
198 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
199 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800200 mActivity = ActivityContext.lookupContext(context);
Pat Manningd0f729d2023-01-09 12:04:25 +0000201 FastBitmapDrawable.setFlagHoverEnabled(ENABLE_CURSOR_HOVER_STATES.get());
Adam Cohen96bb7982014-07-07 11:58:56 -0700202
Adam Cohen96bb7982014-07-07 11:58:56 -0700203 TypedArray a = context.obtainStyledAttributes(attrs,
204 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800205 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700206 mIsRtl = (getResources().getConfiguration().getLayoutDirection()
207 == View.LAYOUT_DIRECTION_RTL);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700208 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700209
Samuel Fufac96fa242019-09-26 23:06:32 -0700210 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700211 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700212 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700213 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700214 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700215 defaultIconSize = grid.iconSizePx;
Thales Lima79456492023-05-23 11:32:22 +0100216 setCenterVertically(grid.iconCenterVertically);
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700217 } else if (mDisplay == DISPLAY_ALL_APPS || mDisplay == DISPLAY_PREDICTION_ROW) {
Winson1f064272016-07-18 17:18:02 -0700218 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
219 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700220 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700221 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700222 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700223 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700224 defaultIconSize = grid.folderChildIconSizePx;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700225 } else if (mDisplay == DISPLAY_SEARCH_RESULT) {
Brandon Dayauonb1957cd2023-03-14 13:27:30 -0700226 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700227 defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size);
228 } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
229 defaultIconSize = getResources().getDimensionPixelSize(
230 R.dimen.search_row_small_icon_size);
Tony Wickham794fe4f2021-01-11 14:54:23 -0600231 } else if (mDisplay == DISPLAY_TASKBAR) {
232 defaultIconSize = grid.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700233 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700234 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700235 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700236 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700237
Winson1f064272016-07-18 17:18:02 -0700238 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700239
240 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
241 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700242 a.recycle();
243
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800244 mLongPressHelper = new CheckLongPressHelper(this);
245
Tony Wickham8912b042018-11-29 15:28:53 -0800246 mDotParams = new DotRenderer.DrawParams();
247
Sunny Goyal66dccad2018-03-19 12:00:51 -0700248 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700249 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700250 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 }
252
Sunny Goyal66dccad2018-03-19 12:00:51 -0700253 @Override
254 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
255 // Disable marques when not focused to that, so that updating text does not cause relayout.
256 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
257 super.onFocusChanged(focused, direction, previouslyFocusedRect);
258 }
259
Sunny Goyald872a972021-11-24 18:07:04 -0800260 public void setHideBadge(boolean hideBadge) {
261 mHideBadge = hideBadge;
262 }
263
Jon Miranda52549442017-10-26 11:28:06 -0700264 /**
265 * Resets the view so it can be recycled.
266 */
267 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800268 mDotInfo = null;
James O'Leary1557ba32022-05-04 16:49:03 +0000269 mDotParams.dotColor = Color.TRANSPARENT;
270 mDotParams.appColor = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800271 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800272 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800273 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800274 setBackground(null);
hyunyoungs825480f2023-03-24 10:11:15 -0700275 if (FeatureFlags.ENABLE_TWOLINE_ALLAPPS.get()
276 || FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()) {
277 setMaxLines(1);
278 }
Sunny Goyal52f4c162022-04-23 16:02:03 +0000279
280 setTag(null);
281 if (mIconLoadRequest != null) {
282 mIconLoadRequest.cancel();
283 mIconLoadRequest = null;
284 }
Jon Miranda52549442017-10-26 11:28:06 -0700285 }
286
Tony Wickhamf34bee82018-12-03 18:11:39 -0800287 private void cancelDotScaleAnim() {
288 if (mDotScaleAnim != null) {
289 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700290 }
291 }
292
Tony Wickhamf34bee82018-12-03 18:11:39 -0800293 private void animateDotScale(float... dotScales) {
294 cancelDotScaleAnim();
295 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
296 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700297 @Override
298 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800299 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700300 }
301 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800302 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700303 }
304
Hyunyoung Song5809a402021-02-02 01:19:18 -0800305 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700306 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
Tony Wickham175d1522021-08-25 09:18:41 -0700307 applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0);
308 }
309
310 @UiThread
311 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) {
Federico Baron4eb502a2022-12-14 00:04:53 -0800312 applyFromWorkspaceItem(info, null);
Winson Chung5f8afe62013-08-12 16:19:28 -0700313 }
314
Tony Wickham175d1522021-08-25 09:18:41 -0700315 /**
316 * Returns whether the newInfo differs from the current getTag().
317 */
318 public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) {
319 WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo
320 ? (WorkspaceItemInfo) getTag()
321 : null;
322 boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null
323 && newInfo.getTargetComponent() != null
324 && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent());
325 return changedIcons && isShown();
326 }
327
Adam Cohen29794c52019-06-21 12:50:30 -0700328 @Override
329 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -0700330 if (delegate instanceof BaseAccessibilityDelegate) {
Adam Cohen29794c52019-06-21 12:50:30 -0700331 super.setAccessibilityDelegate(delegate);
332 } else {
333 // NO-OP
334 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
335 // delegate incorrectly. There are no cases when we shouldn't be using the
336 // LauncherAccessibilityDelegate for BubbleTextView.
337 }
338 }
339
Hyunyoung Song5809a402021-02-02 01:19:18 -0800340 @UiThread
Federico Baron4eb502a2022-12-14 00:04:53 -0800341 public void applyFromWorkspaceItem(WorkspaceItemInfo info, PreloadIconDrawable icon) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800342 applyIconAndLabel(info);
sfufa@google.comca76de02021-09-24 08:55:48 -0700343 setItemInfo(info);
Federico Baron4eb502a2022-12-14 00:04:53 -0800344 applyLoadingState(icon);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800345 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800346 setDownloadStateContentDescription(info, info.getProgressLevel());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800347 }
348
Hyunyoung Song5809a402021-02-02 01:19:18 -0800349 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700350 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800351 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700352
Sunny Goyal95899162019-03-27 16:03:06 -0700353 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700354 setItemInfo(info);
355
Sunny Goyal34b65272015-03-11 16:56:52 -0700356
357 // Verify high res immediately
358 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800359
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000360 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800361 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700362 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800363 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800364 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal508da152014-08-14 10:53:27 -0700365 }
366
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700367 /**
368 * Apply label and tag using a generic {@link ItemInfoWithIcon}
369 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800370 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700371 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800372 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700373 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700374 setItemInfo(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700375
376 // Verify high res immediately
377 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800378
379 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700380 }
381
Sunny Goyalf0ff70a2022-05-11 07:23:10 -0700382 protected void setItemInfo(ItemInfoWithIcon itemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700383 setTag(itemInfo);
sfufa@google.com0fe464d2021-10-14 14:34:50 -0700384 }
385
Hyunyoung Song5809a402021-02-02 01:19:18 -0800386 @UiThread
Samuel Fufac2820182021-01-22 11:47:25 -0600387 protected void applyIconAndLabel(ItemInfoWithIcon info) {
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800388 int flags = shouldUseTheme() ? FLAG_THEMED : 0;
Sunny Goyald872a972021-11-24 18:07:04 -0800389 if (mHideBadge) {
390 flags |= FLAG_NO_BADGE;
391 }
392 FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags);
James O'Leary1557ba32022-05-04 16:49:03 +0000393 mDotParams.appColor = iconDrawable.getIconColor();
fbaron1406a0f2023-05-11 08:46:46 -0700394 mDotParams.dotColor = Themes.getAttrColor(getContext(), R.attr.notificationDotColor);
Sunny Goyalf4204382016-07-13 10:46:07 -0700395 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000396 applyLabel(info);
397 }
398
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800399 protected boolean shouldUseTheme() {
400 return mDisplay == DISPLAY_WORKSPACE || mDisplay == DISPLAY_FOLDER
401 || mDisplay == DISPLAY_TASKBAR;
402 }
403
Brandon Dayauon7d673312023-04-03 12:32:24 -0700404 /**
405 * Only if actual text can be displayed in two line, the {@code true} value will be effective.
406 */
407 protected boolean shouldUseTwoLine() {
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700408 return (FeatureFlags.ENABLE_TWOLINE_ALLAPPS.get() && mDisplay == DISPLAY_ALL_APPS)
Brandon Dayauon7d673312023-04-03 12:32:24 -0700409 || (FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()
410 && mDisplay == DISPLAY_SEARCH_RESULT);
411 }
412
Hyunyoung Song5809a402021-02-02 01:19:18 -0800413 @UiThread
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800414 @VisibleForTesting
415 public void applyLabel(ItemInfoWithIcon info) {
416 CharSequence label = info.title;
417 if (label != null) {
418 mLastOriginalText = label;
419 mLastModifiedText = mLastOriginalText;
420 mBreakPointsIntArray = StringMatcherUtility.getListOfBreakpoints(label, MATCHER);
421 setText(label);
422 }
Sunny Goyalf4204382016-07-13 10:46:07 -0700423 if (info.contentDescription != null) {
424 setContentDescription(info.isDisabled()
425 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
426 : info.contentDescription);
427 }
428 }
429
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700430 /** This is used for testing to forcefully set the display. */
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800431 @VisibleForTesting
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700432 public void setDisplay(int display) {
433 mDisplay = display;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800434 }
435
Winson Chung7501adf2015-06-02 11:24:28 -0700436 /**
437 * Overrides the default long press timeout.
438 */
Tony2ca999c2018-09-24 17:24:51 -0400439 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
440 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700441 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800444 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700445 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800446 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800447 }
Sunny Goyal508da152014-08-14 10:53:27 -0700448 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800449
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800450 @Override
451 protected int[] onCreateDrawableState(int extraSpace) {
452 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
453 if (mStayPressed) {
454 mergeDrawableStates(drawableState, STATE_PRESSED);
455 }
456 return drawableState;
457 }
458
Winson Chungb745afb2015-03-02 11:51:23 -0800459 /** Returns the icon for this view. */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700460 public FastBitmapDrawable getIcon() {
Winson Chungb745afb2015-03-02 11:51:23 -0800461 return mIcon;
462 }
463
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700464 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800465 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700466 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700467 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700468 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700469 return false;
470 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700471 if (isLongClickable()) {
472 super.onTouchEvent(event);
473 mLongPressHelper.onTouchEvent(event);
474 // Keep receiving the rest of the events
475 return true;
476 } else {
477 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700478 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800479 }
480
Sunny Goyal44a3b202020-07-09 08:27:06 -0700481 /**
482 * Returns true if the touch down at the provided position be ignored
483 */
484 protected boolean shouldIgnoreTouchDown(float x, float y) {
Tony Wickham42b03952021-08-31 10:41:36 -0700485 if (mDisplay == DISPLAY_TASKBAR) {
486 // Allow touching within padding on taskbar, given icon sizes are smaller.
487 return false;
488 }
Sunny Goyal44a3b202020-07-09 08:27:06 -0700489 return y < getPaddingTop()
490 || x < getPaddingLeft()
491 || y > getHeight() - getPaddingBottom()
492 || x > getWidth() - getPaddingRight();
493 }
494
Michael Jurkaddd62e92011-02-16 17:49:14 -0800495 void setStayPressed(boolean stayPressed) {
496 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800497 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800498 }
499
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700500 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800501 public void onVisibilityAggregated(boolean isVisible) {
502 super.onVisibilityAggregated(isVisible);
503 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800504 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800505 }
506 }
507
Tony Wickham8ac277e2021-05-24 15:47:38 -0700508 public void clearPressedBackground() {
Sunny Goyal508da152014-08-14 10:53:27 -0700509 setPressed(false);
510 setStayPressed(false);
511 }
512
513 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700514 public boolean onKeyUp(int keyCode, KeyEvent event) {
515 // Unlike touch events, keypress event propagate pressed state change immediately,
516 // without waiting for onClickHandler to execute. Disable pressed state changes here
517 // to avoid flickering.
518 mIgnorePressedStateChange = true;
519 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700520 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800521 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700522 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800523 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800524
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700525 @Override
526 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
527 super.onSizeChanged(w, h, oldw, oldh);
528 checkForEllipsis();
529 }
530
531 @Override
532 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
533 super.onTextChanged(text, start, lengthBefore, lengthAfter);
534 checkForEllipsis();
535 }
536
537 private void checkForEllipsis() {
538 if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) {
539 return;
540 }
541 float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
542 if (width <= 0) {
543 return;
544 }
545 setLetterSpacing(0);
546
547 String text = getText().toString();
548 TextPaint paint = getPaint();
549 if (paint.measureText(text) < width) {
550 return;
551 }
552
553 float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING);
554 // Reset the paint value so that the call to TextView does appropriate diff.
555 paint.setLetterSpacing(0);
556 setLetterSpacing(spacing);
557 }
558
559 /**
560 * Find the appropriate text spacing to display the provided text
561 * @param paint the paint used by the text view
562 * @param text the text to display
563 * @param allowedWidthPx available space to render the text
564 * @param minSpacingEm minimum spacing allowed between characters
565 * @return the final textSpacing value
566 *
567 * @see #setLetterSpacing(float)
568 */
569 private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx,
570 float minSpacingEm) {
571 paint.setLetterSpacing(minSpacingEm);
572 if (paint.measureText(text) > allowedWidthPx) {
573 // If there is no result at high limit, we can do anything more
574 return minSpacingEm;
575 }
576
577 float lowLimit = 0;
578 float highLimit = minSpacingEm;
579
580 for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) {
581 float value = (lowLimit + highLimit) / 2;
582 paint.setLetterSpacing(value);
583 if (paint.measureText(text) < allowedWidthPx) {
584 highLimit = value;
585 } else {
586 lowLimit = value;
587 }
588 }
589
590 // At the end error on the higher side
591 return highLimit;
592 }
593
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700594 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800595 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700596 super.onDraw(canvas);
597 }
598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700600 public void onDraw(Canvas canvas) {
601 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800602 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700603 }
604
Tony Wickham1237df02017-02-24 08:59:36 -0800605 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800606 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700607 *
Tony Wickham1237df02017-02-24 08:59:36 -0800608 * @param canvas The canvas to draw to.
609 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800610 protected void drawDotIfNecessary(Canvas canvas) {
611 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800612 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700613 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
614 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800615 final int scrollX = getScrollX();
616 final int scrollY = getScrollY();
617 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800618 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800619 canvas.translate(-scrollX, -scrollY);
620 }
621 }
622
Jon Miranda9b78e192019-08-30 18:42:01 -0700623 @Override
624 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800625 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800626 return;
627 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800628 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800629
Tony Wickhamf34bee82018-12-03 18:11:39 -0800630 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800631 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800632 } else if (hasDot()) {
633 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800634 }
635 }
636
Brandon Dayauon7e376162023-07-10 16:17:26 -0700637 @VisibleForTesting
638 public boolean getForceHideDot() {
639 return mForceHideDot;
640 }
641
Tony Wickhamf34bee82018-12-03 18:11:39 -0800642 private boolean hasDot() {
643 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800644 }
645
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700646 /**
647 * Get the icon bounds on the view depending on the layout type.
648 */
Tony Wickham1237df02017-02-24 08:59:36 -0800649 public void getIconBounds(Rect outBounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700650 getIconBounds(mIconSize, outBounds);
Tony05d98c22018-07-23 08:01:15 -0700651 }
652
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700653 /**
654 * Get the icon bounds on the view depending on the layout type.
655 */
656 public void getIconBounds(int iconSize, Rect outBounds) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700657 outBounds.set(0, 0, iconSize, iconSize);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700658 if (mLayoutHorizontal) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700659 int top = (getHeight() - iconSize) / 2;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700660 if (mIsRtl) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700661 outBounds.offsetTo(getWidth() - iconSize - getPaddingRight(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700662 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700663 outBounds.offsetTo(getPaddingLeft(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700664 }
665 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700666 outBounds.offset((getWidth() - iconSize) / 2, getPaddingTop());
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669
Jon Miranda228877d2021-02-09 11:05:00 -0500670 /**
671 * Sets whether to vertically center the content.
672 */
673 public void setCenterVertically(boolean centerVertically) {
674 mCenterVertically = centerVertically;
675 }
676
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677 @Override
Winson1f064272016-07-18 17:18:02 -0700678 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
679 if (mCenterVertically) {
680 Paint.FontMetrics fm = getPaint().getFontMetrics();
681 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
682 (int) Math.ceil(fm.bottom - fm.top);
683 int height = MeasureSpec.getSize(heightMeasureSpec);
684 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
685 getPaddingBottom());
686 }
Brandon Dayauon7d673312023-04-03 12:32:24 -0700687 // Only apply two line for all_apps and device search only if necessary.
688 if (shouldUseTwoLine() && (mLastOriginalText != null)) {
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800689 CharSequence modifiedString = modifyTitleToSupportMultiLine(
690 MeasureSpec.getSize(widthMeasureSpec) - getCompoundPaddingLeft()
691 - getCompoundPaddingRight(),
692 mLastOriginalText,
693 getPaint(), mBreakPointsIntArray);
694 if (!TextUtils.equals(modifiedString, mLastModifiedText)) {
695 mLastModifiedText = modifiedString;
696 setText(modifiedString);
697 // if text contains NEW_LINE, set max lines to 2
698 if (TextUtils.indexOf(modifiedString, NEW_LINE) != -1) {
699 setSingleLine(false);
700 setMaxLines(2);
701 } else {
702 setSingleLine(true);
703 setMaxLines(1);
704 }
705 }
706 }
Winson1f064272016-07-18 17:18:02 -0700707 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
708 }
709
710 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700711 public void setTextColor(int color) {
712 mTextColor = color;
Jon Spivack68862142021-10-28 18:37:19 -0700713 mTextColorStateList = null;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700714 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700715 }
716
Adam Cohen96bb7982014-07-07 11:58:56 -0700717 @Override
718 public void setTextColor(ColorStateList colors) {
719 mTextColor = colors.getDefaultColor();
Jon Spivack68862142021-10-28 18:37:19 -0700720 mTextColorStateList = colors;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700721 if (Float.compare(mTextAlpha, 1) == 0) {
722 super.setTextColor(colors);
723 } else {
724 super.setTextColor(getModifiedColor());
725 }
Adam Cohen477828c2013-09-20 12:05:49 -0700726 }
727
Tony7308cde2017-06-27 22:38:33 -0700728 public boolean shouldTextBeVisible() {
729 // Text should be visible everywhere but the hotseat.
730 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
731 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700732 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
733 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700734 }
735
Winson Chung5f8afe62013-08-12 16:19:28 -0700736 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700737 setTextAlpha(visible ? 1 : 0);
738 }
739
740 private void setTextAlpha(float alpha) {
741 mTextAlpha = alpha;
Jon Spivack68862142021-10-28 18:37:19 -0700742 if (mTextColorStateList != null) {
743 setTextColor(mTextColorStateList);
744 } else {
745 super.setTextColor(getModifiedColor());
746 }
Sunny Goyaled7a6932018-04-13 11:41:50 -0700747 }
748
749 private int getModifiedColor() {
750 if (mTextAlpha == 0) {
751 // Special case to prevent text shadows in high contrast mode
752 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700753 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800754 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700755 }
756
757 /**
758 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700759 *
Tony8f402802017-06-16 17:24:54 -0700760 * @param fadeIn Whether the text should fade in or fade out.
761 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700762 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700763 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700764 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700765 }
766
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800767 /**
768 * Generate a new string that will support two line text depending on the current string.
769 * This method calculates the limited width of a text view and creates a string to fit as
770 * many words as it can until the limit is reached. Once the limit is reached, we decide to
771 * either return the original title or continue on a new line. How to get the new string is by
772 * iterating through the list of break points and determining if the strings between the break
773 * points can fit within the line it is in.
774 * Example assuming each character takes up one spot:
775 * title = "Battery Stats", breakpoint = [6], stringPtr = 0, limitedWidth = 7
776 * We get the current word -> from sublist(0, breakpoint[i]+1) so sublist (0,7) -> Battery,
777 * now stringPtr = 7 then from sublist(7) the current string is " Stats" and the runningWidth
778 * at this point exceeds limitedWidth and so we put " Stats" onto the next line (after checking
779 * if the first char is a SPACE, we trim to append "Stats". So resulting string would be
780 * "Battery\nStats"
781 */
782 public static CharSequence modifyTitleToSupportMultiLine(int limitedWidth, CharSequence title,
783 TextPaint paint, IntArray breakPoints) {
784 // current title is less than the width allowed so we can just skip
785 if (title == null || paint.measureText(title, 0, title.length()) <= limitedWidth) {
786 return title;
787 }
788 float currentWordWidth, runningWidth = 0;
789 CharSequence currentWord;
790 StringBuilder newString = new StringBuilder();
791 int stringPtr = 0;
792 for (int i = 0; i < breakPoints.size()+1; i++) {
793 if (i < breakPoints.size()) {
794 currentWord = title.subSequence(stringPtr, breakPoints.get(i)+1);
795 } else {
796 // last word from recent breakpoint until the end of the string
797 currentWord = title.subSequence(stringPtr, title.length());
798 }
799 currentWordWidth = paint.measureText(currentWord,0, currentWord.length());
800 runningWidth += currentWordWidth;
801 if (runningWidth <= limitedWidth) {
802 newString.append(currentWord);
803 } else {
804 // there is no more space
805 if (i == 0) {
806 // if the first words exceeds width, just return as the first line will ellipse
807 return title;
808 } else {
809 // If putting word onto a new line, make sure there is no space or new line
810 // character in the beginning of the current word and just put in the rest of
811 // the characters.
812 CharSequence lastCharacters = title.subSequence(stringPtr, title.length());
813 int beginningLetterType =
814 Character.getType(Character.codePointAt(lastCharacters,0));
815 if (beginningLetterType == Character.SPACE_SEPARATOR
816 || beginningLetterType == Character.LINE_SEPARATOR) {
817 lastCharacters = lastCharacters.length() > 1
818 ? lastCharacters.subSequence(1, lastCharacters.length())
819 : EMPTY;
820 }
821 newString.append(NEW_LINE).append(lastCharacters);
822 return newString.toString();
823 }
824 }
825 if (i >= breakPoints.size()) {
826 // no need to look forward into the string if we've already finished processing
827 break;
828 }
829 stringPtr = breakPoints.get(i)+1;
830 }
831 return newString.toString();
832 }
833
Winson Chungaffd7b42010-08-20 15:11:56 -0700834 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800835 public void cancelLongPress() {
836 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800837 mLongPressHelper.cancelLongPress();
838 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500839
Samuel Fufac2820182021-01-22 11:47:25 -0600840 /**
841 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000842 *
843 * If this app is installing, the progress bar will be updated with the installation progress.
844 * If this app is installed and downloading incrementally, the progress bar will be updated
845 * with the total download progress.
846 */
Federico Baron4eb502a2022-12-14 00:04:53 -0800847 public void applyLoadingState(PreloadIconDrawable icon) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800848 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700849 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Federico Baron4eb502a2022-12-14 00:04:53 -0800850 if ((info.runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0
851 || info.hasPromiseIconUi()
852 || (info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0
853 || (ENABLE_DOWNLOAD_APP_UX_V2.get() && icon != null)) {
Federico Barondb599b22023-03-09 14:44:41 -0800854 updateProgressBarUi(info.getProgressLevel() == 100 ? icon : null);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700855 }
856 }
857 }
858
Federico Baron4eb502a2022-12-14 00:04:53 -0800859 private void updateProgressBarUi(PreloadIconDrawable oldIcon) {
860 FastBitmapDrawable originalIcon = mIcon;
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800861 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Federico Baron4eb502a2022-12-14 00:04:53 -0800862 if (preloadDrawable != null && oldIcon != null) {
863 preloadDrawable.maybePerformFinishedAnimation(oldIcon, () -> setIcon(originalIcon));
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000864 }
865 }
866
867 /** Applies the given progress level to the this icon's progress bar. */
868 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800869 public PreloadIconDrawable applyProgressLevel() {
870 if (!(getTag() instanceof ItemInfoWithIcon)) {
871 return null;
872 }
873
874 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
875 int progressLevel = info.getProgressLevel();
876 if (progressLevel >= 100) {
877 setContentDescription(info.contentDescription != null
878 ? info.contentDescription : "");
879 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800880 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800881 } else {
882 setContentDescription(getContext()
883 .getString(R.string.app_waiting_download_title, info.title));
884 }
885 if (mIcon != null) {
886 PreloadIconDrawable preloadIconDrawable;
887 if (mIcon instanceof PreloadIconDrawable) {
888 preloadIconDrawable = (PreloadIconDrawable) mIcon;
889 preloadIconDrawable.setLevel(progressLevel);
fbaron6664d8c2023-04-04 13:02:59 -0700890 preloadIconDrawable.setIsDisabled(ENABLE_DOWNLOAD_APP_UX_V2.get()
891 ? info.getProgressLevel() == 0
892 : !info.isAppStartable());
Mario Bertschler230612f2017-09-27 17:05:21 -0700893 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800894 preloadIconDrawable = makePreloadIcon();
895 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700896 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800897 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400898 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700899 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400900 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700901
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800902 /**
903 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
904 * object.
905 */
906 @Nullable
907 public PreloadIconDrawable makePreloadIcon() {
908 if (!(getTag() instanceof ItemInfoWithIcon)) {
909 return null;
910 }
911
912 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
913 int progressLevel = info.getProgressLevel();
914 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
915
916 preloadDrawable.setLevel(progressLevel);
fbaron6664d8c2023-04-04 13:02:59 -0700917 preloadDrawable.setIsDisabled(ENABLE_DOWNLOAD_APP_UX_V2.get()
918 ? info.getProgressLevel() == 0
919 : !info.isAppStartable());
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800920 return preloadDrawable;
921 }
922
Tony Wickhamf34bee82018-12-03 18:11:39 -0800923 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800924 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800925 boolean wasDotted = mDotInfo != null;
926 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
927 boolean isDotted = mDotInfo != null;
928 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700929 if (mDisplay == DISPLAY_ALL_APPS) {
930 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
931 } else {
932 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
933 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800934 if (wasDotted || isDotted) {
935 // Animate when a dot is first added or when it is removed.
936 if (animate && (wasDotted ^ isDotted) && isShown()) {
937 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800938 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800939 cancelDotScaleAnim();
940 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800941 invalidate();
942 }
943 }
Andy Wickham56be73b2022-01-18 09:13:43 -0800944 if (!TextUtils.isEmpty(itemInfo.contentDescription)) {
vadimtda6dad52019-05-21 13:24:23 -0700945 if (itemInfo.isDisabled()) {
946 setContentDescription(getContext().getString(R.string.disabled_app_label,
947 itemInfo.contentDescription));
948 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800949 int count = mDotInfo.getNotificationCount();
Steven Ng707b1182021-09-13 11:54:58 +0100950 setContentDescription(
951 getAppLabelPluralString(itemInfo.contentDescription.toString(), count));
Tony Wickham305e9202018-01-23 17:46:47 -0800952 } else {
953 setContentDescription(itemInfo.contentDescription);
954 }
955 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800956 }
957 }
958
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800959 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
960 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
961 != 0) {
962 String percentageString = NumberFormat.getPercentInstance()
963 .format(progressLevel * 0.01);
Federico Baron4eb502a2022-12-14 00:04:53 -0800964 if ((info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800965 setContentDescription(getContext()
966 .getString(
967 R.string.app_installing_title, info.title, percentageString));
968 } else if ((info.runtimeStatusFlags
Federico Baron4eb502a2022-12-14 00:04:53 -0800969 & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800970 setContentDescription(getContext()
971 .getString(
972 R.string.app_downloading_title, info.title, percentageString));
973 }
974 }
975 }
976
Winson Chungb745afb2015-03-02 11:51:23 -0800977 /**
978 * Sets the icon for this view based on the layout direction.
979 */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700980 protected void setIcon(FastBitmapDrawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700981 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700982 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700983 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700984 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800985 if (mIcon != null) {
986 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
987 }
Tony17b7f9b2017-05-23 12:19:09 -0700988 }
989
Jon Miranda9b78e192019-08-30 18:42:01 -0700990 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700991 public void setIconVisible(boolean visible) {
992 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700993 if (!mIsIconVisible) {
994 resetIconScale();
995 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700996 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700997 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700998 }
999
Tony Wickham1e29b042023-03-16 19:58:30 +00001000 /** Sets the icon visual state to disabled or not. */
1001 public void setIconDisabled(boolean isDisabled) {
1002 if (mIcon != null) {
1003 mIcon.setIsDisabled(isDisabled);
1004 }
1005 }
1006
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001007 protected boolean iconUpdateAnimationEnabled() {
1008 return mEnableIconUpdateAnimation;
1009 }
1010
Tony Wickham377ed3f2016-07-20 15:21:04 -07001011 protected void applyCompoundDrawables(Drawable icon) {
Jon Mirandafe749972022-03-15 10:02:39 -07001012 if (icon == null) {
1013 // Icon can be null when we use the BubbleTextView for text only.
1014 return;
1015 }
1016
Sunny Goyal66dccad2018-03-19 12:00:51 -07001017 // If we had already set an icon before, disable relayout as the icon size is the
1018 // same as before.
1019 mDisableRelayout = mIcon != null;
1020
1021 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001022
1023 updateIcon(icon);
1024
1025 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -04001026 if (mIcon != null
1027 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001028 && iconUpdateAnimationEnabled()) {
Steven Nge92bc552021-02-10 17:10:15 +00001029 ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon);
Winson Chungb745afb2015-03-02 11:51:23 -08001030 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001031
Sunny Goyal66dccad2018-03-19 12:00:51 -07001032 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -08001033 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001034
Sunny Goyal69b75642015-05-15 17:00:24 -07001035 @Override
1036 public void requestLayout() {
1037 if (!mDisableRelayout) {
1038 super.requestLayout();
1039 }
1040 }
1041
Sunny Goyal34b65272015-03-11 16:56:52 -07001042 /**
1043 * Applies the item info if it is same as what the view is pointing to currently.
1044 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001045 @Override
1046 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001047 if (getTag() == info) {
1048 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -07001049 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001050 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -07001051
Jon Miranda7f522a22017-07-28 11:56:47 -07001052 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -07001053 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -07001054
Sunny Goyal34b65272015-03-11 16:56:52 -07001055 if (info instanceof AppInfo) {
1056 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -07001057 } else if (info instanceof WorkspaceItemInfo) {
1058 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001059 mActivity.invalidateParent(info);
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001060 } else if (info != null) {
1061 applyFromItemInfoWithIcon(info);
Sunny Goyal34b65272015-03-11 16:56:52 -07001062 }
Winsonc0880492015-08-21 11:16:27 -07001063
Sunny Goyal69b75642015-05-15 17:00:24 -07001064 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001065 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -07001066 }
1067 }
1068
1069 /**
1070 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
1071 */
1072 public void verifyHighRes() {
1073 if (mIconLoadRequest != null) {
1074 mIconLoadRequest.cancel();
1075 mIconLoadRequest = null;
1076 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001077 if (getTag() instanceof ItemInfoWithIcon) {
1078 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -07001079 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001080 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -07001081 .updateIconInBackground(BubbleTextView.this, info);
1082 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001083 }
1084 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -07001085
Jon Miranda47170112017-03-03 14:57:14 -08001086 public int getIconSize() {
1087 return mIconSize;
1088 }
Adam Cohen65086992020-02-19 08:40:49 -08001089
Brandon Dayauond3755f42022-08-29 11:48:27 -07001090 public boolean isDisplaySearchResult() {
1091 return mDisplay == DISPLAY_SEARCH_RESULT ||
1092 mDisplay == DISPLAY_SEARCH_RESULT_SMALL;
1093 }
1094
Sunny Goyal82dfc152023-02-24 16:50:09 -08001095 @Override
1096 public MultiTranslateDelegate getTranslateDelegate() {
1097 return mTranslateDelegate;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001098 }
1099
1100 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001101 public void setReorderBounceScale(float scale) {
1102 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -07001103 super.setScaleX(scale);
1104 super.setScaleY(scale);
1105 }
1106
Sunny Goyal82dfc152023-02-24 16:50:09 -08001107 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001108 public float getReorderBounceScale() {
1109 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -07001110 }
1111
Adam Cohen65086992020-02-19 08:40:49 -08001112 @Override
1113 public int getViewType() {
1114 return DRAGGABLE_ICON;
1115 }
1116
1117 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -07001118 public void getWorkspaceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001119 getIconBounds(mIconSize, bounds);
Adam Cohen65086992020-02-19 08:40:49 -08001120 }
1121
Adam Cohenc77bc452020-05-07 11:55:19 -07001122 public void getSourceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001123 getIconBounds(mIconSize, bounds);
Adam Cohenc77bc452020-05-07 11:55:19 -07001124 }
1125
Adam Cohen65086992020-02-19 08:40:49 -08001126 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -07001127 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001128 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -08001129 setForceHideDot(true);
Sunny Goyal8b9919d2021-04-07 14:45:17 -07001130 return () -> { };
Adam Cohen65086992020-02-19 08:40:49 -08001131 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001132
1133 private void resetIconScale() {
Sunny Goyal52f4c162022-04-23 16:02:03 +00001134 if (mIcon != null) {
1135 mIcon.resetScale();
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001136 }
1137 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001138
1139 private void updateIcon(Drawable newIcon) {
1140 if (mLayoutHorizontal) {
1141 setCompoundDrawablesRelative(newIcon, null, null, null);
1142 } else {
1143 setCompoundDrawables(null, newIcon, null, null);
1144 }
1145 }
Steven Ng707b1182021-09-13 11:54:58 +01001146
1147 private String getAppLabelPluralString(String appName, int notificationCount) {
1148 MessageFormat icuCountFormat = new MessageFormat(
1149 getResources().getString(R.string.dotted_app_label),
1150 Locale.getDefault());
1151 HashMap<String, Object> args = new HashMap();
1152 args.put("app_name", appName);
1153 args.put("count", notificationCount);
1154 return icuCountFormat.format(args);
1155 }
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001156
1157 /**
1158 * Starts a long press action and returns the corresponding pre-drag condition
1159 */
1160 public PreDragCondition startLongPressAction() {
1161 PopupContainerWithArrow popup = PopupContainerWithArrow.showForIcon(this);
1162 return popup != null ? popup.createPreDragCondition(true) : null;
1163 }
1164
1165 /**
1166 * Returns true if the view can show long-press popup
1167 */
1168 public boolean canShowLongPressPopup() {
1169 return getTag() instanceof ItemInfo && ShortcutUtil.supportsShortcuts((ItemInfo) getTag());
1170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171}