blob: ba65bea5b04f49e440ca7cae78f5670135909e11 [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;
Pat Manning4c27e8c2023-10-19 12:37:55 +010020
21import static com.android.launcher3.Flags.enableCursorHoverStates;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070022import static com.android.launcher3.config.FeatureFlags.ENABLE_ICON_LABEL_AUTO_SCALING;
Sunny Goyal14168432019-10-24 15:59:49 -070023import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon;
Sunny Goyald872a972021-11-24 18:07:04 -080024import static com.android.launcher3.icons.BitmapInfo.FLAG_NO_BADGE;
Brandon Dayauon8fe6bd22023-11-03 14:41:45 +000025import static com.android.launcher3.icons.BitmapInfo.FLAG_SKIP_USER_BADGE;
Sunny Goyald872a972021-11-24 18:07:04 -080026import static com.android.launcher3.icons.BitmapInfo.FLAG_THEMED;
Sunny Goyal066ace12018-11-05 11:08:31 -080027import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
Federico Baron4eb502a2022-12-14 00:04:53 -080028import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE;
29import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal066ace12018-11-05 11:08:31 -080030
Tony4043b052018-08-17 13:47:39 -070031import android.animation.Animator;
32import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080033import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070035import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070036import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070038import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070039import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080040import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070041import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.drawable.Drawable;
Steven Ng707b1182021-09-13 11:54:58 +010043import android.icu.text.MessageFormat;
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -070044import android.text.StaticLayout;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070045import android.text.TextPaint;
Andy Wickham56be73b2022-01-18 09:13:43 -080046import android.text.TextUtils;
Sunny Goyal66dccad2018-03-19 12:00:51 -070047import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080048import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080049import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070050import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070051import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080052import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070053import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080054import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080055import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070056
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000057import androidx.annotation.Nullable;
Hyunyoung Song5809a402021-02-02 01:19:18 -080058import androidx.annotation.UiThread;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080059import androidx.annotation.VisibleForTesting;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070060
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -070061import com.android.launcher3.accessibility.BaseAccessibilityDelegate;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080062import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf34bee82018-12-03 18:11:39 -080063import com.android.launcher3.dot.DotInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070064import com.android.launcher3.dragndrop.DragOptions.PreDragCondition;
Adam Cohen65086992020-02-19 08:40:49 -080065import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070066import com.android.launcher3.folder.FolderIcon;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070067import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080068import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080069import com.android.launcher3.icons.DotRenderer;
Sunny Goyal572aca42021-03-24 15:21:39 -070070import com.android.launcher3.icons.FastBitmapDrawable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080071import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal572aca42021-03-24 15:21:39 -070072import com.android.launcher3.icons.PlaceHolderIconDrawable;
Sunny Goyal79e52fc2021-02-03 10:22:28 -080073import com.android.launcher3.icons.cache.HandlerRunnable;
Sunny Goyale396abf2020-04-06 15:11:17 -070074import com.android.launcher3.model.data.AppInfo;
75import com.android.launcher3.model.data.ItemInfo;
76import com.android.launcher3.model.data.ItemInfoWithIcon;
Sunny Goyale396abf2020-04-06 15:11:17 -070077import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070078import com.android.launcher3.popup.PopupContainerWithArrow;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080079import com.android.launcher3.search.StringMatcherUtility;
80import com.android.launcher3.util.IntArray;
Tony Wickham1e29b042023-03-16 19:58:30 +000081import com.android.launcher3.util.MultiTranslateDelegate;
Samuel Fufa167210a2020-05-12 18:40:11 -070082import com.android.launcher3.util.SafeCloseable;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070083import com.android.launcher3.util.ShortcutUtil;
fbaron1406a0f2023-05-11 08:46:46 -070084import com.android.launcher3.util.Themes;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080085import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070086import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070087
Sunny Goyalc469aad2015-10-01 11:24:23 -070088import java.text.NumberFormat;
Steven Ng707b1182021-09-13 11:54:58 +010089import java.util.HashMap;
90import java.util.Locale;
Sunny Goyalc469aad2015-10-01 11:24:23 -070091
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092/**
93 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
94 * because we want to make the bubble taller than the text and TextView's clip is
95 * too aggressive.
96 */
Sunny Goyalb65d7662021-03-07 15:09:11 -080097public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver,
Hyunyoung Songdb9a53d2021-02-17 00:35:10 -080098 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070099
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700100 private static final int DISPLAY_WORKSPACE = 0;
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700101 public static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700102 private static final int DISPLAY_FOLDER = 2;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700103 protected static final int DISPLAY_TASKBAR = 5;
Brandon Dayauon7e376162023-07-10 16:17:26 -0700104 public static final int DISPLAY_SEARCH_RESULT = 6;
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700105 public static final int DISPLAY_SEARCH_RESULT_SMALL = 7;
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700106 public static final int DISPLAY_PREDICTION_ROW = 8;
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700107 public static final int DISPLAY_SEARCH_RESULT_APP_ROW = 9;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700108
Sunny Goyal486adb02021-10-21 11:29:52 -0700109 private static final float MIN_LETTER_SPACING = -0.05f;
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700110 private static final int MAX_SEARCH_LOOP_COUNT = 20;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800111 private static final Character NEW_LINE = '\n';
112 private static final String EMPTY = "";
113 private static final StringMatcherUtility.StringMatcher MATCHER =
114 StringMatcherUtility.StringMatcher.getInstance();
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700115
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700116 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -0700117
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700118 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800119
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800120 private IntArray mBreakPointsIntArray;
121 private CharSequence mLastOriginalText;
122 private CharSequence mLastModifiedText;
123
Tony Wickhamf34bee82018-12-03 18:11:39 -0800124 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
125 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800126 @Override
127 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800128 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800129 }
130
131 @Override
132 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800133 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800134 bubbleTextView.invalidate();
135 }
136 };
137
Sunny Goyaled7a6932018-04-13 11:41:50 -0700138 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
139 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700140 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700141 public Float get(BubbleTextView bubbleTextView) {
142 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700143 }
144
145 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700146 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700147 bubbleTextView.setTextAlpha(alpha);
148 }
149 };
150
Sunny Goyal82dfc152023-02-24 16:50:09 -0800151 private final MultiTranslateDelegate mTranslateDelegate = new MultiTranslateDelegate(this);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800152 private final ActivityContext mActivity;
Sunny Goyalc4c32692021-05-21 14:23:29 -0700153 private FastBitmapDrawable mIcon;
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700154 private DeviceProfile mDeviceProfile;
Adam Cohen65086992020-02-19 08:40:49 -0800155 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700156
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800157 protected int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700158
Sunny Goyaled7a6932018-04-13 11:41:50 -0700159 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700160
Kevin Limb3b467c2023-07-21 02:15:34 +0000161 private boolean mLayoutHorizontal;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700162 private final boolean mIsRtl;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700163 private final int mIconSize;
164
165 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyald872a972021-11-24 18:07:04 -0800166 private boolean mHideBadge = false;
167 @ViewDebug.ExportedProperty(category = "launcher")
Brandon Dayauon8fe6bd22023-11-03 14:41:45 +0000168 private boolean mSkipUserBadge = false;
169 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700170 private boolean mIsIconVisible = true;
171 @ViewDebug.ExportedProperty(category = "launcher")
172 private int mTextColor;
173 @ViewDebug.ExportedProperty(category = "launcher")
Jon Spivack68862142021-10-28 18:37:19 -0700174 private ColorStateList mTextColorStateList;
175 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700176 private float mTextAlpha = 1;
177
Tony4043b052018-08-17 13:47:39 -0700178 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800179 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800180 private DotRenderer mDotRenderer;
Brandon Dayauon812664d2023-11-30 08:51:38 -0800181 private Locale mCurrentLocale;
Tony Wickham8912b042018-11-29 15:28:53 -0800182 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
Sunny Goyal67555872021-05-21 12:56:55 -0700183 protected DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800184 private Animator mDotScaleAnim;
185 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700186
Sunny Goyal4ffec482016-02-09 11:28:52 -0800187 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800188 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800189 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700190 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800191 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700192 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500193
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800194 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700195
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400196 private boolean mEnableIconUpdateAnimation = false;
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700199 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 }
201
202 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700203 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 }
205
206 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
207 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800208 mActivity = ActivityContext.lookupContext(context);
Pat Manningc49d60b2023-09-13 16:40:27 +0100209 FastBitmapDrawable.setFlagHoverEnabled(enableCursorHoverStates());
Adam Cohen96bb7982014-07-07 11:58:56 -0700210
Adam Cohen96bb7982014-07-07 11:58:56 -0700211 TypedArray a = context.obtainStyledAttributes(attrs,
212 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800213 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700214 mIsRtl = (getResources().getConfiguration().getLayoutDirection()
215 == View.LAYOUT_DIRECTION_RTL);
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700216 mDeviceProfile = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700217
Samuel Fufac96fa242019-09-26 23:06:32 -0700218 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700219 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700220 if (mDisplay == DISPLAY_WORKSPACE) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700221 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.iconTextSizePx);
222 setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
223 defaultIconSize = mDeviceProfile.iconSizePx;
224 setCenterVertically(mDeviceProfile.iconCenterVertically);
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700225 } else if (mDisplay == DISPLAY_ALL_APPS || mDisplay == DISPLAY_PREDICTION_ROW
226 || mDisplay == DISPLAY_SEARCH_RESULT_APP_ROW) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700227 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.allAppsIconTextSizePx);
228 setCompoundDrawablePadding(mDeviceProfile.allAppsIconDrawablePaddingPx);
229 defaultIconSize = mDeviceProfile.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700230 } else if (mDisplay == DISPLAY_FOLDER) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700231 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.folderChildTextSizePx);
232 setCompoundDrawablePadding(mDeviceProfile.folderChildDrawablePaddingPx);
233 defaultIconSize = mDeviceProfile.folderChildIconSizePx;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700234 } else if (mDisplay == DISPLAY_SEARCH_RESULT) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700235 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.allAppsIconTextSizePx);
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700236 defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size);
237 } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
238 defaultIconSize = getResources().getDimensionPixelSize(
239 R.dimen.search_row_small_icon_size);
Tony Wickham794fe4f2021-01-11 14:54:23 -0600240 } else if (mDisplay == DISPLAY_TASKBAR) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700241 defaultIconSize = mDeviceProfile.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700242 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700243 // widget_selection or shortcut_popup
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700244 defaultIconSize = mDeviceProfile.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700245 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700246
Winson1f064272016-07-18 17:18:02 -0700247 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700248
249 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
250 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700251 a.recycle();
252
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800253 mLongPressHelper = new CheckLongPressHelper(this);
254
Tony Wickham8912b042018-11-29 15:28:53 -0800255 mDotParams = new DotRenderer.DrawParams();
256
Brandon Dayauon812664d2023-11-30 08:51:38 -0800257 mCurrentLocale = context.getResources().getConfiguration().locale;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700258 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700259 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700260 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 }
262
Sunny Goyal66dccad2018-03-19 12:00:51 -0700263 @Override
264 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
265 // Disable marques when not focused to that, so that updating text does not cause relayout.
266 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
267 super.onFocusChanged(focused, direction, previouslyFocusedRect);
268 }
269
Sunny Goyald872a972021-11-24 18:07:04 -0800270 public void setHideBadge(boolean hideBadge) {
271 mHideBadge = hideBadge;
272 }
273
Brandon Dayauon8fe6bd22023-11-03 14:41:45 +0000274 public void setSkipUserBadge(boolean skipUserBadge) {
275 mSkipUserBadge = skipUserBadge;
276 }
277
Jon Miranda52549442017-10-26 11:28:06 -0700278 /**
279 * Resets the view so it can be recycled.
280 */
281 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800282 mDotInfo = null;
James O'Leary1557ba32022-05-04 16:49:03 +0000283 mDotParams.dotColor = Color.TRANSPARENT;
284 mDotParams.appColor = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800285 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800286 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800287 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800288 setBackground(null);
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700289 if (FeatureFlags.enableTwolineAllapps() || FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()) {
hyunyoungs825480f2023-03-24 10:11:15 -0700290 setMaxLines(1);
291 }
Sunny Goyal52f4c162022-04-23 16:02:03 +0000292
293 setTag(null);
294 if (mIconLoadRequest != null) {
295 mIconLoadRequest.cancel();
296 mIconLoadRequest = null;
297 }
Jon Miranda52549442017-10-26 11:28:06 -0700298 }
299
Tony Wickhamf34bee82018-12-03 18:11:39 -0800300 private void cancelDotScaleAnim() {
301 if (mDotScaleAnim != null) {
302 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700303 }
304 }
305
Jagrut Desai47bf3222023-10-30 12:15:22 -0700306 public void animateDotScale(float... dotScales) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800307 cancelDotScaleAnim();
308 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
309 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700310 @Override
311 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800312 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700313 }
314 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800315 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700316 }
317
Hyunyoung Song5809a402021-02-02 01:19:18 -0800318 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700319 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
Tony Wickham175d1522021-08-25 09:18:41 -0700320 applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0);
321 }
322
323 @UiThread
324 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) {
Federico Baron4eb502a2022-12-14 00:04:53 -0800325 applyFromWorkspaceItem(info, null);
Winson Chung5f8afe62013-08-12 16:19:28 -0700326 }
327
Tony Wickham175d1522021-08-25 09:18:41 -0700328 /**
329 * Returns whether the newInfo differs from the current getTag().
330 */
331 public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) {
332 WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo
333 ? (WorkspaceItemInfo) getTag()
334 : null;
335 boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null
336 && newInfo.getTargetComponent() != null
337 && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent());
338 return changedIcons && isShown();
339 }
340
Adam Cohen29794c52019-06-21 12:50:30 -0700341 @Override
342 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -0700343 if (delegate instanceof BaseAccessibilityDelegate) {
Adam Cohen29794c52019-06-21 12:50:30 -0700344 super.setAccessibilityDelegate(delegate);
345 } else {
346 // NO-OP
347 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
348 // delegate incorrectly. There are no cases when we shouldn't be using the
349 // LauncherAccessibilityDelegate for BubbleTextView.
350 }
351 }
352
Hyunyoung Song5809a402021-02-02 01:19:18 -0800353 @UiThread
Federico Baron4eb502a2022-12-14 00:04:53 -0800354 public void applyFromWorkspaceItem(WorkspaceItemInfo info, PreloadIconDrawable icon) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800355 applyIconAndLabel(info);
sfufa@google.comca76de02021-09-24 08:55:48 -0700356 setItemInfo(info);
Federico Baron4eb502a2022-12-14 00:04:53 -0800357 applyLoadingState(icon);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800358 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800359 setDownloadStateContentDescription(info, info.getProgressLevel());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800360 }
361
Hyunyoung Song5809a402021-02-02 01:19:18 -0800362 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700363 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800364 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700365
Sunny Goyal95899162019-03-27 16:03:06 -0700366 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700367 setItemInfo(info);
368
Sunny Goyal34b65272015-03-11 16:56:52 -0700369
370 // Verify high res immediately
371 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800372
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000373 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800374 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700375 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800376 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800377 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal508da152014-08-14 10:53:27 -0700378 }
379
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700380 /**
381 * Apply label and tag using a generic {@link ItemInfoWithIcon}
382 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800383 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700384 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800385 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700386 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700387 setItemInfo(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700388
389 // Verify high res immediately
390 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800391
392 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700393 }
394
Sunny Goyalf0ff70a2022-05-11 07:23:10 -0700395 protected void setItemInfo(ItemInfoWithIcon itemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700396 setTag(itemInfo);
sfufa@google.com0fe464d2021-10-14 14:34:50 -0700397 }
398
Brandon Dayauon0ae52c92023-09-06 17:27:18 -0700399 @VisibleForTesting
Hyunyoung Song5809a402021-02-02 01:19:18 -0800400 @UiThread
Brandon Dayauon0ae52c92023-09-06 17:27:18 -0700401 public void applyIconAndLabel(ItemInfoWithIcon info) {
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800402 int flags = shouldUseTheme() ? FLAG_THEMED : 0;
Brandon Dayauon0ae52c92023-09-06 17:27:18 -0700403 // Remove badge on icons smaller than 48dp.
404 if (mHideBadge || mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
Sunny Goyald872a972021-11-24 18:07:04 -0800405 flags |= FLAG_NO_BADGE;
406 }
Brandon Dayauon8fe6bd22023-11-03 14:41:45 +0000407 if (mSkipUserBadge) {
408 flags |= FLAG_SKIP_USER_BADGE;
409 }
Sunny Goyald872a972021-11-24 18:07:04 -0800410 FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags);
James O'Leary1557ba32022-05-04 16:49:03 +0000411 mDotParams.appColor = iconDrawable.getIconColor();
fbaron1406a0f2023-05-11 08:46:46 -0700412 mDotParams.dotColor = Themes.getAttrColor(getContext(), R.attr.notificationDotColor);
Sunny Goyalf4204382016-07-13 10:46:07 -0700413 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000414 applyLabel(info);
415 }
416
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800417 protected boolean shouldUseTheme() {
Brandon Dayauon08cdbc52023-09-14 19:05:09 +0000418 return (mDisplay == DISPLAY_WORKSPACE || mDisplay == DISPLAY_FOLDER
419 || mDisplay == DISPLAY_TASKBAR) && Themes.isThemedIconEnabled(getContext());
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800420 }
421
Brandon Dayauon7d673312023-04-03 12:32:24 -0700422 /**
423 * Only if actual text can be displayed in two line, the {@code true} value will be effective.
424 */
425 protected boolean shouldUseTwoLine() {
Brandon Dayauon812664d2023-11-30 08:51:38 -0800426 return (FeatureFlags.enableTwolineAllapps() && isCurrentLanguageEnglish())
427 && (mDisplay == DISPLAY_ALL_APPS || mDisplay == DISPLAY_PREDICTION_ROW);
428 }
429
430 protected boolean isCurrentLanguageEnglish() {
431 return mCurrentLocale.equals(Locale.US);
Brandon Dayauon7d673312023-04-03 12:32:24 -0700432 }
433
Hyunyoung Song5809a402021-02-02 01:19:18 -0800434 @UiThread
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800435 @VisibleForTesting
436 public void applyLabel(ItemInfoWithIcon info) {
437 CharSequence label = info.title;
438 if (label != null) {
439 mLastOriginalText = label;
440 mLastModifiedText = mLastOriginalText;
441 mBreakPointsIntArray = StringMatcherUtility.getListOfBreakpoints(label, MATCHER);
442 setText(label);
443 }
Sunny Goyalf4204382016-07-13 10:46:07 -0700444 if (info.contentDescription != null) {
445 setContentDescription(info.isDisabled()
446 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
447 : info.contentDescription);
448 }
449 }
450
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700451 /** This is used for testing to forcefully set the display. */
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800452 @VisibleForTesting
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700453 public void setDisplay(int display) {
454 mDisplay = display;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800455 }
456
Winson Chung7501adf2015-06-02 11:24:28 -0700457 /**
458 * Overrides the default long press timeout.
459 */
Tony2ca999c2018-09-24 17:24:51 -0400460 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
461 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700462 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800465 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700466 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800467 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800468 }
Sunny Goyal508da152014-08-14 10:53:27 -0700469 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800470
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800471 @Override
472 protected int[] onCreateDrawableState(int extraSpace) {
473 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
474 if (mStayPressed) {
475 mergeDrawableStates(drawableState, STATE_PRESSED);
476 }
477 return drawableState;
478 }
479
Winson Chungb745afb2015-03-02 11:51:23 -0800480 /** Returns the icon for this view. */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700481 public FastBitmapDrawable getIcon() {
Winson Chungb745afb2015-03-02 11:51:23 -0800482 return mIcon;
483 }
484
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700485 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800486 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700487 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700488 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700489 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700490 return false;
491 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700492 if (isLongClickable()) {
493 super.onTouchEvent(event);
494 mLongPressHelper.onTouchEvent(event);
495 // Keep receiving the rest of the events
496 return true;
497 } else {
498 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700499 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800500 }
501
Sunny Goyal44a3b202020-07-09 08:27:06 -0700502 /**
503 * Returns true if the touch down at the provided position be ignored
504 */
505 protected boolean shouldIgnoreTouchDown(float x, float y) {
Tony Wickham42b03952021-08-31 10:41:36 -0700506 if (mDisplay == DISPLAY_TASKBAR) {
507 // Allow touching within padding on taskbar, given icon sizes are smaller.
508 return false;
509 }
Sunny Goyal44a3b202020-07-09 08:27:06 -0700510 return y < getPaddingTop()
511 || x < getPaddingLeft()
512 || y > getHeight() - getPaddingBottom()
513 || x > getWidth() - getPaddingRight();
514 }
515
Michael Jurkaddd62e92011-02-16 17:49:14 -0800516 void setStayPressed(boolean stayPressed) {
517 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800518 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800519 }
520
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700521 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800522 public void onVisibilityAggregated(boolean isVisible) {
523 super.onVisibilityAggregated(isVisible);
524 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800525 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800526 }
527 }
528
Tony Wickham8ac277e2021-05-24 15:47:38 -0700529 public void clearPressedBackground() {
Sunny Goyal508da152014-08-14 10:53:27 -0700530 setPressed(false);
531 setStayPressed(false);
532 }
533
534 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700535 public boolean onKeyUp(int keyCode, KeyEvent event) {
536 // Unlike touch events, keypress event propagate pressed state change immediately,
537 // without waiting for onClickHandler to execute. Disable pressed state changes here
538 // to avoid flickering.
539 mIgnorePressedStateChange = true;
540 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700541 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800542 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700543 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800544 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800545
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700546 @Override
547 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
548 super.onSizeChanged(w, h, oldw, oldh);
549 checkForEllipsis();
550 }
551
552 @Override
553 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
554 super.onTextChanged(text, start, lengthBefore, lengthAfter);
555 checkForEllipsis();
556 }
557
558 private void checkForEllipsis() {
559 if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) {
560 return;
561 }
562 float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
563 if (width <= 0) {
564 return;
565 }
566 setLetterSpacing(0);
567
568 String text = getText().toString();
569 TextPaint paint = getPaint();
570 if (paint.measureText(text) < width) {
571 return;
572 }
573
574 float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING);
575 // Reset the paint value so that the call to TextView does appropriate diff.
576 paint.setLetterSpacing(0);
577 setLetterSpacing(spacing);
578 }
579
580 /**
581 * Find the appropriate text spacing to display the provided text
582 * @param paint the paint used by the text view
583 * @param text the text to display
584 * @param allowedWidthPx available space to render the text
585 * @param minSpacingEm minimum spacing allowed between characters
586 * @return the final textSpacing value
587 *
588 * @see #setLetterSpacing(float)
589 */
590 private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx,
591 float minSpacingEm) {
592 paint.setLetterSpacing(minSpacingEm);
593 if (paint.measureText(text) > allowedWidthPx) {
594 // If there is no result at high limit, we can do anything more
595 return minSpacingEm;
596 }
597
598 float lowLimit = 0;
599 float highLimit = minSpacingEm;
600
601 for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) {
602 float value = (lowLimit + highLimit) / 2;
603 paint.setLetterSpacing(value);
604 if (paint.measureText(text) < allowedWidthPx) {
605 highLimit = value;
606 } else {
607 lowLimit = value;
608 }
609 }
610
611 // At the end error on the higher side
612 return highLimit;
613 }
614
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700615 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800616 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700617 super.onDraw(canvas);
618 }
619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700621 public void onDraw(Canvas canvas) {
622 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800623 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700624 }
625
Tony Wickham1237df02017-02-24 08:59:36 -0800626 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800627 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700628 *
Tony Wickham1237df02017-02-24 08:59:36 -0800629 * @param canvas The canvas to draw to.
630 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800631 protected void drawDotIfNecessary(Canvas canvas) {
632 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800633 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700634 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
635 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800636 final int scrollX = getScrollX();
637 final int scrollY = getScrollY();
638 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800639 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800640 canvas.translate(-scrollX, -scrollY);
641 }
642 }
643
Jon Miranda9b78e192019-08-30 18:42:01 -0700644 @Override
645 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800646 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800647 return;
648 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800649 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800650
Tony Wickhamf34bee82018-12-03 18:11:39 -0800651 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800652 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800653 } else if (hasDot()) {
654 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800655 }
656 }
657
Brandon Dayauon7e376162023-07-10 16:17:26 -0700658 @VisibleForTesting
659 public boolean getForceHideDot() {
660 return mForceHideDot;
661 }
662
Jagrut Desai47bf3222023-10-30 12:15:22 -0700663 public boolean hasDot() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800664 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800665 }
666
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700667 /**
668 * Get the icon bounds on the view depending on the layout type.
669 */
Tony Wickham1237df02017-02-24 08:59:36 -0800670 public void getIconBounds(Rect outBounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700671 getIconBounds(mIconSize, outBounds);
Tony05d98c22018-07-23 08:01:15 -0700672 }
673
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700674 /**
675 * Get the icon bounds on the view depending on the layout type.
676 */
677 public void getIconBounds(int iconSize, Rect outBounds) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700678 outBounds.set(0, 0, iconSize, iconSize);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700679 if (mLayoutHorizontal) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700680 int top = (getHeight() - iconSize) / 2;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700681 if (mIsRtl) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700682 outBounds.offsetTo(getWidth() - iconSize - getPaddingRight(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700683 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700684 outBounds.offsetTo(getPaddingLeft(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700685 }
686 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700687 outBounds.offset((getWidth() - iconSize) / 2, getPaddingTop());
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690
Jon Miranda228877d2021-02-09 11:05:00 -0500691 /**
Kevin Limb3b467c2023-07-21 02:15:34 +0000692 * Sets whether the layout is horizontal.
693 */
694 public void setLayoutHorizontal(boolean layoutHorizontal) {
695 if (mLayoutHorizontal == layoutHorizontal) {
696 return;
697 }
698
699 mLayoutHorizontal = layoutHorizontal;
700 applyCompoundDrawables(getIconOrTransparentColor());
701 }
702
703 /**
Jon Miranda228877d2021-02-09 11:05:00 -0500704 * Sets whether to vertically center the content.
705 */
706 public void setCenterVertically(boolean centerVertically) {
707 mCenterVertically = centerVertically;
708 }
709
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 @Override
Winson1f064272016-07-18 17:18:02 -0700711 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700712 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson1f064272016-07-18 17:18:02 -0700713 if (mCenterVertically) {
714 Paint.FontMetrics fm = getPaint().getFontMetrics();
715 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
716 (int) Math.ceil(fm.bottom - fm.top);
Winson1f064272016-07-18 17:18:02 -0700717 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
718 getPaddingBottom());
719 }
Brandon Dayauon7d673312023-04-03 12:32:24 -0700720 // Only apply two line for all_apps and device search only if necessary.
721 if (shouldUseTwoLine() && (mLastOriginalText != null)) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700722 int allowedVerticalSpace = height - getPaddingTop() - getPaddingBottom()
723 - mDeviceProfile.allAppsIconSizePx
724 - mDeviceProfile.allAppsIconDrawablePaddingPx;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800725 CharSequence modifiedString = modifyTitleToSupportMultiLine(
726 MeasureSpec.getSize(widthMeasureSpec) - getCompoundPaddingLeft()
727 - getCompoundPaddingRight(),
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700728 allowedVerticalSpace,
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800729 mLastOriginalText,
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700730 getPaint(),
731 mBreakPointsIntArray,
732 getLineSpacingMultiplier(),
733 getLineSpacingExtra());
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800734 if (!TextUtils.equals(modifiedString, mLastModifiedText)) {
735 mLastModifiedText = modifiedString;
736 setText(modifiedString);
737 // if text contains NEW_LINE, set max lines to 2
738 if (TextUtils.indexOf(modifiedString, NEW_LINE) != -1) {
739 setSingleLine(false);
740 setMaxLines(2);
741 } else {
742 setSingleLine(true);
743 setMaxLines(1);
744 }
745 }
746 }
Winson1f064272016-07-18 17:18:02 -0700747 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
748 }
749
750 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700751 public void setTextColor(int color) {
752 mTextColor = color;
Jon Spivack68862142021-10-28 18:37:19 -0700753 mTextColorStateList = null;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700754 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700755 }
756
Adam Cohen96bb7982014-07-07 11:58:56 -0700757 @Override
758 public void setTextColor(ColorStateList colors) {
759 mTextColor = colors.getDefaultColor();
Jon Spivack68862142021-10-28 18:37:19 -0700760 mTextColorStateList = colors;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700761 if (Float.compare(mTextAlpha, 1) == 0) {
762 super.setTextColor(colors);
763 } else {
764 super.setTextColor(getModifiedColor());
765 }
Adam Cohen477828c2013-09-20 12:05:49 -0700766 }
767
Tony7308cde2017-06-27 22:38:33 -0700768 public boolean shouldTextBeVisible() {
769 // Text should be visible everywhere but the hotseat.
770 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
771 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700772 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
773 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700774 }
775
Winson Chung5f8afe62013-08-12 16:19:28 -0700776 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700777 setTextAlpha(visible ? 1 : 0);
778 }
779
780 private void setTextAlpha(float alpha) {
781 mTextAlpha = alpha;
Jon Spivack68862142021-10-28 18:37:19 -0700782 if (mTextColorStateList != null) {
783 setTextColor(mTextColorStateList);
784 } else {
785 super.setTextColor(getModifiedColor());
786 }
Sunny Goyaled7a6932018-04-13 11:41:50 -0700787 }
788
789 private int getModifiedColor() {
790 if (mTextAlpha == 0) {
791 // Special case to prevent text shadows in high contrast mode
792 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700793 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800794 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700795 }
796
797 /**
798 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700799 *
Tony8f402802017-06-16 17:24:54 -0700800 * @param fadeIn Whether the text should fade in or fade out.
801 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700802 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700803 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700804 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700805 }
806
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800807 /**
808 * Generate a new string that will support two line text depending on the current string.
809 * This method calculates the limited width of a text view and creates a string to fit as
810 * many words as it can until the limit is reached. Once the limit is reached, we decide to
811 * either return the original title or continue on a new line. How to get the new string is by
812 * iterating through the list of break points and determining if the strings between the break
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700813 * points can fit within the line it is in. We will show the modified string if there is enough
814 * horizontal and vertical space, otherwise this method will just return the original string.
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800815 * Example assuming each character takes up one spot:
816 * title = "Battery Stats", breakpoint = [6], stringPtr = 0, limitedWidth = 7
817 * We get the current word -> from sublist(0, breakpoint[i]+1) so sublist (0,7) -> Battery,
818 * now stringPtr = 7 then from sublist(7) the current string is " Stats" and the runningWidth
819 * at this point exceeds limitedWidth and so we put " Stats" onto the next line (after checking
820 * if the first char is a SPACE, we trim to append "Stats". So resulting string would be
821 * "Battery\nStats"
822 */
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700823 public static CharSequence modifyTitleToSupportMultiLine(int limitedWidth, int limitedHeight,
824 CharSequence title, TextPaint paint, IntArray breakPoints, float spacingMultiplier,
825 float spacingExtra) {
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800826 // current title is less than the width allowed so we can just skip
827 if (title == null || paint.measureText(title, 0, title.length()) <= limitedWidth) {
828 return title;
829 }
830 float currentWordWidth, runningWidth = 0;
831 CharSequence currentWord;
832 StringBuilder newString = new StringBuilder();
Brandon Dayauon6c345f42023-10-03 16:38:42 -0700833 // TODO: Remove when ENABLE_ICON_LABEL_AUTO_SCALING feature flag is being cleaned up.
834 paint.setLetterSpacing(MIN_LETTER_SPACING);
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800835 int stringPtr = 0;
836 for (int i = 0; i < breakPoints.size()+1; i++) {
837 if (i < breakPoints.size()) {
838 currentWord = title.subSequence(stringPtr, breakPoints.get(i)+1);
839 } else {
840 // last word from recent breakpoint until the end of the string
841 currentWord = title.subSequence(stringPtr, title.length());
842 }
843 currentWordWidth = paint.measureText(currentWord,0, currentWord.length());
844 runningWidth += currentWordWidth;
845 if (runningWidth <= limitedWidth) {
846 newString.append(currentWord);
847 } else {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700848 if (i != 0) {
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800849 // If putting word onto a new line, make sure there is no space or new line
850 // character in the beginning of the current word and just put in the rest of
851 // the characters.
852 CharSequence lastCharacters = title.subSequence(stringPtr, title.length());
853 int beginningLetterType =
854 Character.getType(Character.codePointAt(lastCharacters,0));
855 if (beginningLetterType == Character.SPACE_SEPARATOR
856 || beginningLetterType == Character.LINE_SEPARATOR) {
857 lastCharacters = lastCharacters.length() > 1
858 ? lastCharacters.subSequence(1, lastCharacters.length())
859 : EMPTY;
860 }
861 newString.append(NEW_LINE).append(lastCharacters);
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700862 StaticLayout staticLayout = new StaticLayout(newString, paint, limitedWidth,
863 ALIGN_NORMAL, spacingMultiplier, spacingExtra, false);
864 if (staticLayout.getHeight() < limitedHeight) {
865 return newString.toString();
866 }
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800867 }
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700868 // if the first words exceeds width, just return as the first line will ellipse
869 return title;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800870 }
871 if (i >= breakPoints.size()) {
872 // no need to look forward into the string if we've already finished processing
873 break;
874 }
875 stringPtr = breakPoints.get(i)+1;
876 }
877 return newString.toString();
878 }
879
Winson Chungaffd7b42010-08-20 15:11:56 -0700880 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800881 public void cancelLongPress() {
882 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800883 mLongPressHelper.cancelLongPress();
884 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500885
Samuel Fufac2820182021-01-22 11:47:25 -0600886 /**
887 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000888 *
889 * If this app is installing, the progress bar will be updated with the installation progress.
890 * If this app is installed and downloading incrementally, the progress bar will be updated
891 * with the total download progress.
892 */
Federico Baron4eb502a2022-12-14 00:04:53 -0800893 public void applyLoadingState(PreloadIconDrawable icon) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800894 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700895 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Federico Baron4eb502a2022-12-14 00:04:53 -0800896 if ((info.runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0
897 || info.hasPromiseIconUi()
898 || (info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0
fbaron461a9aa2023-09-27 16:13:27 -0700899 || (icon != null)) {
Federico Barondb599b22023-03-09 14:44:41 -0800900 updateProgressBarUi(info.getProgressLevel() == 100 ? icon : null);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700901 }
902 }
903 }
904
Federico Baron4eb502a2022-12-14 00:04:53 -0800905 private void updateProgressBarUi(PreloadIconDrawable oldIcon) {
906 FastBitmapDrawable originalIcon = mIcon;
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800907 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Federico Baron4eb502a2022-12-14 00:04:53 -0800908 if (preloadDrawable != null && oldIcon != null) {
909 preloadDrawable.maybePerformFinishedAnimation(oldIcon, () -> setIcon(originalIcon));
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000910 }
911 }
912
913 /** Applies the given progress level to the this icon's progress bar. */
914 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800915 public PreloadIconDrawable applyProgressLevel() {
916 if (!(getTag() instanceof ItemInfoWithIcon)) {
917 return null;
918 }
919
920 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
921 int progressLevel = info.getProgressLevel();
922 if (progressLevel >= 100) {
923 setContentDescription(info.contentDescription != null
924 ? info.contentDescription : "");
925 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800926 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800927 } else {
928 setContentDescription(getContext()
929 .getString(R.string.app_waiting_download_title, info.title));
930 }
931 if (mIcon != null) {
932 PreloadIconDrawable preloadIconDrawable;
933 if (mIcon instanceof PreloadIconDrawable) {
934 preloadIconDrawable = (PreloadIconDrawable) mIcon;
935 preloadIconDrawable.setLevel(progressLevel);
Rohit Goyal04fe0422024-01-09 21:23:55 +0530936 preloadIconDrawable.setIsDisabled(isIconDisabled(info));
Mario Bertschler230612f2017-09-27 17:05:21 -0700937 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800938 preloadIconDrawable = makePreloadIcon();
939 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700940 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800941 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400942 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700943 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400944 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700945
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800946 /**
947 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
948 * object.
949 */
950 @Nullable
951 public PreloadIconDrawable makePreloadIcon() {
952 if (!(getTag() instanceof ItemInfoWithIcon)) {
953 return null;
954 }
955
956 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
957 int progressLevel = info.getProgressLevel();
958 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
959
960 preloadDrawable.setLevel(progressLevel);
Rohit Goyal04fe0422024-01-09 21:23:55 +0530961 preloadDrawable.setIsDisabled(isIconDisabled(info));
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800962 return preloadDrawable;
963 }
964
Rohit Goyal04fe0422024-01-09 21:23:55 +0530965 private boolean isIconDisabled(ItemInfoWithIcon info) {
966 if (info.isArchived()) {
967 return info.getProgressLevel() == 0
968 && (info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0;
969 }
970 return info.getProgressLevel() == 0;
971 }
972
Tony Wickhamf34bee82018-12-03 18:11:39 -0800973 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800974 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800975 boolean wasDotted = mDotInfo != null;
976 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
977 boolean isDotted = mDotInfo != null;
978 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700979 if (mDisplay == DISPLAY_ALL_APPS) {
980 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
981 } else {
982 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
983 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800984 if (wasDotted || isDotted) {
985 // Animate when a dot is first added or when it is removed.
986 if (animate && (wasDotted ^ isDotted) && isShown()) {
987 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800988 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800989 cancelDotScaleAnim();
990 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800991 invalidate();
992 }
993 }
Andy Wickham56be73b2022-01-18 09:13:43 -0800994 if (!TextUtils.isEmpty(itemInfo.contentDescription)) {
vadimtda6dad52019-05-21 13:24:23 -0700995 if (itemInfo.isDisabled()) {
996 setContentDescription(getContext().getString(R.string.disabled_app_label,
997 itemInfo.contentDescription));
998 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800999 int count = mDotInfo.getNotificationCount();
Steven Ng707b1182021-09-13 11:54:58 +01001000 setContentDescription(
1001 getAppLabelPluralString(itemInfo.contentDescription.toString(), count));
Tony Wickham305e9202018-01-23 17:46:47 -08001002 } else {
1003 setContentDescription(itemInfo.contentDescription);
1004 }
1005 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -08001006 }
1007 }
1008
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -08001009 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
1010 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
1011 != 0) {
1012 String percentageString = NumberFormat.getPercentInstance()
1013 .format(progressLevel * 0.01);
Federico Baron4eb502a2022-12-14 00:04:53 -08001014 if ((info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -08001015 setContentDescription(getContext()
1016 .getString(
1017 R.string.app_installing_title, info.title, percentageString));
1018 } else if ((info.runtimeStatusFlags
Federico Baron4eb502a2022-12-14 00:04:53 -08001019 & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -08001020 setContentDescription(getContext()
1021 .getString(
1022 R.string.app_downloading_title, info.title, percentageString));
1023 }
1024 }
1025 }
1026
Winson Chungb745afb2015-03-02 11:51:23 -08001027 /**
1028 * Sets the icon for this view based on the layout direction.
1029 */
Sunny Goyalc4c32692021-05-21 14:23:29 -07001030 protected void setIcon(FastBitmapDrawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -07001031 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -07001032 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -07001033 }
Sunny Goyal66dccad2018-03-19 12:00:51 -07001034 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -08001035 if (mIcon != null) {
1036 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
1037 }
Tony17b7f9b2017-05-23 12:19:09 -07001038 }
1039
Jon Miranda9b78e192019-08-30 18:42:01 -07001040 @Override
Tony17b7f9b2017-05-23 12:19:09 -07001041 public void setIconVisible(boolean visible) {
1042 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001043 if (!mIsIconVisible) {
1044 resetIconScale();
1045 }
Kevin Limb3b467c2023-07-21 02:15:34 +00001046 Drawable icon = getIconOrTransparentColor();
Tony17b7f9b2017-05-23 12:19:09 -07001047 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -07001048 }
1049
Kevin Limb3b467c2023-07-21 02:15:34 +00001050 private Drawable getIconOrTransparentColor() {
1051 return mIsIconVisible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
1052 }
1053
Tony Wickham1e29b042023-03-16 19:58:30 +00001054 /** Sets the icon visual state to disabled or not. */
1055 public void setIconDisabled(boolean isDisabled) {
1056 if (mIcon != null) {
1057 mIcon.setIsDisabled(isDisabled);
1058 }
1059 }
1060
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001061 protected boolean iconUpdateAnimationEnabled() {
1062 return mEnableIconUpdateAnimation;
1063 }
1064
Tony Wickham377ed3f2016-07-20 15:21:04 -07001065 protected void applyCompoundDrawables(Drawable icon) {
Jon Mirandafe749972022-03-15 10:02:39 -07001066 if (icon == null) {
1067 // Icon can be null when we use the BubbleTextView for text only.
1068 return;
1069 }
1070
Sunny Goyal66dccad2018-03-19 12:00:51 -07001071 // If we had already set an icon before, disable relayout as the icon size is the
1072 // same as before.
1073 mDisableRelayout = mIcon != null;
1074
1075 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001076
1077 updateIcon(icon);
1078
1079 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -04001080 if (mIcon != null
1081 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001082 && iconUpdateAnimationEnabled()) {
Steven Nge92bc552021-02-10 17:10:15 +00001083 ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon);
Winson Chungb745afb2015-03-02 11:51:23 -08001084 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001085
Sunny Goyal66dccad2018-03-19 12:00:51 -07001086 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -08001087 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001088
Sunny Goyal69b75642015-05-15 17:00:24 -07001089 @Override
1090 public void requestLayout() {
1091 if (!mDisableRelayout) {
1092 super.requestLayout();
1093 }
1094 }
1095
Sunny Goyal34b65272015-03-11 16:56:52 -07001096 /**
1097 * Applies the item info if it is same as what the view is pointing to currently.
1098 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001099 @Override
1100 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001101 if (getTag() == info) {
1102 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -07001103 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001104 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -07001105
Jon Miranda7f522a22017-07-28 11:56:47 -07001106 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -07001107 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -07001108
Sunny Goyal34b65272015-03-11 16:56:52 -07001109 if (info instanceof AppInfo) {
1110 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -07001111 } else if (info instanceof WorkspaceItemInfo) {
1112 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001113 mActivity.invalidateParent(info);
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001114 } else if (info != null) {
1115 applyFromItemInfoWithIcon(info);
Sunny Goyal34b65272015-03-11 16:56:52 -07001116 }
Winsonc0880492015-08-21 11:16:27 -07001117
Sunny Goyal69b75642015-05-15 17:00:24 -07001118 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001119 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -07001120 }
1121 }
1122
1123 /**
1124 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
1125 */
1126 public void verifyHighRes() {
1127 if (mIconLoadRequest != null) {
1128 mIconLoadRequest.cancel();
1129 mIconLoadRequest = null;
1130 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001131 if (getTag() instanceof ItemInfoWithIcon) {
1132 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -07001133 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001134 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -07001135 .updateIconInBackground(BubbleTextView.this, info);
1136 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001137 }
1138 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -07001139
Jon Miranda47170112017-03-03 14:57:14 -08001140 public int getIconSize() {
1141 return mIconSize;
1142 }
Adam Cohen65086992020-02-19 08:40:49 -08001143
Brandon Dayauond3755f42022-08-29 11:48:27 -07001144 public boolean isDisplaySearchResult() {
Brandon Dayauon47000ae2023-09-11 15:28:11 -07001145 return mDisplay == DISPLAY_SEARCH_RESULT
1146 || mDisplay == DISPLAY_SEARCH_RESULT_SMALL
1147 || mDisplay == DISPLAY_SEARCH_RESULT_APP_ROW;
1148 }
1149
1150 public int getIconDisplay() {
1151 return mDisplay;
Brandon Dayauond3755f42022-08-29 11:48:27 -07001152 }
1153
Sunny Goyal82dfc152023-02-24 16:50:09 -08001154 @Override
1155 public MultiTranslateDelegate getTranslateDelegate() {
1156 return mTranslateDelegate;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001157 }
1158
1159 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001160 public void setReorderBounceScale(float scale) {
1161 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -07001162 super.setScaleX(scale);
1163 super.setScaleY(scale);
1164 }
1165
Sunny Goyal82dfc152023-02-24 16:50:09 -08001166 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001167 public float getReorderBounceScale() {
1168 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -07001169 }
1170
Adam Cohen65086992020-02-19 08:40:49 -08001171 @Override
1172 public int getViewType() {
1173 return DRAGGABLE_ICON;
1174 }
1175
1176 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -07001177 public void getWorkspaceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001178 getIconBounds(mIconSize, bounds);
Adam Cohen65086992020-02-19 08:40:49 -08001179 }
1180
Adam Cohenc77bc452020-05-07 11:55:19 -07001181 public void getSourceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001182 getIconBounds(mIconSize, bounds);
Adam Cohenc77bc452020-05-07 11:55:19 -07001183 }
1184
Adam Cohen65086992020-02-19 08:40:49 -08001185 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -07001186 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001187 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -08001188 setForceHideDot(true);
Sunny Goyal8b9919d2021-04-07 14:45:17 -07001189 return () -> { };
Adam Cohen65086992020-02-19 08:40:49 -08001190 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001191
1192 private void resetIconScale() {
Sunny Goyal52f4c162022-04-23 16:02:03 +00001193 if (mIcon != null) {
1194 mIcon.resetScale();
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001195 }
1196 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001197
1198 private void updateIcon(Drawable newIcon) {
1199 if (mLayoutHorizontal) {
1200 setCompoundDrawablesRelative(newIcon, null, null, null);
1201 } else {
1202 setCompoundDrawables(null, newIcon, null, null);
1203 }
1204 }
Steven Ng707b1182021-09-13 11:54:58 +01001205
1206 private String getAppLabelPluralString(String appName, int notificationCount) {
1207 MessageFormat icuCountFormat = new MessageFormat(
1208 getResources().getString(R.string.dotted_app_label),
1209 Locale.getDefault());
1210 HashMap<String, Object> args = new HashMap();
1211 args.put("app_name", appName);
1212 args.put("count", notificationCount);
1213 return icuCountFormat.format(args);
1214 }
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001215
1216 /**
1217 * Starts a long press action and returns the corresponding pre-drag condition
1218 */
1219 public PreDragCondition startLongPressAction() {
1220 PopupContainerWithArrow popup = PopupContainerWithArrow.showForIcon(this);
1221 return popup != null ? popup.createPreDragCondition(true) : null;
1222 }
1223
1224 /**
1225 * Returns true if the view can show long-press popup
1226 */
1227 public boolean canShowLongPressPopup() {
1228 return getTag() instanceof ItemInfo && ShortcutUtil.supportsShortcuts((ItemInfo) getTag());
1229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230}