blob: d7b50a042c8b92c0f18b4eaa9284c56e64ebd736 [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
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -070019import static android.text.Layout.Alignment.ALIGN_NORMAL;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070020import static com.android.launcher3.config.FeatureFlags.ENABLE_ICON_LABEL_AUTO_SCALING;
Pat Manningc49d60b2023-09-13 16:40:27 +010021import static com.android.launcher3.config.FeatureFlags.enableCursorHoverStates;
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;
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -070042import android.text.StaticLayout;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070043import android.text.TextPaint;
Andy Wickham56be73b2022-01-18 09:13:43 -080044import android.text.TextUtils;
Sunny Goyal66dccad2018-03-19 12:00:51 -070045import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080046import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080047import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070048import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070049import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080050import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070051import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080052import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080053import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070054
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000055import androidx.annotation.Nullable;
Hyunyoung Song5809a402021-02-02 01:19:18 -080056import androidx.annotation.UiThread;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080057import androidx.annotation.VisibleForTesting;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070058
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -070059import com.android.launcher3.accessibility.BaseAccessibilityDelegate;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080060import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf34bee82018-12-03 18:11:39 -080061import com.android.launcher3.dot.DotInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070062import com.android.launcher3.dragndrop.DragOptions.PreDragCondition;
Adam Cohen65086992020-02-19 08:40:49 -080063import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070064import com.android.launcher3.folder.FolderIcon;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070065import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080066import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080067import com.android.launcher3.icons.DotRenderer;
Sunny Goyal572aca42021-03-24 15:21:39 -070068import com.android.launcher3.icons.FastBitmapDrawable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080069import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal572aca42021-03-24 15:21:39 -070070import com.android.launcher3.icons.PlaceHolderIconDrawable;
Sunny Goyal79e52fc2021-02-03 10:22:28 -080071import com.android.launcher3.icons.cache.HandlerRunnable;
Sunny Goyale396abf2020-04-06 15:11:17 -070072import com.android.launcher3.model.data.AppInfo;
73import com.android.launcher3.model.data.ItemInfo;
74import com.android.launcher3.model.data.ItemInfoWithIcon;
Sunny Goyale396abf2020-04-06 15:11:17 -070075import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070076import com.android.launcher3.popup.PopupContainerWithArrow;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080077import com.android.launcher3.search.StringMatcherUtility;
78import com.android.launcher3.util.IntArray;
Tony Wickham1e29b042023-03-16 19:58:30 +000079import com.android.launcher3.util.MultiTranslateDelegate;
Samuel Fufa167210a2020-05-12 18:40:11 -070080import com.android.launcher3.util.SafeCloseable;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070081import com.android.launcher3.util.ShortcutUtil;
fbaron1406a0f2023-05-11 08:46:46 -070082import com.android.launcher3.util.Themes;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080083import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070084import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070085
Sunny Goyalc469aad2015-10-01 11:24:23 -070086import java.text.NumberFormat;
Steven Ng707b1182021-09-13 11:54:58 +010087import java.util.HashMap;
88import java.util.Locale;
Sunny Goyalc469aad2015-10-01 11:24:23 -070089
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090/**
91 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
92 * because we want to make the bubble taller than the text and TextView's clip is
93 * too aggressive.
94 */
Sunny Goyalb65d7662021-03-07 15:09:11 -080095public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver,
Hyunyoung Songdb9a53d2021-02-17 00:35:10 -080096 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070097
Sunny Goyaldfaccf62015-05-11 16:30:44 -070098 private static final int DISPLAY_WORKSPACE = 0;
Brandon Dayauonf2ea3992023-06-26 12:13:30 -070099 public static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700100 private static final int DISPLAY_FOLDER = 2;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700101 protected static final int DISPLAY_TASKBAR = 5;
Brandon Dayauon7e376162023-07-10 16:17:26 -0700102 public static final int DISPLAY_SEARCH_RESULT = 6;
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700103 public static final int DISPLAY_SEARCH_RESULT_SMALL = 7;
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700104 public static final int DISPLAY_PREDICTION_ROW = 8;
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700105 public static final int DISPLAY_SEARCH_RESULT_APP_ROW = 9;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700106
Sunny Goyal486adb02021-10-21 11:29:52 -0700107 private static final float MIN_LETTER_SPACING = -0.05f;
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700108 private static final int MAX_SEARCH_LOOP_COUNT = 20;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800109 private static final Character NEW_LINE = '\n';
110 private static final String EMPTY = "";
111 private static final StringMatcherUtility.StringMatcher MATCHER =
112 StringMatcherUtility.StringMatcher.getInstance();
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700113
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700114 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -0700115
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700116 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800117
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800118 private IntArray mBreakPointsIntArray;
119 private CharSequence mLastOriginalText;
120 private CharSequence mLastModifiedText;
121
Tony Wickhamf34bee82018-12-03 18:11:39 -0800122 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
123 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800124 @Override
125 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800126 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800127 }
128
129 @Override
130 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800131 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800132 bubbleTextView.invalidate();
133 }
134 };
135
Sunny Goyaled7a6932018-04-13 11:41:50 -0700136 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
137 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700138 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700139 public Float get(BubbleTextView bubbleTextView) {
140 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700141 }
142
143 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700144 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700145 bubbleTextView.setTextAlpha(alpha);
146 }
147 };
148
Sunny Goyal82dfc152023-02-24 16:50:09 -0800149 private final MultiTranslateDelegate mTranslateDelegate = new MultiTranslateDelegate(this);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800150 private final ActivityContext mActivity;
Sunny Goyalc4c32692021-05-21 14:23:29 -0700151 private FastBitmapDrawable mIcon;
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700152 private DeviceProfile mDeviceProfile;
Adam Cohen65086992020-02-19 08:40:49 -0800153 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700154
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800155 protected int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700156
Sunny Goyaled7a6932018-04-13 11:41:50 -0700157 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700158
Kevin Limb3b467c2023-07-21 02:15:34 +0000159 private boolean mLayoutHorizontal;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700160 private final boolean mIsRtl;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700161 private final int mIconSize;
162
163 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyald872a972021-11-24 18:07:04 -0800164 private boolean mHideBadge = false;
165 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700166 private boolean mIsIconVisible = true;
167 @ViewDebug.ExportedProperty(category = "launcher")
168 private int mTextColor;
169 @ViewDebug.ExportedProperty(category = "launcher")
Jon Spivack68862142021-10-28 18:37:19 -0700170 private ColorStateList mTextColorStateList;
171 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700172 private float mTextAlpha = 1;
173
Tony4043b052018-08-17 13:47:39 -0700174 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800175 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800176 private DotRenderer mDotRenderer;
177 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
Sunny Goyal67555872021-05-21 12:56:55 -0700178 protected DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800179 private Animator mDotScaleAnim;
180 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700181
Sunny Goyal4ffec482016-02-09 11:28:52 -0800182 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800183 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800184 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700185 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800186 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700187 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500188
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800189 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700190
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400191 private boolean mEnableIconUpdateAnimation = false;
192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700194 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 }
196
197 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700198 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 }
200
201 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
202 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800203 mActivity = ActivityContext.lookupContext(context);
Pat Manningc49d60b2023-09-13 16:40:27 +0100204 FastBitmapDrawable.setFlagHoverEnabled(enableCursorHoverStates());
Adam Cohen96bb7982014-07-07 11:58:56 -0700205
Adam Cohen96bb7982014-07-07 11:58:56 -0700206 TypedArray a = context.obtainStyledAttributes(attrs,
207 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800208 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700209 mIsRtl = (getResources().getConfiguration().getLayoutDirection()
210 == View.LAYOUT_DIRECTION_RTL);
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700211 mDeviceProfile = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700212
Samuel Fufac96fa242019-09-26 23:06:32 -0700213 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700214 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700215 if (mDisplay == DISPLAY_WORKSPACE) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700216 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.iconTextSizePx);
217 setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
218 defaultIconSize = mDeviceProfile.iconSizePx;
219 setCenterVertically(mDeviceProfile.iconCenterVertically);
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700220 } else if (mDisplay == DISPLAY_ALL_APPS || mDisplay == DISPLAY_PREDICTION_ROW
221 || mDisplay == DISPLAY_SEARCH_RESULT_APP_ROW) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700222 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.allAppsIconTextSizePx);
223 setCompoundDrawablePadding(mDeviceProfile.allAppsIconDrawablePaddingPx);
224 defaultIconSize = mDeviceProfile.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700225 } else if (mDisplay == DISPLAY_FOLDER) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700226 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.folderChildTextSizePx);
227 setCompoundDrawablePadding(mDeviceProfile.folderChildDrawablePaddingPx);
228 defaultIconSize = mDeviceProfile.folderChildIconSizePx;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700229 } else if (mDisplay == DISPLAY_SEARCH_RESULT) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700230 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.allAppsIconTextSizePx);
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700231 defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size);
232 } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
233 defaultIconSize = getResources().getDimensionPixelSize(
234 R.dimen.search_row_small_icon_size);
Tony Wickham794fe4f2021-01-11 14:54:23 -0600235 } else if (mDisplay == DISPLAY_TASKBAR) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700236 defaultIconSize = mDeviceProfile.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700237 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700238 // widget_selection or shortcut_popup
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700239 defaultIconSize = mDeviceProfile.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700240 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700241
Winson1f064272016-07-18 17:18:02 -0700242 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700243
244 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
245 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700246 a.recycle();
247
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800248 mLongPressHelper = new CheckLongPressHelper(this);
249
Tony Wickham8912b042018-11-29 15:28:53 -0800250 mDotParams = new DotRenderer.DrawParams();
251
Sunny Goyal66dccad2018-03-19 12:00:51 -0700252 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700253 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700254 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 }
256
Sunny Goyal66dccad2018-03-19 12:00:51 -0700257 @Override
258 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
259 // Disable marques when not focused to that, so that updating text does not cause relayout.
260 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
261 super.onFocusChanged(focused, direction, previouslyFocusedRect);
262 }
263
Sunny Goyald872a972021-11-24 18:07:04 -0800264 public void setHideBadge(boolean hideBadge) {
265 mHideBadge = hideBadge;
266 }
267
Jon Miranda52549442017-10-26 11:28:06 -0700268 /**
269 * Resets the view so it can be recycled.
270 */
271 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800272 mDotInfo = null;
James O'Leary1557ba32022-05-04 16:49:03 +0000273 mDotParams.dotColor = Color.TRANSPARENT;
274 mDotParams.appColor = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800275 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800276 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800277 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800278 setBackground(null);
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700279 if (FeatureFlags.enableTwolineAllapps() || FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()) {
hyunyoungs825480f2023-03-24 10:11:15 -0700280 setMaxLines(1);
281 }
Sunny Goyal52f4c162022-04-23 16:02:03 +0000282
283 setTag(null);
284 if (mIconLoadRequest != null) {
285 mIconLoadRequest.cancel();
286 mIconLoadRequest = null;
287 }
Jon Miranda52549442017-10-26 11:28:06 -0700288 }
289
Tony Wickhamf34bee82018-12-03 18:11:39 -0800290 private void cancelDotScaleAnim() {
291 if (mDotScaleAnim != null) {
292 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700293 }
294 }
295
Tony Wickhamf34bee82018-12-03 18:11:39 -0800296 private void animateDotScale(float... dotScales) {
297 cancelDotScaleAnim();
298 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
299 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700300 @Override
301 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800302 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700303 }
304 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800305 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700306 }
307
Hyunyoung Song5809a402021-02-02 01:19:18 -0800308 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700309 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
Tony Wickham175d1522021-08-25 09:18:41 -0700310 applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0);
311 }
312
313 @UiThread
314 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) {
Federico Baron4eb502a2022-12-14 00:04:53 -0800315 applyFromWorkspaceItem(info, null);
Winson Chung5f8afe62013-08-12 16:19:28 -0700316 }
317
Tony Wickham175d1522021-08-25 09:18:41 -0700318 /**
319 * Returns whether the newInfo differs from the current getTag().
320 */
321 public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) {
322 WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo
323 ? (WorkspaceItemInfo) getTag()
324 : null;
325 boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null
326 && newInfo.getTargetComponent() != null
327 && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent());
328 return changedIcons && isShown();
329 }
330
Adam Cohen29794c52019-06-21 12:50:30 -0700331 @Override
332 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -0700333 if (delegate instanceof BaseAccessibilityDelegate) {
Adam Cohen29794c52019-06-21 12:50:30 -0700334 super.setAccessibilityDelegate(delegate);
335 } else {
336 // NO-OP
337 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
338 // delegate incorrectly. There are no cases when we shouldn't be using the
339 // LauncherAccessibilityDelegate for BubbleTextView.
340 }
341 }
342
Hyunyoung Song5809a402021-02-02 01:19:18 -0800343 @UiThread
Federico Baron4eb502a2022-12-14 00:04:53 -0800344 public void applyFromWorkspaceItem(WorkspaceItemInfo info, PreloadIconDrawable icon) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800345 applyIconAndLabel(info);
sfufa@google.comca76de02021-09-24 08:55:48 -0700346 setItemInfo(info);
Federico Baron4eb502a2022-12-14 00:04:53 -0800347 applyLoadingState(icon);
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());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800350 }
351
Hyunyoung Song5809a402021-02-02 01:19:18 -0800352 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700353 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800354 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700355
Sunny Goyal95899162019-03-27 16:03:06 -0700356 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700357 setItemInfo(info);
358
Sunny Goyal34b65272015-03-11 16:56:52 -0700359
360 // Verify high res immediately
361 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800362
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000363 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800364 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700365 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800366 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800367 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal508da152014-08-14 10:53:27 -0700368 }
369
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700370 /**
371 * Apply label and tag using a generic {@link ItemInfoWithIcon}
372 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800373 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700374 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800375 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700376 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700377 setItemInfo(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700378
379 // Verify high res immediately
380 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800381
382 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700383 }
384
Sunny Goyalf0ff70a2022-05-11 07:23:10 -0700385 protected void setItemInfo(ItemInfoWithIcon itemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700386 setTag(itemInfo);
sfufa@google.com0fe464d2021-10-14 14:34:50 -0700387 }
388
Brandon Dayauon0ae52c92023-09-06 17:27:18 -0700389 @VisibleForTesting
Hyunyoung Song5809a402021-02-02 01:19:18 -0800390 @UiThread
Brandon Dayauon0ae52c92023-09-06 17:27:18 -0700391 public void applyIconAndLabel(ItemInfoWithIcon info) {
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800392 int flags = shouldUseTheme() ? FLAG_THEMED : 0;
Brandon Dayauon0ae52c92023-09-06 17:27:18 -0700393 // Remove badge on icons smaller than 48dp.
394 if (mHideBadge || mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
Sunny Goyald872a972021-11-24 18:07:04 -0800395 flags |= FLAG_NO_BADGE;
396 }
397 FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags);
James O'Leary1557ba32022-05-04 16:49:03 +0000398 mDotParams.appColor = iconDrawable.getIconColor();
fbaron1406a0f2023-05-11 08:46:46 -0700399 mDotParams.dotColor = Themes.getAttrColor(getContext(), R.attr.notificationDotColor);
Sunny Goyalf4204382016-07-13 10:46:07 -0700400 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000401 applyLabel(info);
402 }
403
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800404 protected boolean shouldUseTheme() {
Brandon Dayauon08cdbc52023-09-14 19:05:09 +0000405 return (mDisplay == DISPLAY_WORKSPACE || mDisplay == DISPLAY_FOLDER
406 || mDisplay == DISPLAY_TASKBAR) && Themes.isThemedIconEnabled(getContext());
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800407 }
408
Brandon Dayauon7d673312023-04-03 12:32:24 -0700409 /**
410 * Only if actual text can be displayed in two line, the {@code true} value will be effective.
411 */
412 protected boolean shouldUseTwoLine() {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700413 return ((FeatureFlags.enableTwolineAllapps())
414 && (mDisplay == DISPLAY_ALL_APPS || mDisplay == DISPLAY_PREDICTION_ROW))
Brandon Dayauon7d673312023-04-03 12:32:24 -0700415 || (FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()
416 && mDisplay == DISPLAY_SEARCH_RESULT);
417 }
418
Hyunyoung Song5809a402021-02-02 01:19:18 -0800419 @UiThread
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800420 @VisibleForTesting
421 public void applyLabel(ItemInfoWithIcon info) {
422 CharSequence label = info.title;
423 if (label != null) {
424 mLastOriginalText = label;
425 mLastModifiedText = mLastOriginalText;
426 mBreakPointsIntArray = StringMatcherUtility.getListOfBreakpoints(label, MATCHER);
427 setText(label);
428 }
Sunny Goyalf4204382016-07-13 10:46:07 -0700429 if (info.contentDescription != null) {
430 setContentDescription(info.isDisabled()
431 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
432 : info.contentDescription);
433 }
434 }
435
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700436 /** This is used for testing to forcefully set the display. */
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800437 @VisibleForTesting
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700438 public void setDisplay(int display) {
439 mDisplay = display;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800440 }
441
Winson Chung7501adf2015-06-02 11:24:28 -0700442 /**
443 * Overrides the default long press timeout.
444 */
Tony2ca999c2018-09-24 17:24:51 -0400445 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
446 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700447 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800450 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700451 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800452 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800453 }
Sunny Goyal508da152014-08-14 10:53:27 -0700454 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800455
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800456 @Override
457 protected int[] onCreateDrawableState(int extraSpace) {
458 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
459 if (mStayPressed) {
460 mergeDrawableStates(drawableState, STATE_PRESSED);
461 }
462 return drawableState;
463 }
464
Winson Chungb745afb2015-03-02 11:51:23 -0800465 /** Returns the icon for this view. */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700466 public FastBitmapDrawable getIcon() {
Winson Chungb745afb2015-03-02 11:51:23 -0800467 return mIcon;
468 }
469
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700470 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800471 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700472 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700473 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700474 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700475 return false;
476 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700477 if (isLongClickable()) {
478 super.onTouchEvent(event);
479 mLongPressHelper.onTouchEvent(event);
480 // Keep receiving the rest of the events
481 return true;
482 } else {
483 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700484 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800485 }
486
Sunny Goyal44a3b202020-07-09 08:27:06 -0700487 /**
488 * Returns true if the touch down at the provided position be ignored
489 */
490 protected boolean shouldIgnoreTouchDown(float x, float y) {
Tony Wickham42b03952021-08-31 10:41:36 -0700491 if (mDisplay == DISPLAY_TASKBAR) {
492 // Allow touching within padding on taskbar, given icon sizes are smaller.
493 return false;
494 }
Sunny Goyal44a3b202020-07-09 08:27:06 -0700495 return y < getPaddingTop()
496 || x < getPaddingLeft()
497 || y > getHeight() - getPaddingBottom()
498 || x > getWidth() - getPaddingRight();
499 }
500
Michael Jurkaddd62e92011-02-16 17:49:14 -0800501 void setStayPressed(boolean stayPressed) {
502 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800503 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800504 }
505
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700506 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800507 public void onVisibilityAggregated(boolean isVisible) {
508 super.onVisibilityAggregated(isVisible);
509 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800510 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800511 }
512 }
513
Tony Wickham8ac277e2021-05-24 15:47:38 -0700514 public void clearPressedBackground() {
Sunny Goyal508da152014-08-14 10:53:27 -0700515 setPressed(false);
516 setStayPressed(false);
517 }
518
519 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700520 public boolean onKeyUp(int keyCode, KeyEvent event) {
521 // Unlike touch events, keypress event propagate pressed state change immediately,
522 // without waiting for onClickHandler to execute. Disable pressed state changes here
523 // to avoid flickering.
524 mIgnorePressedStateChange = true;
525 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700526 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800527 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700528 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800529 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800530
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700531 @Override
532 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
533 super.onSizeChanged(w, h, oldw, oldh);
534 checkForEllipsis();
535 }
536
537 @Override
538 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
539 super.onTextChanged(text, start, lengthBefore, lengthAfter);
540 checkForEllipsis();
541 }
542
543 private void checkForEllipsis() {
544 if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) {
545 return;
546 }
547 float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
548 if (width <= 0) {
549 return;
550 }
551 setLetterSpacing(0);
552
553 String text = getText().toString();
554 TextPaint paint = getPaint();
555 if (paint.measureText(text) < width) {
556 return;
557 }
558
559 float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING);
560 // Reset the paint value so that the call to TextView does appropriate diff.
561 paint.setLetterSpacing(0);
562 setLetterSpacing(spacing);
563 }
564
565 /**
566 * Find the appropriate text spacing to display the provided text
567 * @param paint the paint used by the text view
568 * @param text the text to display
569 * @param allowedWidthPx available space to render the text
570 * @param minSpacingEm minimum spacing allowed between characters
571 * @return the final textSpacing value
572 *
573 * @see #setLetterSpacing(float)
574 */
575 private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx,
576 float minSpacingEm) {
577 paint.setLetterSpacing(minSpacingEm);
578 if (paint.measureText(text) > allowedWidthPx) {
579 // If there is no result at high limit, we can do anything more
580 return minSpacingEm;
581 }
582
583 float lowLimit = 0;
584 float highLimit = minSpacingEm;
585
586 for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) {
587 float value = (lowLimit + highLimit) / 2;
588 paint.setLetterSpacing(value);
589 if (paint.measureText(text) < allowedWidthPx) {
590 highLimit = value;
591 } else {
592 lowLimit = value;
593 }
594 }
595
596 // At the end error on the higher side
597 return highLimit;
598 }
599
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700600 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800601 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700602 super.onDraw(canvas);
603 }
604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700606 public void onDraw(Canvas canvas) {
607 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800608 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700609 }
610
Tony Wickham1237df02017-02-24 08:59:36 -0800611 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800612 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700613 *
Tony Wickham1237df02017-02-24 08:59:36 -0800614 * @param canvas The canvas to draw to.
615 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800616 protected void drawDotIfNecessary(Canvas canvas) {
617 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800618 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700619 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
620 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800621 final int scrollX = getScrollX();
622 final int scrollY = getScrollY();
623 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800624 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800625 canvas.translate(-scrollX, -scrollY);
626 }
627 }
628
Jon Miranda9b78e192019-08-30 18:42:01 -0700629 @Override
630 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800631 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800632 return;
633 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800634 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800635
Tony Wickhamf34bee82018-12-03 18:11:39 -0800636 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800637 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800638 } else if (hasDot()) {
639 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800640 }
641 }
642
Brandon Dayauon7e376162023-07-10 16:17:26 -0700643 @VisibleForTesting
644 public boolean getForceHideDot() {
645 return mForceHideDot;
646 }
647
Tony Wickhamf34bee82018-12-03 18:11:39 -0800648 private boolean hasDot() {
649 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800650 }
651
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700652 /**
653 * Get the icon bounds on the view depending on the layout type.
654 */
Tony Wickham1237df02017-02-24 08:59:36 -0800655 public void getIconBounds(Rect outBounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700656 getIconBounds(mIconSize, outBounds);
Tony05d98c22018-07-23 08:01:15 -0700657 }
658
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700659 /**
660 * Get the icon bounds on the view depending on the layout type.
661 */
662 public void getIconBounds(int iconSize, Rect outBounds) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700663 outBounds.set(0, 0, iconSize, iconSize);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700664 if (mLayoutHorizontal) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700665 int top = (getHeight() - iconSize) / 2;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700666 if (mIsRtl) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700667 outBounds.offsetTo(getWidth() - iconSize - getPaddingRight(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700668 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700669 outBounds.offsetTo(getPaddingLeft(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700670 }
671 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700672 outBounds.offset((getWidth() - iconSize) / 2, getPaddingTop());
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400675
Jon Miranda228877d2021-02-09 11:05:00 -0500676 /**
Kevin Limb3b467c2023-07-21 02:15:34 +0000677 * Sets whether the layout is horizontal.
678 */
679 public void setLayoutHorizontal(boolean layoutHorizontal) {
680 if (mLayoutHorizontal == layoutHorizontal) {
681 return;
682 }
683
684 mLayoutHorizontal = layoutHorizontal;
685 applyCompoundDrawables(getIconOrTransparentColor());
686 }
687
688 /**
Jon Miranda228877d2021-02-09 11:05:00 -0500689 * Sets whether to vertically center the content.
690 */
691 public void setCenterVertically(boolean centerVertically) {
692 mCenterVertically = centerVertically;
693 }
694
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695 @Override
Winson1f064272016-07-18 17:18:02 -0700696 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700697 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson1f064272016-07-18 17:18:02 -0700698 if (mCenterVertically) {
699 Paint.FontMetrics fm = getPaint().getFontMetrics();
700 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
701 (int) Math.ceil(fm.bottom - fm.top);
Winson1f064272016-07-18 17:18:02 -0700702 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
703 getPaddingBottom());
704 }
Brandon Dayauon7d673312023-04-03 12:32:24 -0700705 // Only apply two line for all_apps and device search only if necessary.
706 if (shouldUseTwoLine() && (mLastOriginalText != null)) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700707 int allowedVerticalSpace = height - getPaddingTop() - getPaddingBottom()
708 - mDeviceProfile.allAppsIconSizePx
709 - mDeviceProfile.allAppsIconDrawablePaddingPx;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800710 CharSequence modifiedString = modifyTitleToSupportMultiLine(
711 MeasureSpec.getSize(widthMeasureSpec) - getCompoundPaddingLeft()
712 - getCompoundPaddingRight(),
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700713 allowedVerticalSpace,
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800714 mLastOriginalText,
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700715 getPaint(),
716 mBreakPointsIntArray,
717 getLineSpacingMultiplier(),
718 getLineSpacingExtra());
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800719 if (!TextUtils.equals(modifiedString, mLastModifiedText)) {
720 mLastModifiedText = modifiedString;
721 setText(modifiedString);
722 // if text contains NEW_LINE, set max lines to 2
723 if (TextUtils.indexOf(modifiedString, NEW_LINE) != -1) {
724 setSingleLine(false);
725 setMaxLines(2);
726 } else {
727 setSingleLine(true);
728 setMaxLines(1);
729 }
730 }
731 }
Winson1f064272016-07-18 17:18:02 -0700732 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
733 }
734
735 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700736 public void setTextColor(int color) {
737 mTextColor = color;
Jon Spivack68862142021-10-28 18:37:19 -0700738 mTextColorStateList = null;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700739 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700740 }
741
Adam Cohen96bb7982014-07-07 11:58:56 -0700742 @Override
743 public void setTextColor(ColorStateList colors) {
744 mTextColor = colors.getDefaultColor();
Jon Spivack68862142021-10-28 18:37:19 -0700745 mTextColorStateList = colors;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700746 if (Float.compare(mTextAlpha, 1) == 0) {
747 super.setTextColor(colors);
748 } else {
749 super.setTextColor(getModifiedColor());
750 }
Adam Cohen477828c2013-09-20 12:05:49 -0700751 }
752
Tony7308cde2017-06-27 22:38:33 -0700753 public boolean shouldTextBeVisible() {
754 // Text should be visible everywhere but the hotseat.
755 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
756 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700757 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
758 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700759 }
760
Winson Chung5f8afe62013-08-12 16:19:28 -0700761 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700762 setTextAlpha(visible ? 1 : 0);
763 }
764
765 private void setTextAlpha(float alpha) {
766 mTextAlpha = alpha;
Jon Spivack68862142021-10-28 18:37:19 -0700767 if (mTextColorStateList != null) {
768 setTextColor(mTextColorStateList);
769 } else {
770 super.setTextColor(getModifiedColor());
771 }
Sunny Goyaled7a6932018-04-13 11:41:50 -0700772 }
773
774 private int getModifiedColor() {
775 if (mTextAlpha == 0) {
776 // Special case to prevent text shadows in high contrast mode
777 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700778 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800779 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700780 }
781
782 /**
783 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700784 *
Tony8f402802017-06-16 17:24:54 -0700785 * @param fadeIn Whether the text should fade in or fade out.
786 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700787 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700788 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700789 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700790 }
791
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800792 /**
793 * Generate a new string that will support two line text depending on the current string.
794 * This method calculates the limited width of a text view and creates a string to fit as
795 * many words as it can until the limit is reached. Once the limit is reached, we decide to
796 * either return the original title or continue on a new line. How to get the new string is by
797 * iterating through the list of break points and determining if the strings between the break
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700798 * points can fit within the line it is in. We will show the modified string if there is enough
799 * horizontal and vertical space, otherwise this method will just return the original string.
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800800 * Example assuming each character takes up one spot:
801 * title = "Battery Stats", breakpoint = [6], stringPtr = 0, limitedWidth = 7
802 * We get the current word -> from sublist(0, breakpoint[i]+1) so sublist (0,7) -> Battery,
803 * now stringPtr = 7 then from sublist(7) the current string is " Stats" and the runningWidth
804 * at this point exceeds limitedWidth and so we put " Stats" onto the next line (after checking
805 * if the first char is a SPACE, we trim to append "Stats". So resulting string would be
806 * "Battery\nStats"
807 */
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700808 public static CharSequence modifyTitleToSupportMultiLine(int limitedWidth, int limitedHeight,
809 CharSequence title, TextPaint paint, IntArray breakPoints, float spacingMultiplier,
810 float spacingExtra) {
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800811 // current title is less than the width allowed so we can just skip
812 if (title == null || paint.measureText(title, 0, title.length()) <= limitedWidth) {
813 return title;
814 }
815 float currentWordWidth, runningWidth = 0;
816 CharSequence currentWord;
817 StringBuilder newString = new StringBuilder();
Brandon Dayauon6c345f42023-10-03 16:38:42 -0700818 // TODO: Remove when ENABLE_ICON_LABEL_AUTO_SCALING feature flag is being cleaned up.
819 paint.setLetterSpacing(MIN_LETTER_SPACING);
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800820 int stringPtr = 0;
821 for (int i = 0; i < breakPoints.size()+1; i++) {
822 if (i < breakPoints.size()) {
823 currentWord = title.subSequence(stringPtr, breakPoints.get(i)+1);
824 } else {
825 // last word from recent breakpoint until the end of the string
826 currentWord = title.subSequence(stringPtr, title.length());
827 }
828 currentWordWidth = paint.measureText(currentWord,0, currentWord.length());
829 runningWidth += currentWordWidth;
830 if (runningWidth <= limitedWidth) {
831 newString.append(currentWord);
832 } else {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700833 if (i != 0) {
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800834 // If putting word onto a new line, make sure there is no space or new line
835 // character in the beginning of the current word and just put in the rest of
836 // the characters.
837 CharSequence lastCharacters = title.subSequence(stringPtr, title.length());
838 int beginningLetterType =
839 Character.getType(Character.codePointAt(lastCharacters,0));
840 if (beginningLetterType == Character.SPACE_SEPARATOR
841 || beginningLetterType == Character.LINE_SEPARATOR) {
842 lastCharacters = lastCharacters.length() > 1
843 ? lastCharacters.subSequence(1, lastCharacters.length())
844 : EMPTY;
845 }
846 newString.append(NEW_LINE).append(lastCharacters);
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700847 StaticLayout staticLayout = new StaticLayout(newString, paint, limitedWidth,
848 ALIGN_NORMAL, spacingMultiplier, spacingExtra, false);
849 if (staticLayout.getHeight() < limitedHeight) {
850 return newString.toString();
851 }
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800852 }
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700853 // if the first words exceeds width, just return as the first line will ellipse
854 return title;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800855 }
856 if (i >= breakPoints.size()) {
857 // no need to look forward into the string if we've already finished processing
858 break;
859 }
860 stringPtr = breakPoints.get(i)+1;
861 }
862 return newString.toString();
863 }
864
Winson Chungaffd7b42010-08-20 15:11:56 -0700865 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800866 public void cancelLongPress() {
867 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800868 mLongPressHelper.cancelLongPress();
869 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500870
Samuel Fufac2820182021-01-22 11:47:25 -0600871 /**
872 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000873 *
874 * If this app is installing, the progress bar will be updated with the installation progress.
875 * If this app is installed and downloading incrementally, the progress bar will be updated
876 * with the total download progress.
877 */
Federico Baron4eb502a2022-12-14 00:04:53 -0800878 public void applyLoadingState(PreloadIconDrawable icon) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800879 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700880 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Federico Baron4eb502a2022-12-14 00:04:53 -0800881 if ((info.runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0
882 || info.hasPromiseIconUi()
883 || (info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0
fbaron461a9aa2023-09-27 16:13:27 -0700884 || (icon != null)) {
Federico Barondb599b22023-03-09 14:44:41 -0800885 updateProgressBarUi(info.getProgressLevel() == 100 ? icon : null);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700886 }
887 }
888 }
889
Federico Baron4eb502a2022-12-14 00:04:53 -0800890 private void updateProgressBarUi(PreloadIconDrawable oldIcon) {
891 FastBitmapDrawable originalIcon = mIcon;
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800892 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Federico Baron4eb502a2022-12-14 00:04:53 -0800893 if (preloadDrawable != null && oldIcon != null) {
894 preloadDrawable.maybePerformFinishedAnimation(oldIcon, () -> setIcon(originalIcon));
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000895 }
896 }
897
898 /** Applies the given progress level to the this icon's progress bar. */
899 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800900 public PreloadIconDrawable applyProgressLevel() {
901 if (!(getTag() instanceof ItemInfoWithIcon)) {
902 return null;
903 }
904
905 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
906 int progressLevel = info.getProgressLevel();
907 if (progressLevel >= 100) {
908 setContentDescription(info.contentDescription != null
909 ? info.contentDescription : "");
910 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800911 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800912 } else {
913 setContentDescription(getContext()
914 .getString(R.string.app_waiting_download_title, info.title));
915 }
916 if (mIcon != null) {
917 PreloadIconDrawable preloadIconDrawable;
918 if (mIcon instanceof PreloadIconDrawable) {
919 preloadIconDrawable = (PreloadIconDrawable) mIcon;
920 preloadIconDrawable.setLevel(progressLevel);
fbaron461a9aa2023-09-27 16:13:27 -0700921 preloadIconDrawable.setIsDisabled(info.getProgressLevel() == 0);
Mario Bertschler230612f2017-09-27 17:05:21 -0700922 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800923 preloadIconDrawable = makePreloadIcon();
924 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700925 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800926 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400927 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700928 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400929 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700930
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800931 /**
932 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
933 * object.
934 */
935 @Nullable
936 public PreloadIconDrawable makePreloadIcon() {
937 if (!(getTag() instanceof ItemInfoWithIcon)) {
938 return null;
939 }
940
941 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
942 int progressLevel = info.getProgressLevel();
943 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
944
945 preloadDrawable.setLevel(progressLevel);
fbaron461a9aa2023-09-27 16:13:27 -0700946 preloadDrawable.setIsDisabled(info.getProgressLevel() == 0);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800947 return preloadDrawable;
948 }
949
Tony Wickhamf34bee82018-12-03 18:11:39 -0800950 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800951 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800952 boolean wasDotted = mDotInfo != null;
953 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
954 boolean isDotted = mDotInfo != null;
955 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700956 if (mDisplay == DISPLAY_ALL_APPS) {
957 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
958 } else {
959 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
960 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800961 if (wasDotted || isDotted) {
962 // Animate when a dot is first added or when it is removed.
963 if (animate && (wasDotted ^ isDotted) && isShown()) {
964 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800965 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800966 cancelDotScaleAnim();
967 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800968 invalidate();
969 }
970 }
Andy Wickham56be73b2022-01-18 09:13:43 -0800971 if (!TextUtils.isEmpty(itemInfo.contentDescription)) {
vadimtda6dad52019-05-21 13:24:23 -0700972 if (itemInfo.isDisabled()) {
973 setContentDescription(getContext().getString(R.string.disabled_app_label,
974 itemInfo.contentDescription));
975 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800976 int count = mDotInfo.getNotificationCount();
Steven Ng707b1182021-09-13 11:54:58 +0100977 setContentDescription(
978 getAppLabelPluralString(itemInfo.contentDescription.toString(), count));
Tony Wickham305e9202018-01-23 17:46:47 -0800979 } else {
980 setContentDescription(itemInfo.contentDescription);
981 }
982 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800983 }
984 }
985
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800986 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
987 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
988 != 0) {
989 String percentageString = NumberFormat.getPercentInstance()
990 .format(progressLevel * 0.01);
Federico Baron4eb502a2022-12-14 00:04:53 -0800991 if ((info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800992 setContentDescription(getContext()
993 .getString(
994 R.string.app_installing_title, info.title, percentageString));
995 } else if ((info.runtimeStatusFlags
Federico Baron4eb502a2022-12-14 00:04:53 -0800996 & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800997 setContentDescription(getContext()
998 .getString(
999 R.string.app_downloading_title, info.title, percentageString));
1000 }
1001 }
1002 }
1003
Winson Chungb745afb2015-03-02 11:51:23 -08001004 /**
1005 * Sets the icon for this view based on the layout direction.
1006 */
Sunny Goyalc4c32692021-05-21 14:23:29 -07001007 protected void setIcon(FastBitmapDrawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -07001008 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -07001009 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -07001010 }
Sunny Goyal66dccad2018-03-19 12:00:51 -07001011 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -08001012 if (mIcon != null) {
1013 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
1014 }
Tony17b7f9b2017-05-23 12:19:09 -07001015 }
1016
Jon Miranda9b78e192019-08-30 18:42:01 -07001017 @Override
Tony17b7f9b2017-05-23 12:19:09 -07001018 public void setIconVisible(boolean visible) {
1019 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001020 if (!mIsIconVisible) {
1021 resetIconScale();
1022 }
Kevin Limb3b467c2023-07-21 02:15:34 +00001023 Drawable icon = getIconOrTransparentColor();
Tony17b7f9b2017-05-23 12:19:09 -07001024 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -07001025 }
1026
Kevin Limb3b467c2023-07-21 02:15:34 +00001027 private Drawable getIconOrTransparentColor() {
1028 return mIsIconVisible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
1029 }
1030
Tony Wickham1e29b042023-03-16 19:58:30 +00001031 /** Sets the icon visual state to disabled or not. */
1032 public void setIconDisabled(boolean isDisabled) {
1033 if (mIcon != null) {
1034 mIcon.setIsDisabled(isDisabled);
1035 }
1036 }
1037
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001038 protected boolean iconUpdateAnimationEnabled() {
1039 return mEnableIconUpdateAnimation;
1040 }
1041
Tony Wickham377ed3f2016-07-20 15:21:04 -07001042 protected void applyCompoundDrawables(Drawable icon) {
Jon Mirandafe749972022-03-15 10:02:39 -07001043 if (icon == null) {
1044 // Icon can be null when we use the BubbleTextView for text only.
1045 return;
1046 }
1047
Sunny Goyal66dccad2018-03-19 12:00:51 -07001048 // If we had already set an icon before, disable relayout as the icon size is the
1049 // same as before.
1050 mDisableRelayout = mIcon != null;
1051
1052 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001053
1054 updateIcon(icon);
1055
1056 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -04001057 if (mIcon != null
1058 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001059 && iconUpdateAnimationEnabled()) {
Steven Nge92bc552021-02-10 17:10:15 +00001060 ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon);
Winson Chungb745afb2015-03-02 11:51:23 -08001061 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001062
Sunny Goyal66dccad2018-03-19 12:00:51 -07001063 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -08001064 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001065
Sunny Goyal69b75642015-05-15 17:00:24 -07001066 @Override
1067 public void requestLayout() {
1068 if (!mDisableRelayout) {
1069 super.requestLayout();
1070 }
1071 }
1072
Sunny Goyal34b65272015-03-11 16:56:52 -07001073 /**
1074 * Applies the item info if it is same as what the view is pointing to currently.
1075 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001076 @Override
1077 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001078 if (getTag() == info) {
1079 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -07001080 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001081 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -07001082
Jon Miranda7f522a22017-07-28 11:56:47 -07001083 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -07001084 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -07001085
Sunny Goyal34b65272015-03-11 16:56:52 -07001086 if (info instanceof AppInfo) {
1087 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -07001088 } else if (info instanceof WorkspaceItemInfo) {
1089 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001090 mActivity.invalidateParent(info);
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001091 } else if (info != null) {
1092 applyFromItemInfoWithIcon(info);
Sunny Goyal34b65272015-03-11 16:56:52 -07001093 }
Winsonc0880492015-08-21 11:16:27 -07001094
Sunny Goyal69b75642015-05-15 17:00:24 -07001095 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001096 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -07001097 }
1098 }
1099
1100 /**
1101 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
1102 */
1103 public void verifyHighRes() {
1104 if (mIconLoadRequest != null) {
1105 mIconLoadRequest.cancel();
1106 mIconLoadRequest = null;
1107 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001108 if (getTag() instanceof ItemInfoWithIcon) {
1109 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -07001110 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001111 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -07001112 .updateIconInBackground(BubbleTextView.this, info);
1113 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001114 }
1115 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -07001116
Jon Miranda47170112017-03-03 14:57:14 -08001117 public int getIconSize() {
1118 return mIconSize;
1119 }
Adam Cohen65086992020-02-19 08:40:49 -08001120
Brandon Dayauond3755f42022-08-29 11:48:27 -07001121 public boolean isDisplaySearchResult() {
Brandon Dayauon47000ae2023-09-11 15:28:11 -07001122 return mDisplay == DISPLAY_SEARCH_RESULT
1123 || mDisplay == DISPLAY_SEARCH_RESULT_SMALL
1124 || mDisplay == DISPLAY_SEARCH_RESULT_APP_ROW;
1125 }
1126
1127 public int getIconDisplay() {
1128 return mDisplay;
Brandon Dayauond3755f42022-08-29 11:48:27 -07001129 }
1130
Sunny Goyal82dfc152023-02-24 16:50:09 -08001131 @Override
1132 public MultiTranslateDelegate getTranslateDelegate() {
1133 return mTranslateDelegate;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001134 }
1135
1136 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001137 public void setReorderBounceScale(float scale) {
1138 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -07001139 super.setScaleX(scale);
1140 super.setScaleY(scale);
1141 }
1142
Sunny Goyal82dfc152023-02-24 16:50:09 -08001143 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001144 public float getReorderBounceScale() {
1145 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -07001146 }
1147
Adam Cohen65086992020-02-19 08:40:49 -08001148 @Override
1149 public int getViewType() {
1150 return DRAGGABLE_ICON;
1151 }
1152
1153 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -07001154 public void getWorkspaceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001155 getIconBounds(mIconSize, bounds);
Adam Cohen65086992020-02-19 08:40:49 -08001156 }
1157
Adam Cohenc77bc452020-05-07 11:55:19 -07001158 public void getSourceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001159 getIconBounds(mIconSize, bounds);
Adam Cohenc77bc452020-05-07 11:55:19 -07001160 }
1161
Adam Cohen65086992020-02-19 08:40:49 -08001162 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -07001163 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001164 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -08001165 setForceHideDot(true);
Sunny Goyal8b9919d2021-04-07 14:45:17 -07001166 return () -> { };
Adam Cohen65086992020-02-19 08:40:49 -08001167 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001168
1169 private void resetIconScale() {
Sunny Goyal52f4c162022-04-23 16:02:03 +00001170 if (mIcon != null) {
1171 mIcon.resetScale();
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001172 }
1173 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001174
1175 private void updateIcon(Drawable newIcon) {
1176 if (mLayoutHorizontal) {
1177 setCompoundDrawablesRelative(newIcon, null, null, null);
1178 } else {
1179 setCompoundDrawables(null, newIcon, null, null);
1180 }
1181 }
Steven Ng707b1182021-09-13 11:54:58 +01001182
1183 private String getAppLabelPluralString(String appName, int notificationCount) {
1184 MessageFormat icuCountFormat = new MessageFormat(
1185 getResources().getString(R.string.dotted_app_label),
1186 Locale.getDefault());
1187 HashMap<String, Object> args = new HashMap();
1188 args.put("app_name", appName);
1189 args.put("count", notificationCount);
1190 return icuCountFormat.format(args);
1191 }
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001192
1193 /**
1194 * Starts a long press action and returns the corresponding pre-drag condition
1195 */
1196 public PreDragCondition startLongPressAction() {
1197 PopupContainerWithArrow popup = PopupContainerWithArrow.showForIcon(this);
1198 return popup != null ? popup.createPreDragCondition(true) : null;
1199 }
1200
1201 /**
1202 * Returns true if the view can show long-press popup
1203 */
1204 public boolean canShowLongPressPopup() {
1205 return getTag() instanceof ItemInfo && ShortcutUtil.supportsShortcuts((ItemInfo) getTag());
1206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207}