blob: bd004e91a5eca1ea9ca6536f24d62a833d22a552 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Federico Baron4eb502a2022-12-14 00:04:53 -080019import static com.android.launcher3.config.FeatureFlags.ENABLE_DOWNLOAD_APP_UX_V2;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070020import static com.android.launcher3.config.FeatureFlags.ENABLE_ICON_LABEL_AUTO_SCALING;
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;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070042import android.text.TextPaint;
Andy Wickham56be73b2022-01-18 09:13:43 -080043import android.text.TextUtils;
Sunny Goyal66dccad2018-03-19 12:00:51 -070044import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080045import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080046import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070047import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070048import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080049import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070050import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080051import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080052import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070053
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000054import androidx.annotation.Nullable;
Hyunyoung Song5809a402021-02-02 01:19:18 -080055import androidx.annotation.UiThread;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080056import androidx.annotation.VisibleForTesting;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070057
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -070058import com.android.launcher3.accessibility.BaseAccessibilityDelegate;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080059import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf34bee82018-12-03 18:11:39 -080060import com.android.launcher3.dot.DotInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070061import com.android.launcher3.dragndrop.DragOptions.PreDragCondition;
Adam Cohen65086992020-02-19 08:40:49 -080062import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070063import com.android.launcher3.folder.FolderIcon;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070064import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080065import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080066import com.android.launcher3.icons.DotRenderer;
Sunny Goyal572aca42021-03-24 15:21:39 -070067import com.android.launcher3.icons.FastBitmapDrawable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080068import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal572aca42021-03-24 15:21:39 -070069import com.android.launcher3.icons.PlaceHolderIconDrawable;
Sunny Goyal79e52fc2021-02-03 10:22:28 -080070import com.android.launcher3.icons.cache.HandlerRunnable;
Sunny Goyale396abf2020-04-06 15:11:17 -070071import com.android.launcher3.model.data.AppInfo;
72import com.android.launcher3.model.data.ItemInfo;
73import com.android.launcher3.model.data.ItemInfoWithIcon;
Sunny Goyale396abf2020-04-06 15:11:17 -070074import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070075import com.android.launcher3.popup.PopupContainerWithArrow;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080076import com.android.launcher3.search.StringMatcherUtility;
77import com.android.launcher3.util.IntArray;
Tony Wickham1e29b042023-03-16 19:58:30 +000078import com.android.launcher3.util.MultiTranslateDelegate;
Samuel Fufa167210a2020-05-12 18:40:11 -070079import com.android.launcher3.util.SafeCloseable;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070080import com.android.launcher3.util.ShortcutUtil;
fbaron1406a0f2023-05-11 08:46:46 -070081import com.android.launcher3.util.Themes;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080082import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070083import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070084
Sunny Goyalc469aad2015-10-01 11:24:23 -070085import java.text.NumberFormat;
Steven Ng707b1182021-09-13 11:54:58 +010086import java.util.HashMap;
87import java.util.Locale;
Sunny Goyalc469aad2015-10-01 11:24:23 -070088
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089/**
90 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
91 * because we want to make the bubble taller than the text and TextView's clip is
92 * too aggressive.
93 */
Sunny Goyalb65d7662021-03-07 15:09:11 -080094public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver,
Hyunyoung Songdb9a53d2021-02-17 00:35:10 -080095 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070096
Sunny Goyaldfaccf62015-05-11 16:30:44 -070097 private static final int DISPLAY_WORKSPACE = 0;
Brandon Dayauonf2ea3992023-06-26 12:13:30 -070098 public static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070099 private static final int DISPLAY_FOLDER = 2;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700100 protected static final int DISPLAY_TASKBAR = 5;
Brandon Dayauon7e376162023-07-10 16:17:26 -0700101 public static final int DISPLAY_SEARCH_RESULT = 6;
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700102 public static final int DISPLAY_SEARCH_RESULT_SMALL = 7;
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700103 public static final int DISPLAY_PREDICTION_ROW = 8;
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700104 public static final int DISPLAY_SEARCH_RESULT_APP_ROW = 9;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700105
Sunny Goyal486adb02021-10-21 11:29:52 -0700106 private static final float MIN_LETTER_SPACING = -0.05f;
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700107 private static final int MAX_SEARCH_LOOP_COUNT = 20;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800108 private static final Character NEW_LINE = '\n';
109 private static final String EMPTY = "";
110 private static final StringMatcherUtility.StringMatcher MATCHER =
111 StringMatcherUtility.StringMatcher.getInstance();
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700112
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700113 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -0700114
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700115 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800116
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800117 private IntArray mBreakPointsIntArray;
118 private CharSequence mLastOriginalText;
119 private CharSequence mLastModifiedText;
120
Tony Wickhamf34bee82018-12-03 18:11:39 -0800121 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
122 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800123 @Override
124 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800125 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800126 }
127
128 @Override
129 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800130 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800131 bubbleTextView.invalidate();
132 }
133 };
134
Sunny Goyaled7a6932018-04-13 11:41:50 -0700135 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
136 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700137 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700138 public Float get(BubbleTextView bubbleTextView) {
139 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700140 }
141
142 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700143 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700144 bubbleTextView.setTextAlpha(alpha);
145 }
146 };
147
Sunny Goyal82dfc152023-02-24 16:50:09 -0800148 private final MultiTranslateDelegate mTranslateDelegate = new MultiTranslateDelegate(this);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800149 private final ActivityContext mActivity;
Sunny Goyalc4c32692021-05-21 14:23:29 -0700150 private FastBitmapDrawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800151 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700152
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800153 protected int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700154
Sunny Goyaled7a6932018-04-13 11:41:50 -0700155 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700156
Kevin Limb3b467c2023-07-21 02:15:34 +0000157 private boolean mLayoutHorizontal;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700158 private final boolean mIsRtl;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700159 private final int mIconSize;
160
161 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyald872a972021-11-24 18:07:04 -0800162 private boolean mHideBadge = false;
163 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700164 private boolean mIsIconVisible = true;
165 @ViewDebug.ExportedProperty(category = "launcher")
166 private int mTextColor;
167 @ViewDebug.ExportedProperty(category = "launcher")
Jon Spivack68862142021-10-28 18:37:19 -0700168 private ColorStateList mTextColorStateList;
169 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700170 private float mTextAlpha = 1;
171
Tony4043b052018-08-17 13:47:39 -0700172 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800173 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800174 private DotRenderer mDotRenderer;
175 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
Sunny Goyal67555872021-05-21 12:56:55 -0700176 protected DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800177 private Animator mDotScaleAnim;
178 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700179
Sunny Goyal4ffec482016-02-09 11:28:52 -0800180 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800181 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800182 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700183 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800184 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700185 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500186
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800187 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700188
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400189 private boolean mEnableIconUpdateAnimation = false;
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700192 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
194
195 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700196 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 }
198
199 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
200 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800201 mActivity = ActivityContext.lookupContext(context);
Pat Manningc49d60b2023-09-13 16:40:27 +0100202 FastBitmapDrawable.setFlagHoverEnabled(enableCursorHoverStates());
Adam Cohen96bb7982014-07-07 11:58:56 -0700203
Adam Cohen96bb7982014-07-07 11:58:56 -0700204 TypedArray a = context.obtainStyledAttributes(attrs,
205 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800206 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700207 mIsRtl = (getResources().getConfiguration().getLayoutDirection()
208 == View.LAYOUT_DIRECTION_RTL);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700209 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700210
Samuel Fufac96fa242019-09-26 23:06:32 -0700211 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700212 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700213 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700214 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700215 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700216 defaultIconSize = grid.iconSizePx;
Thales Lima79456492023-05-23 11:32:22 +0100217 setCenterVertically(grid.iconCenterVertically);
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700218 } else if (mDisplay == DISPLAY_ALL_APPS || mDisplay == DISPLAY_PREDICTION_ROW
219 || mDisplay == DISPLAY_SEARCH_RESULT_APP_ROW) {
Winson1f064272016-07-18 17:18:02 -0700220 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
221 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700222 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700223 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700224 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700225 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700226 defaultIconSize = grid.folderChildIconSizePx;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700227 } else if (mDisplay == DISPLAY_SEARCH_RESULT) {
Brandon Dayauonb1957cd2023-03-14 13:27:30 -0700228 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700229 defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size);
230 } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
231 defaultIconSize = getResources().getDimensionPixelSize(
232 R.dimen.search_row_small_icon_size);
Tony Wickham794fe4f2021-01-11 14:54:23 -0600233 } else if (mDisplay == DISPLAY_TASKBAR) {
234 defaultIconSize = grid.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700235 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700236 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700237 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700238 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700239
Winson1f064272016-07-18 17:18:02 -0700240 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700241
242 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
243 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700244 a.recycle();
245
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800246 mLongPressHelper = new CheckLongPressHelper(this);
247
Tony Wickham8912b042018-11-29 15:28:53 -0800248 mDotParams = new DotRenderer.DrawParams();
249
Sunny Goyal66dccad2018-03-19 12:00:51 -0700250 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700251 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700252 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 }
254
Sunny Goyal66dccad2018-03-19 12:00:51 -0700255 @Override
256 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
257 // Disable marques when not focused to that, so that updating text does not cause relayout.
258 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
259 super.onFocusChanged(focused, direction, previouslyFocusedRect);
260 }
261
Sunny Goyald872a972021-11-24 18:07:04 -0800262 public void setHideBadge(boolean hideBadge) {
263 mHideBadge = hideBadge;
264 }
265
Jon Miranda52549442017-10-26 11:28:06 -0700266 /**
267 * Resets the view so it can be recycled.
268 */
269 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800270 mDotInfo = null;
James O'Leary1557ba32022-05-04 16:49:03 +0000271 mDotParams.dotColor = Color.TRANSPARENT;
272 mDotParams.appColor = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800273 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800274 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800275 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800276 setBackground(null);
Anushree Ganjam2a14b972023-08-30 13:31:10 -0700277 if (Flags.enableTwolineAllapps() || FeatureFlags.ENABLE_TWOLINE_ALLAPPS.get()
hyunyoungs825480f2023-03-24 10:11:15 -0700278 || FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()) {
279 setMaxLines(1);
280 }
Sunny Goyal52f4c162022-04-23 16:02:03 +0000281
282 setTag(null);
283 if (mIconLoadRequest != null) {
284 mIconLoadRequest.cancel();
285 mIconLoadRequest = null;
286 }
Jon Miranda52549442017-10-26 11:28:06 -0700287 }
288
Tony Wickhamf34bee82018-12-03 18:11:39 -0800289 private void cancelDotScaleAnim() {
290 if (mDotScaleAnim != null) {
291 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700292 }
293 }
294
Tony Wickhamf34bee82018-12-03 18:11:39 -0800295 private void animateDotScale(float... dotScales) {
296 cancelDotScaleAnim();
297 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
298 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700299 @Override
300 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800301 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700302 }
303 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800304 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700305 }
306
Hyunyoung Song5809a402021-02-02 01:19:18 -0800307 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700308 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
Tony Wickham175d1522021-08-25 09:18:41 -0700309 applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0);
310 }
311
312 @UiThread
313 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) {
Federico Baron4eb502a2022-12-14 00:04:53 -0800314 applyFromWorkspaceItem(info, null);
Winson Chung5f8afe62013-08-12 16:19:28 -0700315 }
316
Tony Wickham175d1522021-08-25 09:18:41 -0700317 /**
318 * Returns whether the newInfo differs from the current getTag().
319 */
320 public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) {
321 WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo
322 ? (WorkspaceItemInfo) getTag()
323 : null;
324 boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null
325 && newInfo.getTargetComponent() != null
326 && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent());
327 return changedIcons && isShown();
328 }
329
Adam Cohen29794c52019-06-21 12:50:30 -0700330 @Override
331 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -0700332 if (delegate instanceof BaseAccessibilityDelegate) {
Adam Cohen29794c52019-06-21 12:50:30 -0700333 super.setAccessibilityDelegate(delegate);
334 } else {
335 // NO-OP
336 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
337 // delegate incorrectly. There are no cases when we shouldn't be using the
338 // LauncherAccessibilityDelegate for BubbleTextView.
339 }
340 }
341
Hyunyoung Song5809a402021-02-02 01:19:18 -0800342 @UiThread
Federico Baron4eb502a2022-12-14 00:04:53 -0800343 public void applyFromWorkspaceItem(WorkspaceItemInfo info, PreloadIconDrawable icon) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800344 applyIconAndLabel(info);
sfufa@google.comca76de02021-09-24 08:55:48 -0700345 setItemInfo(info);
Federico Baron4eb502a2022-12-14 00:04:53 -0800346 applyLoadingState(icon);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800347 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800348 setDownloadStateContentDescription(info, info.getProgressLevel());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800349 }
350
Hyunyoung Song5809a402021-02-02 01:19:18 -0800351 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700352 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800353 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700354
Sunny Goyal95899162019-03-27 16:03:06 -0700355 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700356 setItemInfo(info);
357
Sunny Goyal34b65272015-03-11 16:56:52 -0700358
359 // Verify high res immediately
360 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800361
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000362 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800363 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700364 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800365 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800366 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal508da152014-08-14 10:53:27 -0700367 }
368
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700369 /**
370 * Apply label and tag using a generic {@link ItemInfoWithIcon}
371 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800372 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700373 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800374 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700375 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700376 setItemInfo(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700377
378 // Verify high res immediately
379 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800380
381 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700382 }
383
Sunny Goyalf0ff70a2022-05-11 07:23:10 -0700384 protected void setItemInfo(ItemInfoWithIcon itemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700385 setTag(itemInfo);
sfufa@google.com0fe464d2021-10-14 14:34:50 -0700386 }
387
Hyunyoung Song5809a402021-02-02 01:19:18 -0800388 @UiThread
Samuel Fufac2820182021-01-22 11:47:25 -0600389 protected void applyIconAndLabel(ItemInfoWithIcon info) {
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800390 int flags = shouldUseTheme() ? FLAG_THEMED : 0;
Sunny Goyald872a972021-11-24 18:07:04 -0800391 if (mHideBadge) {
392 flags |= FLAG_NO_BADGE;
393 }
394 FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags);
James O'Leary1557ba32022-05-04 16:49:03 +0000395 mDotParams.appColor = iconDrawable.getIconColor();
fbaron1406a0f2023-05-11 08:46:46 -0700396 mDotParams.dotColor = Themes.getAttrColor(getContext(), R.attr.notificationDotColor);
Sunny Goyalf4204382016-07-13 10:46:07 -0700397 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000398 applyLabel(info);
399 }
400
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800401 protected boolean shouldUseTheme() {
Brandon Dayauon08cdbc52023-09-14 19:05:09 +0000402 return (mDisplay == DISPLAY_WORKSPACE || mDisplay == DISPLAY_FOLDER
403 || mDisplay == DISPLAY_TASKBAR) && Themes.isThemedIconEnabled(getContext());
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800404 }
405
Brandon Dayauon7d673312023-04-03 12:32:24 -0700406 /**
407 * Only if actual text can be displayed in two line, the {@code true} value will be effective.
408 */
409 protected boolean shouldUseTwoLine() {
Anushree Ganjam2a14b972023-08-30 13:31:10 -0700410 return ((Flags.enableTwolineAllapps() || FeatureFlags.ENABLE_TWOLINE_ALLAPPS.get())
411 && mDisplay == DISPLAY_ALL_APPS)
Brandon Dayauon7d673312023-04-03 12:32:24 -0700412 || (FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()
413 && mDisplay == DISPLAY_SEARCH_RESULT);
414 }
415
Hyunyoung Song5809a402021-02-02 01:19:18 -0800416 @UiThread
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800417 @VisibleForTesting
418 public void applyLabel(ItemInfoWithIcon info) {
419 CharSequence label = info.title;
420 if (label != null) {
421 mLastOriginalText = label;
422 mLastModifiedText = mLastOriginalText;
423 mBreakPointsIntArray = StringMatcherUtility.getListOfBreakpoints(label, MATCHER);
424 setText(label);
425 }
Sunny Goyalf4204382016-07-13 10:46:07 -0700426 if (info.contentDescription != null) {
427 setContentDescription(info.isDisabled()
428 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
429 : info.contentDescription);
430 }
431 }
432
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700433 /** This is used for testing to forcefully set the display. */
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800434 @VisibleForTesting
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700435 public void setDisplay(int display) {
436 mDisplay = display;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800437 }
438
Winson Chung7501adf2015-06-02 11:24:28 -0700439 /**
440 * Overrides the default long press timeout.
441 */
Tony2ca999c2018-09-24 17:24:51 -0400442 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
443 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700444 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800447 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700448 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800449 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800450 }
Sunny Goyal508da152014-08-14 10:53:27 -0700451 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800452
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800453 @Override
454 protected int[] onCreateDrawableState(int extraSpace) {
455 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
456 if (mStayPressed) {
457 mergeDrawableStates(drawableState, STATE_PRESSED);
458 }
459 return drawableState;
460 }
461
Winson Chungb745afb2015-03-02 11:51:23 -0800462 /** Returns the icon for this view. */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700463 public FastBitmapDrawable getIcon() {
Winson Chungb745afb2015-03-02 11:51:23 -0800464 return mIcon;
465 }
466
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700467 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800468 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700469 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700470 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700471 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700472 return false;
473 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700474 if (isLongClickable()) {
475 super.onTouchEvent(event);
476 mLongPressHelper.onTouchEvent(event);
477 // Keep receiving the rest of the events
478 return true;
479 } else {
480 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700481 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800482 }
483
Sunny Goyal44a3b202020-07-09 08:27:06 -0700484 /**
485 * Returns true if the touch down at the provided position be ignored
486 */
487 protected boolean shouldIgnoreTouchDown(float x, float y) {
Tony Wickham42b03952021-08-31 10:41:36 -0700488 if (mDisplay == DISPLAY_TASKBAR) {
489 // Allow touching within padding on taskbar, given icon sizes are smaller.
490 return false;
491 }
Sunny Goyal44a3b202020-07-09 08:27:06 -0700492 return y < getPaddingTop()
493 || x < getPaddingLeft()
494 || y > getHeight() - getPaddingBottom()
495 || x > getWidth() - getPaddingRight();
496 }
497
Michael Jurkaddd62e92011-02-16 17:49:14 -0800498 void setStayPressed(boolean stayPressed) {
499 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800500 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800501 }
502
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700503 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800504 public void onVisibilityAggregated(boolean isVisible) {
505 super.onVisibilityAggregated(isVisible);
506 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800507 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800508 }
509 }
510
Tony Wickham8ac277e2021-05-24 15:47:38 -0700511 public void clearPressedBackground() {
Sunny Goyal508da152014-08-14 10:53:27 -0700512 setPressed(false);
513 setStayPressed(false);
514 }
515
516 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700517 public boolean onKeyUp(int keyCode, KeyEvent event) {
518 // Unlike touch events, keypress event propagate pressed state change immediately,
519 // without waiting for onClickHandler to execute. Disable pressed state changes here
520 // to avoid flickering.
521 mIgnorePressedStateChange = true;
522 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700523 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800524 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700525 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800526 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800527
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700528 @Override
529 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
530 super.onSizeChanged(w, h, oldw, oldh);
531 checkForEllipsis();
532 }
533
534 @Override
535 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
536 super.onTextChanged(text, start, lengthBefore, lengthAfter);
537 checkForEllipsis();
538 }
539
540 private void checkForEllipsis() {
541 if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) {
542 return;
543 }
544 float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
545 if (width <= 0) {
546 return;
547 }
548 setLetterSpacing(0);
549
550 String text = getText().toString();
551 TextPaint paint = getPaint();
552 if (paint.measureText(text) < width) {
553 return;
554 }
555
556 float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING);
557 // Reset the paint value so that the call to TextView does appropriate diff.
558 paint.setLetterSpacing(0);
559 setLetterSpacing(spacing);
560 }
561
562 /**
563 * Find the appropriate text spacing to display the provided text
564 * @param paint the paint used by the text view
565 * @param text the text to display
566 * @param allowedWidthPx available space to render the text
567 * @param minSpacingEm minimum spacing allowed between characters
568 * @return the final textSpacing value
569 *
570 * @see #setLetterSpacing(float)
571 */
572 private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx,
573 float minSpacingEm) {
574 paint.setLetterSpacing(minSpacingEm);
575 if (paint.measureText(text) > allowedWidthPx) {
576 // If there is no result at high limit, we can do anything more
577 return minSpacingEm;
578 }
579
580 float lowLimit = 0;
581 float highLimit = minSpacingEm;
582
583 for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) {
584 float value = (lowLimit + highLimit) / 2;
585 paint.setLetterSpacing(value);
586 if (paint.measureText(text) < allowedWidthPx) {
587 highLimit = value;
588 } else {
589 lowLimit = value;
590 }
591 }
592
593 // At the end error on the higher side
594 return highLimit;
595 }
596
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700597 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800598 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700599 super.onDraw(canvas);
600 }
601
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700603 public void onDraw(Canvas canvas) {
604 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800605 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700606 }
607
Tony Wickham1237df02017-02-24 08:59:36 -0800608 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800609 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700610 *
Tony Wickham1237df02017-02-24 08:59:36 -0800611 * @param canvas The canvas to draw to.
612 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800613 protected void drawDotIfNecessary(Canvas canvas) {
614 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800615 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700616 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
617 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800618 final int scrollX = getScrollX();
619 final int scrollY = getScrollY();
620 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800621 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800622 canvas.translate(-scrollX, -scrollY);
623 }
624 }
625
Jon Miranda9b78e192019-08-30 18:42:01 -0700626 @Override
627 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800628 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800629 return;
630 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800631 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800632
Tony Wickhamf34bee82018-12-03 18:11:39 -0800633 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800634 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800635 } else if (hasDot()) {
636 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800637 }
638 }
639
Brandon Dayauon7e376162023-07-10 16:17:26 -0700640 @VisibleForTesting
641 public boolean getForceHideDot() {
642 return mForceHideDot;
643 }
644
Tony Wickhamf34bee82018-12-03 18:11:39 -0800645 private boolean hasDot() {
646 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800647 }
648
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700649 /**
650 * Get the icon bounds on the view depending on the layout type.
651 */
Tony Wickham1237df02017-02-24 08:59:36 -0800652 public void getIconBounds(Rect outBounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700653 getIconBounds(mIconSize, outBounds);
Tony05d98c22018-07-23 08:01:15 -0700654 }
655
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700656 /**
657 * Get the icon bounds on the view depending on the layout type.
658 */
659 public void getIconBounds(int iconSize, Rect outBounds) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700660 outBounds.set(0, 0, iconSize, iconSize);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700661 if (mLayoutHorizontal) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700662 int top = (getHeight() - iconSize) / 2;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700663 if (mIsRtl) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700664 outBounds.offsetTo(getWidth() - iconSize - getPaddingRight(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700665 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700666 outBounds.offsetTo(getPaddingLeft(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700667 }
668 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700669 outBounds.offset((getWidth() - iconSize) / 2, getPaddingTop());
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672
Jon Miranda228877d2021-02-09 11:05:00 -0500673 /**
Kevin Limb3b467c2023-07-21 02:15:34 +0000674 * Sets whether the layout is horizontal.
675 */
676 public void setLayoutHorizontal(boolean layoutHorizontal) {
677 if (mLayoutHorizontal == layoutHorizontal) {
678 return;
679 }
680
681 mLayoutHorizontal = layoutHorizontal;
682 applyCompoundDrawables(getIconOrTransparentColor());
683 }
684
685 /**
Jon Miranda228877d2021-02-09 11:05:00 -0500686 * Sets whether to vertically center the content.
687 */
688 public void setCenterVertically(boolean centerVertically) {
689 mCenterVertically = centerVertically;
690 }
691
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692 @Override
Winson1f064272016-07-18 17:18:02 -0700693 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
694 if (mCenterVertically) {
695 Paint.FontMetrics fm = getPaint().getFontMetrics();
696 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
697 (int) Math.ceil(fm.bottom - fm.top);
698 int height = MeasureSpec.getSize(heightMeasureSpec);
699 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
700 getPaddingBottom());
701 }
Brandon Dayauon7d673312023-04-03 12:32:24 -0700702 // Only apply two line for all_apps and device search only if necessary.
703 if (shouldUseTwoLine() && (mLastOriginalText != null)) {
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800704 CharSequence modifiedString = modifyTitleToSupportMultiLine(
705 MeasureSpec.getSize(widthMeasureSpec) - getCompoundPaddingLeft()
706 - getCompoundPaddingRight(),
707 mLastOriginalText,
708 getPaint(), mBreakPointsIntArray);
709 if (!TextUtils.equals(modifiedString, mLastModifiedText)) {
710 mLastModifiedText = modifiedString;
711 setText(modifiedString);
712 // if text contains NEW_LINE, set max lines to 2
713 if (TextUtils.indexOf(modifiedString, NEW_LINE) != -1) {
714 setSingleLine(false);
715 setMaxLines(2);
716 } else {
717 setSingleLine(true);
718 setMaxLines(1);
719 }
720 }
721 }
Winson1f064272016-07-18 17:18:02 -0700722 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
723 }
724
725 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700726 public void setTextColor(int color) {
727 mTextColor = color;
Jon Spivack68862142021-10-28 18:37:19 -0700728 mTextColorStateList = null;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700729 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700730 }
731
Adam Cohen96bb7982014-07-07 11:58:56 -0700732 @Override
733 public void setTextColor(ColorStateList colors) {
734 mTextColor = colors.getDefaultColor();
Jon Spivack68862142021-10-28 18:37:19 -0700735 mTextColorStateList = colors;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700736 if (Float.compare(mTextAlpha, 1) == 0) {
737 super.setTextColor(colors);
738 } else {
739 super.setTextColor(getModifiedColor());
740 }
Adam Cohen477828c2013-09-20 12:05:49 -0700741 }
742
Tony7308cde2017-06-27 22:38:33 -0700743 public boolean shouldTextBeVisible() {
744 // Text should be visible everywhere but the hotseat.
745 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
746 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700747 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
748 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700749 }
750
Winson Chung5f8afe62013-08-12 16:19:28 -0700751 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700752 setTextAlpha(visible ? 1 : 0);
753 }
754
755 private void setTextAlpha(float alpha) {
756 mTextAlpha = alpha;
Jon Spivack68862142021-10-28 18:37:19 -0700757 if (mTextColorStateList != null) {
758 setTextColor(mTextColorStateList);
759 } else {
760 super.setTextColor(getModifiedColor());
761 }
Sunny Goyaled7a6932018-04-13 11:41:50 -0700762 }
763
764 private int getModifiedColor() {
765 if (mTextAlpha == 0) {
766 // Special case to prevent text shadows in high contrast mode
767 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700768 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800769 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700770 }
771
772 /**
773 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700774 *
Tony8f402802017-06-16 17:24:54 -0700775 * @param fadeIn Whether the text should fade in or fade out.
776 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700777 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700778 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700779 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700780 }
781
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800782 /**
783 * Generate a new string that will support two line text depending on the current string.
784 * This method calculates the limited width of a text view and creates a string to fit as
785 * many words as it can until the limit is reached. Once the limit is reached, we decide to
786 * either return the original title or continue on a new line. How to get the new string is by
787 * iterating through the list of break points and determining if the strings between the break
788 * points can fit within the line it is in.
789 * Example assuming each character takes up one spot:
790 * title = "Battery Stats", breakpoint = [6], stringPtr = 0, limitedWidth = 7
791 * We get the current word -> from sublist(0, breakpoint[i]+1) so sublist (0,7) -> Battery,
792 * now stringPtr = 7 then from sublist(7) the current string is " Stats" and the runningWidth
793 * at this point exceeds limitedWidth and so we put " Stats" onto the next line (after checking
794 * if the first char is a SPACE, we trim to append "Stats". So resulting string would be
795 * "Battery\nStats"
796 */
797 public static CharSequence modifyTitleToSupportMultiLine(int limitedWidth, CharSequence title,
798 TextPaint paint, IntArray breakPoints) {
799 // current title is less than the width allowed so we can just skip
800 if (title == null || paint.measureText(title, 0, title.length()) <= limitedWidth) {
801 return title;
802 }
803 float currentWordWidth, runningWidth = 0;
804 CharSequence currentWord;
805 StringBuilder newString = new StringBuilder();
806 int stringPtr = 0;
807 for (int i = 0; i < breakPoints.size()+1; i++) {
808 if (i < breakPoints.size()) {
809 currentWord = title.subSequence(stringPtr, breakPoints.get(i)+1);
810 } else {
811 // last word from recent breakpoint until the end of the string
812 currentWord = title.subSequence(stringPtr, title.length());
813 }
814 currentWordWidth = paint.measureText(currentWord,0, currentWord.length());
815 runningWidth += currentWordWidth;
816 if (runningWidth <= limitedWidth) {
817 newString.append(currentWord);
818 } else {
819 // there is no more space
820 if (i == 0) {
821 // if the first words exceeds width, just return as the first line will ellipse
822 return title;
823 } else {
824 // If putting word onto a new line, make sure there is no space or new line
825 // character in the beginning of the current word and just put in the rest of
826 // the characters.
827 CharSequence lastCharacters = title.subSequence(stringPtr, title.length());
828 int beginningLetterType =
829 Character.getType(Character.codePointAt(lastCharacters,0));
830 if (beginningLetterType == Character.SPACE_SEPARATOR
831 || beginningLetterType == Character.LINE_SEPARATOR) {
832 lastCharacters = lastCharacters.length() > 1
833 ? lastCharacters.subSequence(1, lastCharacters.length())
834 : EMPTY;
835 }
836 newString.append(NEW_LINE).append(lastCharacters);
837 return newString.toString();
838 }
839 }
840 if (i >= breakPoints.size()) {
841 // no need to look forward into the string if we've already finished processing
842 break;
843 }
844 stringPtr = breakPoints.get(i)+1;
845 }
846 return newString.toString();
847 }
848
Winson Chungaffd7b42010-08-20 15:11:56 -0700849 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800850 public void cancelLongPress() {
851 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800852 mLongPressHelper.cancelLongPress();
853 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500854
Samuel Fufac2820182021-01-22 11:47:25 -0600855 /**
856 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000857 *
858 * If this app is installing, the progress bar will be updated with the installation progress.
859 * If this app is installed and downloading incrementally, the progress bar will be updated
860 * with the total download progress.
861 */
Federico Baron4eb502a2022-12-14 00:04:53 -0800862 public void applyLoadingState(PreloadIconDrawable icon) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800863 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700864 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Federico Baron4eb502a2022-12-14 00:04:53 -0800865 if ((info.runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0
866 || info.hasPromiseIconUi()
867 || (info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0
868 || (ENABLE_DOWNLOAD_APP_UX_V2.get() && icon != null)) {
Federico Barondb599b22023-03-09 14:44:41 -0800869 updateProgressBarUi(info.getProgressLevel() == 100 ? icon : null);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700870 }
871 }
872 }
873
Federico Baron4eb502a2022-12-14 00:04:53 -0800874 private void updateProgressBarUi(PreloadIconDrawable oldIcon) {
875 FastBitmapDrawable originalIcon = mIcon;
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800876 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Federico Baron4eb502a2022-12-14 00:04:53 -0800877 if (preloadDrawable != null && oldIcon != null) {
878 preloadDrawable.maybePerformFinishedAnimation(oldIcon, () -> setIcon(originalIcon));
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000879 }
880 }
881
882 /** Applies the given progress level to the this icon's progress bar. */
883 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800884 public PreloadIconDrawable applyProgressLevel() {
885 if (!(getTag() instanceof ItemInfoWithIcon)) {
886 return null;
887 }
888
889 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
890 int progressLevel = info.getProgressLevel();
891 if (progressLevel >= 100) {
892 setContentDescription(info.contentDescription != null
893 ? info.contentDescription : "");
894 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800895 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800896 } else {
897 setContentDescription(getContext()
898 .getString(R.string.app_waiting_download_title, info.title));
899 }
900 if (mIcon != null) {
901 PreloadIconDrawable preloadIconDrawable;
902 if (mIcon instanceof PreloadIconDrawable) {
903 preloadIconDrawable = (PreloadIconDrawable) mIcon;
904 preloadIconDrawable.setLevel(progressLevel);
fbaron6664d8c2023-04-04 13:02:59 -0700905 preloadIconDrawable.setIsDisabled(ENABLE_DOWNLOAD_APP_UX_V2.get()
906 ? info.getProgressLevel() == 0
907 : !info.isAppStartable());
Mario Bertschler230612f2017-09-27 17:05:21 -0700908 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800909 preloadIconDrawable = makePreloadIcon();
910 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700911 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800912 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400913 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700914 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400915 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700916
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800917 /**
918 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
919 * object.
920 */
921 @Nullable
922 public PreloadIconDrawable makePreloadIcon() {
923 if (!(getTag() instanceof ItemInfoWithIcon)) {
924 return null;
925 }
926
927 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
928 int progressLevel = info.getProgressLevel();
929 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
930
931 preloadDrawable.setLevel(progressLevel);
fbaron6664d8c2023-04-04 13:02:59 -0700932 preloadDrawable.setIsDisabled(ENABLE_DOWNLOAD_APP_UX_V2.get()
933 ? info.getProgressLevel() == 0
934 : !info.isAppStartable());
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800935 return preloadDrawable;
936 }
937
Tony Wickhamf34bee82018-12-03 18:11:39 -0800938 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800939 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800940 boolean wasDotted = mDotInfo != null;
941 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
942 boolean isDotted = mDotInfo != null;
943 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700944 if (mDisplay == DISPLAY_ALL_APPS) {
945 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
946 } else {
947 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
948 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800949 if (wasDotted || isDotted) {
950 // Animate when a dot is first added or when it is removed.
951 if (animate && (wasDotted ^ isDotted) && isShown()) {
952 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800953 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800954 cancelDotScaleAnim();
955 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800956 invalidate();
957 }
958 }
Andy Wickham56be73b2022-01-18 09:13:43 -0800959 if (!TextUtils.isEmpty(itemInfo.contentDescription)) {
vadimtda6dad52019-05-21 13:24:23 -0700960 if (itemInfo.isDisabled()) {
961 setContentDescription(getContext().getString(R.string.disabled_app_label,
962 itemInfo.contentDescription));
963 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800964 int count = mDotInfo.getNotificationCount();
Steven Ng707b1182021-09-13 11:54:58 +0100965 setContentDescription(
966 getAppLabelPluralString(itemInfo.contentDescription.toString(), count));
Tony Wickham305e9202018-01-23 17:46:47 -0800967 } else {
968 setContentDescription(itemInfo.contentDescription);
969 }
970 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800971 }
972 }
973
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800974 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
975 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
976 != 0) {
977 String percentageString = NumberFormat.getPercentInstance()
978 .format(progressLevel * 0.01);
Federico Baron4eb502a2022-12-14 00:04:53 -0800979 if ((info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800980 setContentDescription(getContext()
981 .getString(
982 R.string.app_installing_title, info.title, percentageString));
983 } else if ((info.runtimeStatusFlags
Federico Baron4eb502a2022-12-14 00:04:53 -0800984 & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800985 setContentDescription(getContext()
986 .getString(
987 R.string.app_downloading_title, info.title, percentageString));
988 }
989 }
990 }
991
Winson Chungb745afb2015-03-02 11:51:23 -0800992 /**
993 * Sets the icon for this view based on the layout direction.
994 */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700995 protected void setIcon(FastBitmapDrawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700996 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700997 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700998 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700999 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -08001000 if (mIcon != null) {
1001 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
1002 }
Tony17b7f9b2017-05-23 12:19:09 -07001003 }
1004
Jon Miranda9b78e192019-08-30 18:42:01 -07001005 @Override
Tony17b7f9b2017-05-23 12:19:09 -07001006 public void setIconVisible(boolean visible) {
1007 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001008 if (!mIsIconVisible) {
1009 resetIconScale();
1010 }
Kevin Limb3b467c2023-07-21 02:15:34 +00001011 Drawable icon = getIconOrTransparentColor();
Tony17b7f9b2017-05-23 12:19:09 -07001012 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -07001013 }
1014
Kevin Limb3b467c2023-07-21 02:15:34 +00001015 private Drawable getIconOrTransparentColor() {
1016 return mIsIconVisible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
1017 }
1018
Tony Wickham1e29b042023-03-16 19:58:30 +00001019 /** Sets the icon visual state to disabled or not. */
1020 public void setIconDisabled(boolean isDisabled) {
1021 if (mIcon != null) {
1022 mIcon.setIsDisabled(isDisabled);
1023 }
1024 }
1025
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001026 protected boolean iconUpdateAnimationEnabled() {
1027 return mEnableIconUpdateAnimation;
1028 }
1029
Tony Wickham377ed3f2016-07-20 15:21:04 -07001030 protected void applyCompoundDrawables(Drawable icon) {
Jon Mirandafe749972022-03-15 10:02:39 -07001031 if (icon == null) {
1032 // Icon can be null when we use the BubbleTextView for text only.
1033 return;
1034 }
1035
Sunny Goyal66dccad2018-03-19 12:00:51 -07001036 // If we had already set an icon before, disable relayout as the icon size is the
1037 // same as before.
1038 mDisableRelayout = mIcon != null;
1039
1040 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001041
1042 updateIcon(icon);
1043
1044 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -04001045 if (mIcon != null
1046 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001047 && iconUpdateAnimationEnabled()) {
Steven Nge92bc552021-02-10 17:10:15 +00001048 ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon);
Winson Chungb745afb2015-03-02 11:51:23 -08001049 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001050
Sunny Goyal66dccad2018-03-19 12:00:51 -07001051 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -08001052 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001053
Sunny Goyal69b75642015-05-15 17:00:24 -07001054 @Override
1055 public void requestLayout() {
1056 if (!mDisableRelayout) {
1057 super.requestLayout();
1058 }
1059 }
1060
Sunny Goyal34b65272015-03-11 16:56:52 -07001061 /**
1062 * Applies the item info if it is same as what the view is pointing to currently.
1063 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001064 @Override
1065 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001066 if (getTag() == info) {
1067 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -07001068 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001069 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -07001070
Jon Miranda7f522a22017-07-28 11:56:47 -07001071 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -07001072 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -07001073
Sunny Goyal34b65272015-03-11 16:56:52 -07001074 if (info instanceof AppInfo) {
1075 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -07001076 } else if (info instanceof WorkspaceItemInfo) {
1077 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001078 mActivity.invalidateParent(info);
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001079 } else if (info != null) {
1080 applyFromItemInfoWithIcon(info);
Sunny Goyal34b65272015-03-11 16:56:52 -07001081 }
Winsonc0880492015-08-21 11:16:27 -07001082
Sunny Goyal69b75642015-05-15 17:00:24 -07001083 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001084 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -07001085 }
1086 }
1087
1088 /**
1089 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
1090 */
1091 public void verifyHighRes() {
1092 if (mIconLoadRequest != null) {
1093 mIconLoadRequest.cancel();
1094 mIconLoadRequest = null;
1095 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001096 if (getTag() instanceof ItemInfoWithIcon) {
1097 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -07001098 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001099 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -07001100 .updateIconInBackground(BubbleTextView.this, info);
1101 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001102 }
1103 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -07001104
Jon Miranda47170112017-03-03 14:57:14 -08001105 public int getIconSize() {
1106 return mIconSize;
1107 }
Adam Cohen65086992020-02-19 08:40:49 -08001108
Brandon Dayauond3755f42022-08-29 11:48:27 -07001109 public boolean isDisplaySearchResult() {
Brandon Dayauon47000ae2023-09-11 15:28:11 -07001110 return mDisplay == DISPLAY_SEARCH_RESULT
1111 || mDisplay == DISPLAY_SEARCH_RESULT_SMALL
1112 || mDisplay == DISPLAY_SEARCH_RESULT_APP_ROW;
1113 }
1114
1115 public int getIconDisplay() {
1116 return mDisplay;
Brandon Dayauond3755f42022-08-29 11:48:27 -07001117 }
1118
Sunny Goyal82dfc152023-02-24 16:50:09 -08001119 @Override
1120 public MultiTranslateDelegate getTranslateDelegate() {
1121 return mTranslateDelegate;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001122 }
1123
1124 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001125 public void setReorderBounceScale(float scale) {
1126 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -07001127 super.setScaleX(scale);
1128 super.setScaleY(scale);
1129 }
1130
Sunny Goyal82dfc152023-02-24 16:50:09 -08001131 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001132 public float getReorderBounceScale() {
1133 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -07001134 }
1135
Adam Cohen65086992020-02-19 08:40:49 -08001136 @Override
1137 public int getViewType() {
1138 return DRAGGABLE_ICON;
1139 }
1140
1141 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -07001142 public void getWorkspaceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001143 getIconBounds(mIconSize, bounds);
Adam Cohen65086992020-02-19 08:40:49 -08001144 }
1145
Adam Cohenc77bc452020-05-07 11:55:19 -07001146 public void getSourceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001147 getIconBounds(mIconSize, bounds);
Adam Cohenc77bc452020-05-07 11:55:19 -07001148 }
1149
Adam Cohen65086992020-02-19 08:40:49 -08001150 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -07001151 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001152 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -08001153 setForceHideDot(true);
Sunny Goyal8b9919d2021-04-07 14:45:17 -07001154 return () -> { };
Adam Cohen65086992020-02-19 08:40:49 -08001155 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001156
1157 private void resetIconScale() {
Sunny Goyal52f4c162022-04-23 16:02:03 +00001158 if (mIcon != null) {
1159 mIcon.resetScale();
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001160 }
1161 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001162
1163 private void updateIcon(Drawable newIcon) {
1164 if (mLayoutHorizontal) {
1165 setCompoundDrawablesRelative(newIcon, null, null, null);
1166 } else {
1167 setCompoundDrawables(null, newIcon, null, null);
1168 }
1169 }
Steven Ng707b1182021-09-13 11:54:58 +01001170
1171 private String getAppLabelPluralString(String appName, int notificationCount) {
1172 MessageFormat icuCountFormat = new MessageFormat(
1173 getResources().getString(R.string.dotted_app_label),
1174 Locale.getDefault());
1175 HashMap<String, Object> args = new HashMap();
1176 args.put("app_name", appName);
1177 args.put("count", notificationCount);
1178 return icuCountFormat.format(args);
1179 }
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001180
1181 /**
1182 * Starts a long press action and returns the corresponding pre-drag condition
1183 */
1184 public PreDragCondition startLongPressAction() {
1185 PopupContainerWithArrow popup = PopupContainerWithArrow.showForIcon(this);
1186 return popup != null ? popup.createPreDragCondition(true) : null;
1187 }
1188
1189 /**
1190 * Returns true if the view can show long-press popup
1191 */
1192 public boolean canShowLongPressPopup() {
1193 return getTag() instanceof ItemInfo && ShortcutUtil.supportsShortcuts((ItemInfo) getTag());
1194 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195}