blob: 05d434e47926c59ff9b94c28d687c9576f5ccbd4 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Federico Baron4eb502a2022-12-14 00:04:53 -080019import static com.android.launcher3.config.FeatureFlags.ENABLE_DOWNLOAD_APP_UX_V2;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070020import static com.android.launcher3.config.FeatureFlags.ENABLE_ICON_LABEL_AUTO_SCALING;
Sunny Goyal14168432019-10-24 15:59:49 -070021import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon;
Sunny Goyald872a972021-11-24 18:07:04 -080022import static com.android.launcher3.icons.BitmapInfo.FLAG_NO_BADGE;
23import static com.android.launcher3.icons.BitmapInfo.FLAG_THEMED;
Sunny Goyal066ace12018-11-05 11:08:31 -080024import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
Federico Baron4eb502a2022-12-14 00:04:53 -080025import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE;
26import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal066ace12018-11-05 11:08:31 -080027
Tony4043b052018-08-17 13:47:39 -070028import android.animation.Animator;
29import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080030import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070032import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070033import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070035import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070036import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080037import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070038import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.graphics.drawable.Drawable;
Steven Ng707b1182021-09-13 11:54:58 +010040import android.icu.text.MessageFormat;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070041import android.text.TextPaint;
Andy Wickham56be73b2022-01-18 09:13:43 -080042import android.text.TextUtils;
Sunny Goyal66dccad2018-03-19 12:00:51 -070043import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080044import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080045import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070046import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070047import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080048import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070049import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080050import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080051import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070052
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000053import androidx.annotation.Nullable;
Hyunyoung Song5809a402021-02-02 01:19:18 -080054import androidx.annotation.UiThread;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080055import androidx.annotation.VisibleForTesting;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070056
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -070057import com.android.launcher3.accessibility.BaseAccessibilityDelegate;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080058import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf34bee82018-12-03 18:11:39 -080059import com.android.launcher3.dot.DotInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070060import com.android.launcher3.dragndrop.DragOptions.PreDragCondition;
Adam Cohen65086992020-02-19 08:40:49 -080061import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070062import com.android.launcher3.folder.FolderIcon;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070063import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080064import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080065import com.android.launcher3.icons.DotRenderer;
Sunny Goyal572aca42021-03-24 15:21:39 -070066import com.android.launcher3.icons.FastBitmapDrawable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080067import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal572aca42021-03-24 15:21:39 -070068import com.android.launcher3.icons.PlaceHolderIconDrawable;
Sunny Goyal79e52fc2021-02-03 10:22:28 -080069import com.android.launcher3.icons.cache.HandlerRunnable;
Sunny Goyale396abf2020-04-06 15:11:17 -070070import com.android.launcher3.model.data.AppInfo;
71import com.android.launcher3.model.data.ItemInfo;
72import com.android.launcher3.model.data.ItemInfoWithIcon;
Sunny Goyale396abf2020-04-06 15:11:17 -070073import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070074import com.android.launcher3.popup.PopupContainerWithArrow;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080075import com.android.launcher3.search.StringMatcherUtility;
76import com.android.launcher3.util.IntArray;
Tony Wickham1e29b042023-03-16 19:58:30 +000077import com.android.launcher3.util.MultiTranslateDelegate;
Samuel Fufa167210a2020-05-12 18:40:11 -070078import com.android.launcher3.util.SafeCloseable;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070079import com.android.launcher3.util.ShortcutUtil;
fbaron1406a0f2023-05-11 08:46:46 -070080import com.android.launcher3.util.Themes;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080081import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070082import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070083
Sunny Goyalc469aad2015-10-01 11:24:23 -070084import java.text.NumberFormat;
Steven Ng707b1182021-09-13 11:54:58 +010085import java.util.HashMap;
86import java.util.Locale;
Sunny Goyalc469aad2015-10-01 11:24:23 -070087
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088/**
89 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
90 * because we want to make the bubble taller than the text and TextView's clip is
91 * too aggressive.
92 */
Sunny Goyalb65d7662021-03-07 15:09:11 -080093public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver,
Hyunyoung Songdb9a53d2021-02-17 00:35:10 -080094 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070095
Sunny Goyaldfaccf62015-05-11 16:30:44 -070096 private static final int DISPLAY_WORKSPACE = 0;
97 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070098 private static final int DISPLAY_FOLDER = 2;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -070099 protected static final int DISPLAY_TASKBAR = 5;
100 private static final int DISPLAY_SEARCH_RESULT = 6;
101 private static final int DISPLAY_SEARCH_RESULT_SMALL = 7;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700102
Sunny Goyal486adb02021-10-21 11:29:52 -0700103 private static final float MIN_LETTER_SPACING = -0.05f;
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700104 private static final int MAX_SEARCH_LOOP_COUNT = 20;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800105 private static final Character NEW_LINE = '\n';
106 private static final String EMPTY = "";
107 private static final StringMatcherUtility.StringMatcher MATCHER =
108 StringMatcherUtility.StringMatcher.getInstance();
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700109
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700110 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -0700111
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700112 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800113
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800114 private IntArray mBreakPointsIntArray;
115 private CharSequence mLastOriginalText;
116 private CharSequence mLastModifiedText;
117
Tony Wickhamf34bee82018-12-03 18:11:39 -0800118 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
119 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800120 @Override
121 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800122 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800123 }
124
125 @Override
126 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800127 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800128 bubbleTextView.invalidate();
129 }
130 };
131
Sunny Goyaled7a6932018-04-13 11:41:50 -0700132 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
133 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700134 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700135 public Float get(BubbleTextView bubbleTextView) {
136 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700137 }
138
139 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700140 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700141 bubbleTextView.setTextAlpha(alpha);
142 }
143 };
144
Sunny Goyal82dfc152023-02-24 16:50:09 -0800145 private final MultiTranslateDelegate mTranslateDelegate = new MultiTranslateDelegate(this);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800146 private final ActivityContext mActivity;
Sunny Goyalc4c32692021-05-21 14:23:29 -0700147 private FastBitmapDrawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800148 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700149
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800150 protected int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700151
Sunny Goyaled7a6932018-04-13 11:41:50 -0700152 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700153
154 private final boolean mLayoutHorizontal;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700155 private final boolean mIsRtl;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700156 private final int mIconSize;
157
158 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyald872a972021-11-24 18:07:04 -0800159 private boolean mHideBadge = false;
160 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700161 private boolean mIsIconVisible = true;
162 @ViewDebug.ExportedProperty(category = "launcher")
163 private int mTextColor;
164 @ViewDebug.ExportedProperty(category = "launcher")
Jon Spivack68862142021-10-28 18:37:19 -0700165 private ColorStateList mTextColorStateList;
166 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700167 private float mTextAlpha = 1;
168
Tony4043b052018-08-17 13:47:39 -0700169 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800170 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800171 private DotRenderer mDotRenderer;
172 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
Sunny Goyal67555872021-05-21 12:56:55 -0700173 protected DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800174 private Animator mDotScaleAnim;
175 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700176
Sunny Goyal4ffec482016-02-09 11:28:52 -0800177 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800178 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800179 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700180 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800181 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700182 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500183
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800184 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700185
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400186 private boolean mEnableIconUpdateAnimation = false;
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700189 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191
192 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700193 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 }
195
196 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
197 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800198 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700199
Adam Cohen96bb7982014-07-07 11:58:56 -0700200 TypedArray a = context.obtainStyledAttributes(attrs,
201 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800202 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700203 mIsRtl = (getResources().getConfiguration().getLayoutDirection()
204 == View.LAYOUT_DIRECTION_RTL);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700205 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700206
Samuel Fufac96fa242019-09-26 23:06:32 -0700207 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700208 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700209 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700210 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700211 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700212 defaultIconSize = grid.iconSizePx;
Thales Lima79456492023-05-23 11:32:22 +0100213 setCenterVertically(grid.iconCenterVertically);
Samuel Fufac96fa242019-09-26 23:06:32 -0700214 } else if (mDisplay == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700215 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
216 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700217 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700218 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700219 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700220 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700221 defaultIconSize = grid.folderChildIconSizePx;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700222 } else if (mDisplay == DISPLAY_SEARCH_RESULT) {
Brandon Dayauonb1957cd2023-03-14 13:27:30 -0700223 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700224 defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size);
225 } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
226 defaultIconSize = getResources().getDimensionPixelSize(
227 R.dimen.search_row_small_icon_size);
Tony Wickham794fe4f2021-01-11 14:54:23 -0600228 } else if (mDisplay == DISPLAY_TASKBAR) {
229 defaultIconSize = grid.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700230 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700231 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700232 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700233 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700234
Winson1f064272016-07-18 17:18:02 -0700235 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700236
237 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
238 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700239 a.recycle();
240
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800241 mLongPressHelper = new CheckLongPressHelper(this);
242
Tony Wickham8912b042018-11-29 15:28:53 -0800243 mDotParams = new DotRenderer.DrawParams();
244
Sunny Goyal66dccad2018-03-19 12:00:51 -0700245 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700246 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700247 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 }
249
Sunny Goyal66dccad2018-03-19 12:00:51 -0700250 @Override
251 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
252 // Disable marques when not focused to that, so that updating text does not cause relayout.
253 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
254 super.onFocusChanged(focused, direction, previouslyFocusedRect);
255 }
256
Sunny Goyald872a972021-11-24 18:07:04 -0800257 public void setHideBadge(boolean hideBadge) {
258 mHideBadge = hideBadge;
259 }
260
Jon Miranda52549442017-10-26 11:28:06 -0700261 /**
262 * Resets the view so it can be recycled.
263 */
264 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800265 mDotInfo = null;
James O'Leary1557ba32022-05-04 16:49:03 +0000266 mDotParams.dotColor = Color.TRANSPARENT;
267 mDotParams.appColor = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800268 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800269 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800270 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800271 setBackground(null);
hyunyoungs825480f2023-03-24 10:11:15 -0700272 if (FeatureFlags.ENABLE_TWOLINE_ALLAPPS.get()
273 || FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()) {
274 setMaxLines(1);
275 }
Sunny Goyal52f4c162022-04-23 16:02:03 +0000276
277 setTag(null);
278 if (mIconLoadRequest != null) {
279 mIconLoadRequest.cancel();
280 mIconLoadRequest = null;
281 }
Jon Miranda52549442017-10-26 11:28:06 -0700282 }
283
Tony Wickhamf34bee82018-12-03 18:11:39 -0800284 private void cancelDotScaleAnim() {
285 if (mDotScaleAnim != null) {
286 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700287 }
288 }
289
Tony Wickhamf34bee82018-12-03 18:11:39 -0800290 private void animateDotScale(float... dotScales) {
291 cancelDotScaleAnim();
292 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
293 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700294 @Override
295 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800296 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700297 }
298 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800299 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700300 }
301
Hyunyoung Song5809a402021-02-02 01:19:18 -0800302 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700303 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
Tony Wickham175d1522021-08-25 09:18:41 -0700304 applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0);
305 }
306
307 @UiThread
308 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) {
Federico Baron4eb502a2022-12-14 00:04:53 -0800309 applyFromWorkspaceItem(info, null);
Winson Chung5f8afe62013-08-12 16:19:28 -0700310 }
311
Tony Wickham175d1522021-08-25 09:18:41 -0700312 /**
313 * Returns whether the newInfo differs from the current getTag().
314 */
315 public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) {
316 WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo
317 ? (WorkspaceItemInfo) getTag()
318 : null;
319 boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null
320 && newInfo.getTargetComponent() != null
321 && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent());
322 return changedIcons && isShown();
323 }
324
Adam Cohen29794c52019-06-21 12:50:30 -0700325 @Override
326 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -0700327 if (delegate instanceof BaseAccessibilityDelegate) {
Adam Cohen29794c52019-06-21 12:50:30 -0700328 super.setAccessibilityDelegate(delegate);
329 } else {
330 // NO-OP
331 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
332 // delegate incorrectly. There are no cases when we shouldn't be using the
333 // LauncherAccessibilityDelegate for BubbleTextView.
334 }
335 }
336
Hyunyoung Song5809a402021-02-02 01:19:18 -0800337 @UiThread
Federico Baron4eb502a2022-12-14 00:04:53 -0800338 public void applyFromWorkspaceItem(WorkspaceItemInfo info, PreloadIconDrawable icon) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800339 applyIconAndLabel(info);
sfufa@google.comca76de02021-09-24 08:55:48 -0700340 setItemInfo(info);
Federico Baron4eb502a2022-12-14 00:04:53 -0800341 applyLoadingState(icon);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800342 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800343 setDownloadStateContentDescription(info, info.getProgressLevel());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800344 }
345
Hyunyoung Song5809a402021-02-02 01:19:18 -0800346 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700347 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800348 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700349
Sunny Goyal95899162019-03-27 16:03:06 -0700350 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700351 setItemInfo(info);
352
Sunny Goyal34b65272015-03-11 16:56:52 -0700353
354 // Verify high res immediately
355 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800356
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000357 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800358 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700359 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800360 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800361 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal508da152014-08-14 10:53:27 -0700362 }
363
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700364 /**
365 * Apply label and tag using a generic {@link ItemInfoWithIcon}
366 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800367 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700368 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800369 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700370 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700371 setItemInfo(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700372
373 // Verify high res immediately
374 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800375
376 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700377 }
378
Sunny Goyalf0ff70a2022-05-11 07:23:10 -0700379 protected void setItemInfo(ItemInfoWithIcon itemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700380 setTag(itemInfo);
sfufa@google.com0fe464d2021-10-14 14:34:50 -0700381 }
382
Hyunyoung Song5809a402021-02-02 01:19:18 -0800383 @UiThread
Samuel Fufac2820182021-01-22 11:47:25 -0600384 protected void applyIconAndLabel(ItemInfoWithIcon info) {
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800385 int flags = shouldUseTheme() ? FLAG_THEMED : 0;
Sunny Goyald872a972021-11-24 18:07:04 -0800386 if (mHideBadge) {
387 flags |= FLAG_NO_BADGE;
388 }
389 FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags);
James O'Leary1557ba32022-05-04 16:49:03 +0000390 mDotParams.appColor = iconDrawable.getIconColor();
fbaron1406a0f2023-05-11 08:46:46 -0700391 mDotParams.dotColor = Themes.getAttrColor(getContext(), R.attr.notificationDotColor);
Sunny Goyalf4204382016-07-13 10:46:07 -0700392 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000393 applyLabel(info);
394 }
395
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800396 protected boolean shouldUseTheme() {
397 return mDisplay == DISPLAY_WORKSPACE || mDisplay == DISPLAY_FOLDER
398 || mDisplay == DISPLAY_TASKBAR;
399 }
400
Brandon Dayauon7d673312023-04-03 12:32:24 -0700401 /**
402 * Only if actual text can be displayed in two line, the {@code true} value will be effective.
403 */
404 protected boolean shouldUseTwoLine() {
405 return (FeatureFlags.ENABLE_TWOLINE_ALLAPPS.get() && mDisplay == DISPLAY_ALL_APPS)
406 || (FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()
407 && mDisplay == DISPLAY_SEARCH_RESULT);
408 }
409
Hyunyoung Song5809a402021-02-02 01:19:18 -0800410 @UiThread
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800411 @VisibleForTesting
412 public void applyLabel(ItemInfoWithIcon info) {
413 CharSequence label = info.title;
414 if (label != null) {
415 mLastOriginalText = label;
416 mLastModifiedText = mLastOriginalText;
417 mBreakPointsIntArray = StringMatcherUtility.getListOfBreakpoints(label, MATCHER);
418 setText(label);
419 }
Sunny Goyalf4204382016-07-13 10:46:07 -0700420 if (info.contentDescription != null) {
421 setContentDescription(info.isDisabled()
422 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
423 : info.contentDescription);
424 }
425 }
426
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800427 /** This is used for testing to forcefully set the display to ALL_APPS */
428 @VisibleForTesting
429 public void setDisplayAllApps() {
430 mDisplay = DISPLAY_ALL_APPS;
431 }
432
Winson Chung7501adf2015-06-02 11:24:28 -0700433 /**
434 * Overrides the default long press timeout.
435 */
Tony2ca999c2018-09-24 17:24:51 -0400436 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
437 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700438 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800441 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700442 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800443 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800444 }
Sunny Goyal508da152014-08-14 10:53:27 -0700445 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800446
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800447 @Override
448 protected int[] onCreateDrawableState(int extraSpace) {
449 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
450 if (mStayPressed) {
451 mergeDrawableStates(drawableState, STATE_PRESSED);
452 }
453 return drawableState;
454 }
455
Winson Chungb745afb2015-03-02 11:51:23 -0800456 /** Returns the icon for this view. */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700457 public FastBitmapDrawable getIcon() {
Winson Chungb745afb2015-03-02 11:51:23 -0800458 return mIcon;
459 }
460
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700461 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800462 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700463 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700464 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700465 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700466 return false;
467 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700468 if (isLongClickable()) {
469 super.onTouchEvent(event);
470 mLongPressHelper.onTouchEvent(event);
471 // Keep receiving the rest of the events
472 return true;
473 } else {
474 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700475 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800476 }
477
Sunny Goyal44a3b202020-07-09 08:27:06 -0700478 /**
479 * Returns true if the touch down at the provided position be ignored
480 */
481 protected boolean shouldIgnoreTouchDown(float x, float y) {
Tony Wickham42b03952021-08-31 10:41:36 -0700482 if (mDisplay == DISPLAY_TASKBAR) {
483 // Allow touching within padding on taskbar, given icon sizes are smaller.
484 return false;
485 }
Sunny Goyal44a3b202020-07-09 08:27:06 -0700486 return y < getPaddingTop()
487 || x < getPaddingLeft()
488 || y > getHeight() - getPaddingBottom()
489 || x > getWidth() - getPaddingRight();
490 }
491
Michael Jurkaddd62e92011-02-16 17:49:14 -0800492 void setStayPressed(boolean stayPressed) {
493 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800494 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800495 }
496
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700497 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800498 public void onVisibilityAggregated(boolean isVisible) {
499 super.onVisibilityAggregated(isVisible);
500 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800501 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800502 }
503 }
504
Tony Wickham8ac277e2021-05-24 15:47:38 -0700505 public void clearPressedBackground() {
Sunny Goyal508da152014-08-14 10:53:27 -0700506 setPressed(false);
507 setStayPressed(false);
508 }
509
510 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700511 public boolean onKeyUp(int keyCode, KeyEvent event) {
512 // Unlike touch events, keypress event propagate pressed state change immediately,
513 // without waiting for onClickHandler to execute. Disable pressed state changes here
514 // to avoid flickering.
515 mIgnorePressedStateChange = true;
516 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700517 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800518 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700519 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800520 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800521
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700522 @Override
523 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
524 super.onSizeChanged(w, h, oldw, oldh);
525 checkForEllipsis();
526 }
527
528 @Override
529 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
530 super.onTextChanged(text, start, lengthBefore, lengthAfter);
531 checkForEllipsis();
532 }
533
534 private void checkForEllipsis() {
535 if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) {
536 return;
537 }
538 float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
539 if (width <= 0) {
540 return;
541 }
542 setLetterSpacing(0);
543
544 String text = getText().toString();
545 TextPaint paint = getPaint();
546 if (paint.measureText(text) < width) {
547 return;
548 }
549
550 float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING);
551 // Reset the paint value so that the call to TextView does appropriate diff.
552 paint.setLetterSpacing(0);
553 setLetterSpacing(spacing);
554 }
555
556 /**
557 * Find the appropriate text spacing to display the provided text
558 * @param paint the paint used by the text view
559 * @param text the text to display
560 * @param allowedWidthPx available space to render the text
561 * @param minSpacingEm minimum spacing allowed between characters
562 * @return the final textSpacing value
563 *
564 * @see #setLetterSpacing(float)
565 */
566 private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx,
567 float minSpacingEm) {
568 paint.setLetterSpacing(minSpacingEm);
569 if (paint.measureText(text) > allowedWidthPx) {
570 // If there is no result at high limit, we can do anything more
571 return minSpacingEm;
572 }
573
574 float lowLimit = 0;
575 float highLimit = minSpacingEm;
576
577 for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) {
578 float value = (lowLimit + highLimit) / 2;
579 paint.setLetterSpacing(value);
580 if (paint.measureText(text) < allowedWidthPx) {
581 highLimit = value;
582 } else {
583 lowLimit = value;
584 }
585 }
586
587 // At the end error on the higher side
588 return highLimit;
589 }
590
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700591 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800592 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700593 super.onDraw(canvas);
594 }
595
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700597 public void onDraw(Canvas canvas) {
598 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800599 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700600 }
601
Tony Wickham1237df02017-02-24 08:59:36 -0800602 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800603 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700604 *
Tony Wickham1237df02017-02-24 08:59:36 -0800605 * @param canvas The canvas to draw to.
606 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800607 protected void drawDotIfNecessary(Canvas canvas) {
608 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800609 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700610 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
611 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800612 final int scrollX = getScrollX();
613 final int scrollY = getScrollY();
614 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800615 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800616 canvas.translate(-scrollX, -scrollY);
617 }
618 }
619
Jon Miranda9b78e192019-08-30 18:42:01 -0700620 @Override
621 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800622 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800623 return;
624 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800625 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800626
Tony Wickhamf34bee82018-12-03 18:11:39 -0800627 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800628 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800629 } else if (hasDot()) {
630 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800631 }
632 }
633
Tony Wickhamf34bee82018-12-03 18:11:39 -0800634 private boolean hasDot() {
635 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800636 }
637
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700638 /**
639 * Get the icon bounds on the view depending on the layout type.
640 */
Tony Wickham1237df02017-02-24 08:59:36 -0800641 public void getIconBounds(Rect outBounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700642 getIconBounds(mIconSize, outBounds);
Tony05d98c22018-07-23 08:01:15 -0700643 }
644
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700645 /**
646 * Get the icon bounds on the view depending on the layout type.
647 */
648 public void getIconBounds(int iconSize, Rect outBounds) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700649 outBounds.set(0, 0, iconSize, iconSize);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700650 if (mLayoutHorizontal) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700651 int top = (getHeight() - iconSize) / 2;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700652 if (mIsRtl) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700653 outBounds.offsetTo(getWidth() - iconSize - getPaddingRight(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700654 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700655 outBounds.offsetTo(getPaddingLeft(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700656 }
657 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700658 outBounds.offset((getWidth() - iconSize) / 2, getPaddingTop());
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661
Jon Miranda228877d2021-02-09 11:05:00 -0500662 /**
663 * Sets whether to vertically center the content.
664 */
665 public void setCenterVertically(boolean centerVertically) {
666 mCenterVertically = centerVertically;
667 }
668
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 @Override
Winson1f064272016-07-18 17:18:02 -0700670 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
671 if (mCenterVertically) {
672 Paint.FontMetrics fm = getPaint().getFontMetrics();
673 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
674 (int) Math.ceil(fm.bottom - fm.top);
675 int height = MeasureSpec.getSize(heightMeasureSpec);
676 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
677 getPaddingBottom());
678 }
Brandon Dayauon7d673312023-04-03 12:32:24 -0700679 // Only apply two line for all_apps and device search only if necessary.
680 if (shouldUseTwoLine() && (mLastOriginalText != null)) {
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800681 CharSequence modifiedString = modifyTitleToSupportMultiLine(
682 MeasureSpec.getSize(widthMeasureSpec) - getCompoundPaddingLeft()
683 - getCompoundPaddingRight(),
684 mLastOriginalText,
685 getPaint(), mBreakPointsIntArray);
686 if (!TextUtils.equals(modifiedString, mLastModifiedText)) {
687 mLastModifiedText = modifiedString;
688 setText(modifiedString);
689 // if text contains NEW_LINE, set max lines to 2
690 if (TextUtils.indexOf(modifiedString, NEW_LINE) != -1) {
691 setSingleLine(false);
692 setMaxLines(2);
693 } else {
694 setSingleLine(true);
695 setMaxLines(1);
696 }
697 }
698 }
Winson1f064272016-07-18 17:18:02 -0700699 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
700 }
701
702 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700703 public void setTextColor(int color) {
704 mTextColor = color;
Jon Spivack68862142021-10-28 18:37:19 -0700705 mTextColorStateList = null;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700706 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700707 }
708
Adam Cohen96bb7982014-07-07 11:58:56 -0700709 @Override
710 public void setTextColor(ColorStateList colors) {
711 mTextColor = colors.getDefaultColor();
Jon Spivack68862142021-10-28 18:37:19 -0700712 mTextColorStateList = colors;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700713 if (Float.compare(mTextAlpha, 1) == 0) {
714 super.setTextColor(colors);
715 } else {
716 super.setTextColor(getModifiedColor());
717 }
Adam Cohen477828c2013-09-20 12:05:49 -0700718 }
719
Tony7308cde2017-06-27 22:38:33 -0700720 public boolean shouldTextBeVisible() {
721 // Text should be visible everywhere but the hotseat.
722 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
723 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700724 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
725 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700726 }
727
Winson Chung5f8afe62013-08-12 16:19:28 -0700728 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700729 setTextAlpha(visible ? 1 : 0);
730 }
731
732 private void setTextAlpha(float alpha) {
733 mTextAlpha = alpha;
Jon Spivack68862142021-10-28 18:37:19 -0700734 if (mTextColorStateList != null) {
735 setTextColor(mTextColorStateList);
736 } else {
737 super.setTextColor(getModifiedColor());
738 }
Sunny Goyaled7a6932018-04-13 11:41:50 -0700739 }
740
741 private int getModifiedColor() {
742 if (mTextAlpha == 0) {
743 // Special case to prevent text shadows in high contrast mode
744 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700745 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800746 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700747 }
748
749 /**
750 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700751 *
Tony8f402802017-06-16 17:24:54 -0700752 * @param fadeIn Whether the text should fade in or fade out.
753 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700754 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700755 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700756 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700757 }
758
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800759 /**
760 * Generate a new string that will support two line text depending on the current string.
761 * This method calculates the limited width of a text view and creates a string to fit as
762 * many words as it can until the limit is reached. Once the limit is reached, we decide to
763 * either return the original title or continue on a new line. How to get the new string is by
764 * iterating through the list of break points and determining if the strings between the break
765 * points can fit within the line it is in.
766 * Example assuming each character takes up one spot:
767 * title = "Battery Stats", breakpoint = [6], stringPtr = 0, limitedWidth = 7
768 * We get the current word -> from sublist(0, breakpoint[i]+1) so sublist (0,7) -> Battery,
769 * now stringPtr = 7 then from sublist(7) the current string is " Stats" and the runningWidth
770 * at this point exceeds limitedWidth and so we put " Stats" onto the next line (after checking
771 * if the first char is a SPACE, we trim to append "Stats". So resulting string would be
772 * "Battery\nStats"
773 */
774 public static CharSequence modifyTitleToSupportMultiLine(int limitedWidth, CharSequence title,
775 TextPaint paint, IntArray breakPoints) {
776 // current title is less than the width allowed so we can just skip
777 if (title == null || paint.measureText(title, 0, title.length()) <= limitedWidth) {
778 return title;
779 }
780 float currentWordWidth, runningWidth = 0;
781 CharSequence currentWord;
782 StringBuilder newString = new StringBuilder();
783 int stringPtr = 0;
784 for (int i = 0; i < breakPoints.size()+1; i++) {
785 if (i < breakPoints.size()) {
786 currentWord = title.subSequence(stringPtr, breakPoints.get(i)+1);
787 } else {
788 // last word from recent breakpoint until the end of the string
789 currentWord = title.subSequence(stringPtr, title.length());
790 }
791 currentWordWidth = paint.measureText(currentWord,0, currentWord.length());
792 runningWidth += currentWordWidth;
793 if (runningWidth <= limitedWidth) {
794 newString.append(currentWord);
795 } else {
796 // there is no more space
797 if (i == 0) {
798 // if the first words exceeds width, just return as the first line will ellipse
799 return title;
800 } else {
801 // If putting word onto a new line, make sure there is no space or new line
802 // character in the beginning of the current word and just put in the rest of
803 // the characters.
804 CharSequence lastCharacters = title.subSequence(stringPtr, title.length());
805 int beginningLetterType =
806 Character.getType(Character.codePointAt(lastCharacters,0));
807 if (beginningLetterType == Character.SPACE_SEPARATOR
808 || beginningLetterType == Character.LINE_SEPARATOR) {
809 lastCharacters = lastCharacters.length() > 1
810 ? lastCharacters.subSequence(1, lastCharacters.length())
811 : EMPTY;
812 }
813 newString.append(NEW_LINE).append(lastCharacters);
814 return newString.toString();
815 }
816 }
817 if (i >= breakPoints.size()) {
818 // no need to look forward into the string if we've already finished processing
819 break;
820 }
821 stringPtr = breakPoints.get(i)+1;
822 }
823 return newString.toString();
824 }
825
Winson Chungaffd7b42010-08-20 15:11:56 -0700826 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800827 public void cancelLongPress() {
828 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800829 mLongPressHelper.cancelLongPress();
830 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500831
Samuel Fufac2820182021-01-22 11:47:25 -0600832 /**
833 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000834 *
835 * If this app is installing, the progress bar will be updated with the installation progress.
836 * If this app is installed and downloading incrementally, the progress bar will be updated
837 * with the total download progress.
838 */
Federico Baron4eb502a2022-12-14 00:04:53 -0800839 public void applyLoadingState(PreloadIconDrawable icon) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800840 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700841 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Federico Baron4eb502a2022-12-14 00:04:53 -0800842 if ((info.runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0
843 || info.hasPromiseIconUi()
844 || (info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0
845 || (ENABLE_DOWNLOAD_APP_UX_V2.get() && icon != null)) {
Federico Barondb599b22023-03-09 14:44:41 -0800846 updateProgressBarUi(info.getProgressLevel() == 100 ? icon : null);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700847 }
848 }
849 }
850
Federico Baron4eb502a2022-12-14 00:04:53 -0800851 private void updateProgressBarUi(PreloadIconDrawable oldIcon) {
852 FastBitmapDrawable originalIcon = mIcon;
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800853 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Federico Baron4eb502a2022-12-14 00:04:53 -0800854 if (preloadDrawable != null && oldIcon != null) {
855 preloadDrawable.maybePerformFinishedAnimation(oldIcon, () -> setIcon(originalIcon));
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000856 }
857 }
858
859 /** Applies the given progress level to the this icon's progress bar. */
860 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800861 public PreloadIconDrawable applyProgressLevel() {
862 if (!(getTag() instanceof ItemInfoWithIcon)) {
863 return null;
864 }
865
866 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
867 int progressLevel = info.getProgressLevel();
868 if (progressLevel >= 100) {
869 setContentDescription(info.contentDescription != null
870 ? info.contentDescription : "");
871 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800872 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800873 } else {
874 setContentDescription(getContext()
875 .getString(R.string.app_waiting_download_title, info.title));
876 }
877 if (mIcon != null) {
878 PreloadIconDrawable preloadIconDrawable;
879 if (mIcon instanceof PreloadIconDrawable) {
880 preloadIconDrawable = (PreloadIconDrawable) mIcon;
881 preloadIconDrawable.setLevel(progressLevel);
fbaron6664d8c2023-04-04 13:02:59 -0700882 preloadIconDrawable.setIsDisabled(ENABLE_DOWNLOAD_APP_UX_V2.get()
883 ? info.getProgressLevel() == 0
884 : !info.isAppStartable());
Mario Bertschler230612f2017-09-27 17:05:21 -0700885 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800886 preloadIconDrawable = makePreloadIcon();
887 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700888 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800889 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400890 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700891 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400892 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700893
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800894 /**
895 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
896 * object.
897 */
898 @Nullable
899 public PreloadIconDrawable makePreloadIcon() {
900 if (!(getTag() instanceof ItemInfoWithIcon)) {
901 return null;
902 }
903
904 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
905 int progressLevel = info.getProgressLevel();
906 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
907
908 preloadDrawable.setLevel(progressLevel);
fbaron6664d8c2023-04-04 13:02:59 -0700909 preloadDrawable.setIsDisabled(ENABLE_DOWNLOAD_APP_UX_V2.get()
910 ? info.getProgressLevel() == 0
911 : !info.isAppStartable());
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800912 return preloadDrawable;
913 }
914
Tony Wickhamf34bee82018-12-03 18:11:39 -0800915 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800916 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800917 boolean wasDotted = mDotInfo != null;
918 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
919 boolean isDotted = mDotInfo != null;
920 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700921 if (mDisplay == DISPLAY_ALL_APPS) {
922 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
923 } else {
924 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
925 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800926 if (wasDotted || isDotted) {
927 // Animate when a dot is first added or when it is removed.
928 if (animate && (wasDotted ^ isDotted) && isShown()) {
929 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800930 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800931 cancelDotScaleAnim();
932 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800933 invalidate();
934 }
935 }
Andy Wickham56be73b2022-01-18 09:13:43 -0800936 if (!TextUtils.isEmpty(itemInfo.contentDescription)) {
vadimtda6dad52019-05-21 13:24:23 -0700937 if (itemInfo.isDisabled()) {
938 setContentDescription(getContext().getString(R.string.disabled_app_label,
939 itemInfo.contentDescription));
940 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800941 int count = mDotInfo.getNotificationCount();
Steven Ng707b1182021-09-13 11:54:58 +0100942 setContentDescription(
943 getAppLabelPluralString(itemInfo.contentDescription.toString(), count));
Tony Wickham305e9202018-01-23 17:46:47 -0800944 } else {
945 setContentDescription(itemInfo.contentDescription);
946 }
947 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800948 }
949 }
950
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800951 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
952 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
953 != 0) {
954 String percentageString = NumberFormat.getPercentInstance()
955 .format(progressLevel * 0.01);
Federico Baron4eb502a2022-12-14 00:04:53 -0800956 if ((info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800957 setContentDescription(getContext()
958 .getString(
959 R.string.app_installing_title, info.title, percentageString));
960 } else if ((info.runtimeStatusFlags
Federico Baron4eb502a2022-12-14 00:04:53 -0800961 & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800962 setContentDescription(getContext()
963 .getString(
964 R.string.app_downloading_title, info.title, percentageString));
965 }
966 }
967 }
968
Winson Chungb745afb2015-03-02 11:51:23 -0800969 /**
970 * Sets the icon for this view based on the layout direction.
971 */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700972 protected void setIcon(FastBitmapDrawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700973 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700974 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700975 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700976 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800977 if (mIcon != null) {
978 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
979 }
Tony17b7f9b2017-05-23 12:19:09 -0700980 }
981
Jon Miranda9b78e192019-08-30 18:42:01 -0700982 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700983 public void setIconVisible(boolean visible) {
984 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700985 if (!mIsIconVisible) {
986 resetIconScale();
987 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700988 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700989 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700990 }
991
Tony Wickham1e29b042023-03-16 19:58:30 +0000992 /** Sets the icon visual state to disabled or not. */
993 public void setIconDisabled(boolean isDisabled) {
994 if (mIcon != null) {
995 mIcon.setIsDisabled(isDisabled);
996 }
997 }
998
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400999 protected boolean iconUpdateAnimationEnabled() {
1000 return mEnableIconUpdateAnimation;
1001 }
1002
Tony Wickham377ed3f2016-07-20 15:21:04 -07001003 protected void applyCompoundDrawables(Drawable icon) {
Jon Mirandafe749972022-03-15 10:02:39 -07001004 if (icon == null) {
1005 // Icon can be null when we use the BubbleTextView for text only.
1006 return;
1007 }
1008
Sunny Goyal66dccad2018-03-19 12:00:51 -07001009 // If we had already set an icon before, disable relayout as the icon size is the
1010 // same as before.
1011 mDisableRelayout = mIcon != null;
1012
1013 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001014
1015 updateIcon(icon);
1016
1017 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -04001018 if (mIcon != null
1019 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001020 && iconUpdateAnimationEnabled()) {
Steven Nge92bc552021-02-10 17:10:15 +00001021 ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon);
Winson Chungb745afb2015-03-02 11:51:23 -08001022 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001023
Sunny Goyal66dccad2018-03-19 12:00:51 -07001024 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -08001025 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001026
Sunny Goyal69b75642015-05-15 17:00:24 -07001027 @Override
1028 public void requestLayout() {
1029 if (!mDisableRelayout) {
1030 super.requestLayout();
1031 }
1032 }
1033
Sunny Goyal34b65272015-03-11 16:56:52 -07001034 /**
1035 * Applies the item info if it is same as what the view is pointing to currently.
1036 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001037 @Override
1038 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001039 if (getTag() == info) {
1040 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -07001041 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001042 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -07001043
Jon Miranda7f522a22017-07-28 11:56:47 -07001044 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -07001045 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -07001046
Sunny Goyal34b65272015-03-11 16:56:52 -07001047 if (info instanceof AppInfo) {
1048 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -07001049 } else if (info instanceof WorkspaceItemInfo) {
1050 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001051 mActivity.invalidateParent(info);
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001052 } else if (info != null) {
1053 applyFromItemInfoWithIcon(info);
Sunny Goyal34b65272015-03-11 16:56:52 -07001054 }
Winsonc0880492015-08-21 11:16:27 -07001055
Sunny Goyal69b75642015-05-15 17:00:24 -07001056 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001057 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -07001058 }
1059 }
1060
1061 /**
1062 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
1063 */
1064 public void verifyHighRes() {
1065 if (mIconLoadRequest != null) {
1066 mIconLoadRequest.cancel();
1067 mIconLoadRequest = null;
1068 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001069 if (getTag() instanceof ItemInfoWithIcon) {
1070 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -07001071 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001072 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -07001073 .updateIconInBackground(BubbleTextView.this, info);
1074 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001075 }
1076 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -07001077
Jon Miranda47170112017-03-03 14:57:14 -08001078 public int getIconSize() {
1079 return mIconSize;
1080 }
Adam Cohen65086992020-02-19 08:40:49 -08001081
Brandon Dayauond3755f42022-08-29 11:48:27 -07001082 public boolean isDisplaySearchResult() {
1083 return mDisplay == DISPLAY_SEARCH_RESULT ||
1084 mDisplay == DISPLAY_SEARCH_RESULT_SMALL;
1085 }
1086
Sunny Goyal82dfc152023-02-24 16:50:09 -08001087 @Override
1088 public MultiTranslateDelegate getTranslateDelegate() {
1089 return mTranslateDelegate;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001090 }
1091
1092 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001093 public void setReorderBounceScale(float scale) {
1094 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -07001095 super.setScaleX(scale);
1096 super.setScaleY(scale);
1097 }
1098
Sunny Goyal82dfc152023-02-24 16:50:09 -08001099 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001100 public float getReorderBounceScale() {
1101 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -07001102 }
1103
Adam Cohen65086992020-02-19 08:40:49 -08001104 @Override
1105 public int getViewType() {
1106 return DRAGGABLE_ICON;
1107 }
1108
1109 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -07001110 public void getWorkspaceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001111 getIconBounds(mIconSize, bounds);
Adam Cohen65086992020-02-19 08:40:49 -08001112 }
1113
Adam Cohenc77bc452020-05-07 11:55:19 -07001114 public void getSourceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001115 getIconBounds(mIconSize, bounds);
Adam Cohenc77bc452020-05-07 11:55:19 -07001116 }
1117
Adam Cohen65086992020-02-19 08:40:49 -08001118 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -07001119 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001120 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -08001121 setForceHideDot(true);
Sunny Goyal8b9919d2021-04-07 14:45:17 -07001122 return () -> { };
Adam Cohen65086992020-02-19 08:40:49 -08001123 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001124
1125 private void resetIconScale() {
Sunny Goyal52f4c162022-04-23 16:02:03 +00001126 if (mIcon != null) {
1127 mIcon.resetScale();
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001128 }
1129 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001130
1131 private void updateIcon(Drawable newIcon) {
1132 if (mLayoutHorizontal) {
1133 setCompoundDrawablesRelative(newIcon, null, null, null);
1134 } else {
1135 setCompoundDrawables(null, newIcon, null, null);
1136 }
1137 }
Steven Ng707b1182021-09-13 11:54:58 +01001138
1139 private String getAppLabelPluralString(String appName, int notificationCount) {
1140 MessageFormat icuCountFormat = new MessageFormat(
1141 getResources().getString(R.string.dotted_app_label),
1142 Locale.getDefault());
1143 HashMap<String, Object> args = new HashMap();
1144 args.put("app_name", appName);
1145 args.put("count", notificationCount);
1146 return icuCountFormat.format(args);
1147 }
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001148
1149 /**
1150 * Starts a long press action and returns the corresponding pre-drag condition
1151 */
1152 public PreDragCondition startLongPressAction() {
1153 PopupContainerWithArrow popup = PopupContainerWithArrow.showForIcon(this);
1154 return popup != null ? popup.createPreDragCondition(true) : null;
1155 }
1156
1157 /**
1158 * Returns true if the view can show long-press popup
1159 */
1160 public boolean canShowLongPressPopup() {
1161 return getTag() instanceof ItemInfo && ShortcutUtil.supportsShortcuts((ItemInfo) getTag());
1162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163}