blob: 53d9281ac03a6788e046c06e38c069ab4ab46ec2 [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;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080080import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070081import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070082
Sunny Goyalc469aad2015-10-01 11:24:23 -070083import java.text.NumberFormat;
Steven Ng707b1182021-09-13 11:54:58 +010084import java.util.HashMap;
85import java.util.Locale;
Sunny Goyalc469aad2015-10-01 11:24:23 -070086
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087/**
88 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
89 * because we want to make the bubble taller than the text and TextView's clip is
90 * too aggressive.
91 */
Sunny Goyalb65d7662021-03-07 15:09:11 -080092public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver,
Hyunyoung Songdb9a53d2021-02-17 00:35:10 -080093 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070094
Sunny Goyaldfaccf62015-05-11 16:30:44 -070095 private static final int DISPLAY_WORKSPACE = 0;
96 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070097 private static final int DISPLAY_FOLDER = 2;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -070098 protected static final int DISPLAY_TASKBAR = 5;
99 private static final int DISPLAY_SEARCH_RESULT = 6;
100 private static final int DISPLAY_SEARCH_RESULT_SMALL = 7;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700101
Sunny Goyal486adb02021-10-21 11:29:52 -0700102 private static final float MIN_LETTER_SPACING = -0.05f;
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700103 private static final int MAX_SEARCH_LOOP_COUNT = 20;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800104 private static final Character NEW_LINE = '\n';
105 private static final String EMPTY = "";
106 private static final StringMatcherUtility.StringMatcher MATCHER =
107 StringMatcherUtility.StringMatcher.getInstance();
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700108
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700109 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -0700110
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700111 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800112
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800113 private IntArray mBreakPointsIntArray;
114 private CharSequence mLastOriginalText;
115 private CharSequence mLastModifiedText;
116
Tony Wickhamf34bee82018-12-03 18:11:39 -0800117 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
118 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800119 @Override
120 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800121 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800122 }
123
124 @Override
125 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800126 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800127 bubbleTextView.invalidate();
128 }
129 };
130
Sunny Goyaled7a6932018-04-13 11:41:50 -0700131 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
132 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700133 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700134 public Float get(BubbleTextView bubbleTextView) {
135 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700136 }
137
138 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700139 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700140 bubbleTextView.setTextAlpha(alpha);
141 }
142 };
143
Sunny Goyal82dfc152023-02-24 16:50:09 -0800144 private final MultiTranslateDelegate mTranslateDelegate = new MultiTranslateDelegate(this);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800145 private final ActivityContext mActivity;
Sunny Goyalc4c32692021-05-21 14:23:29 -0700146 private FastBitmapDrawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800147 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700148
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800149 protected int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700150
Sunny Goyaled7a6932018-04-13 11:41:50 -0700151 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700152
153 private final boolean mLayoutHorizontal;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700154 private final boolean mIsRtl;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700155 private final int mIconSize;
156
157 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyald872a972021-11-24 18:07:04 -0800158 private boolean mHideBadge = false;
159 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700160 private boolean mIsIconVisible = true;
161 @ViewDebug.ExportedProperty(category = "launcher")
162 private int mTextColor;
163 @ViewDebug.ExportedProperty(category = "launcher")
Jon Spivack68862142021-10-28 18:37:19 -0700164 private ColorStateList mTextColorStateList;
165 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700166 private float mTextAlpha = 1;
167
Tony4043b052018-08-17 13:47:39 -0700168 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800169 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800170 private DotRenderer mDotRenderer;
171 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
Sunny Goyal67555872021-05-21 12:56:55 -0700172 protected DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800173 private Animator mDotScaleAnim;
174 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700175
Sunny Goyal4ffec482016-02-09 11:28:52 -0800176 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800177 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800178 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700179 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800180 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700181 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500182
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800183 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700184
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400185 private boolean mEnableIconUpdateAnimation = false;
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700188 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 }
190
191 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700192 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
194
195 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
196 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800197 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700198
Adam Cohen96bb7982014-07-07 11:58:56 -0700199 TypedArray a = context.obtainStyledAttributes(attrs,
200 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800201 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700202 mIsRtl = (getResources().getConfiguration().getLayoutDirection()
203 == View.LAYOUT_DIRECTION_RTL);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700204 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700205
Samuel Fufac96fa242019-09-26 23:06:32 -0700206 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700207 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700208 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700209 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700210 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700211 defaultIconSize = grid.iconSizePx;
Jon Mirandae126d722021-02-25 10:45:20 -0500212 setCenterVertically(grid.isScalableGrid);
Samuel Fufac96fa242019-09-26 23:06:32 -0700213 } else if (mDisplay == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700214 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
215 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700216 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700217 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700218 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700219 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700220 defaultIconSize = grid.folderChildIconSizePx;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700221 } else if (mDisplay == DISPLAY_SEARCH_RESULT) {
Brandon Dayauonb1957cd2023-03-14 13:27:30 -0700222 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700223 defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size);
224 } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
225 defaultIconSize = getResources().getDimensionPixelSize(
226 R.dimen.search_row_small_icon_size);
Tony Wickham794fe4f2021-01-11 14:54:23 -0600227 } else if (mDisplay == DISPLAY_TASKBAR) {
228 defaultIconSize = grid.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700229 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700230 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700231 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700232 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700233
Winson1f064272016-07-18 17:18:02 -0700234 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700235
236 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
237 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700238 a.recycle();
239
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800240 mLongPressHelper = new CheckLongPressHelper(this);
241
Tony Wickham8912b042018-11-29 15:28:53 -0800242 mDotParams = new DotRenderer.DrawParams();
243
Sunny Goyal66dccad2018-03-19 12:00:51 -0700244 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700245 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700246 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 }
248
Sunny Goyal66dccad2018-03-19 12:00:51 -0700249 @Override
250 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
251 // Disable marques when not focused to that, so that updating text does not cause relayout.
252 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
253 super.onFocusChanged(focused, direction, previouslyFocusedRect);
254 }
255
Sunny Goyald872a972021-11-24 18:07:04 -0800256 public void setHideBadge(boolean hideBadge) {
257 mHideBadge = hideBadge;
258 }
259
Jon Miranda52549442017-10-26 11:28:06 -0700260 /**
261 * Resets the view so it can be recycled.
262 */
263 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800264 mDotInfo = null;
James O'Leary1557ba32022-05-04 16:49:03 +0000265 mDotParams.dotColor = Color.TRANSPARENT;
266 mDotParams.appColor = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800267 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800268 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800269 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800270 setBackground(null);
hyunyoungs825480f2023-03-24 10:11:15 -0700271 if (FeatureFlags.ENABLE_TWOLINE_ALLAPPS.get()
272 || FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()) {
273 setMaxLines(1);
274 }
Sunny Goyal52f4c162022-04-23 16:02:03 +0000275
276 setTag(null);
277 if (mIconLoadRequest != null) {
278 mIconLoadRequest.cancel();
279 mIconLoadRequest = null;
280 }
Jon Miranda52549442017-10-26 11:28:06 -0700281 }
282
Tony Wickhamf34bee82018-12-03 18:11:39 -0800283 private void cancelDotScaleAnim() {
284 if (mDotScaleAnim != null) {
285 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700286 }
287 }
288
Tony Wickhamf34bee82018-12-03 18:11:39 -0800289 private void animateDotScale(float... dotScales) {
290 cancelDotScaleAnim();
291 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
292 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700293 @Override
294 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800295 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700296 }
297 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800298 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700299 }
300
Hyunyoung Song5809a402021-02-02 01:19:18 -0800301 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700302 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
Tony Wickham175d1522021-08-25 09:18:41 -0700303 applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0);
304 }
305
306 @UiThread
307 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) {
Federico Baron4eb502a2022-12-14 00:04:53 -0800308 applyFromWorkspaceItem(info, null);
Winson Chung5f8afe62013-08-12 16:19:28 -0700309 }
310
Tony Wickham175d1522021-08-25 09:18:41 -0700311 /**
312 * Returns whether the newInfo differs from the current getTag().
313 */
314 public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) {
315 WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo
316 ? (WorkspaceItemInfo) getTag()
317 : null;
318 boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null
319 && newInfo.getTargetComponent() != null
320 && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent());
321 return changedIcons && isShown();
322 }
323
Adam Cohen29794c52019-06-21 12:50:30 -0700324 @Override
325 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -0700326 if (delegate instanceof BaseAccessibilityDelegate) {
Adam Cohen29794c52019-06-21 12:50:30 -0700327 super.setAccessibilityDelegate(delegate);
328 } else {
329 // NO-OP
330 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
331 // delegate incorrectly. There are no cases when we shouldn't be using the
332 // LauncherAccessibilityDelegate for BubbleTextView.
333 }
334 }
335
Hyunyoung Song5809a402021-02-02 01:19:18 -0800336 @UiThread
Federico Baron4eb502a2022-12-14 00:04:53 -0800337 public void applyFromWorkspaceItem(WorkspaceItemInfo info, PreloadIconDrawable icon) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800338 applyIconAndLabel(info);
sfufa@google.comca76de02021-09-24 08:55:48 -0700339 setItemInfo(info);
Federico Baron4eb502a2022-12-14 00:04:53 -0800340 applyLoadingState(icon);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800341 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800342 setDownloadStateContentDescription(info, info.getProgressLevel());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800343 }
344
Hyunyoung Song5809a402021-02-02 01:19:18 -0800345 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700346 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800347 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700348
Sunny Goyal95899162019-03-27 16:03:06 -0700349 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700350 setItemInfo(info);
351
Sunny Goyal34b65272015-03-11 16:56:52 -0700352
353 // Verify high res immediately
354 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800355
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000356 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800357 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700358 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800359 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800360 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal508da152014-08-14 10:53:27 -0700361 }
362
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700363 /**
364 * Apply label and tag using a generic {@link ItemInfoWithIcon}
365 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800366 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700367 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800368 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700369 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700370 setItemInfo(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700371
372 // Verify high res immediately
373 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800374
375 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700376 }
377
Sunny Goyalf0ff70a2022-05-11 07:23:10 -0700378 protected void setItemInfo(ItemInfoWithIcon itemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700379 setTag(itemInfo);
sfufa@google.com0fe464d2021-10-14 14:34:50 -0700380 }
381
Hyunyoung Song5809a402021-02-02 01:19:18 -0800382 @UiThread
Samuel Fufac2820182021-01-22 11:47:25 -0600383 protected void applyIconAndLabel(ItemInfoWithIcon info) {
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800384 int flags = shouldUseTheme() ? FLAG_THEMED : 0;
Sunny Goyald872a972021-11-24 18:07:04 -0800385 if (mHideBadge) {
386 flags |= FLAG_NO_BADGE;
387 }
388 FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags);
James O'Leary1557ba32022-05-04 16:49:03 +0000389 mDotParams.appColor = iconDrawable.getIconColor();
390 mDotParams.dotColor = getContext().getResources()
James O'Learyede514f2022-06-06 16:01:45 -0400391 .getColor(android.R.color.system_accent3_200, getContext().getTheme());
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
Hyunyoung Song5809a402021-02-02 01:19:18 -0800401 @UiThread
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800402 @VisibleForTesting
403 public void applyLabel(ItemInfoWithIcon info) {
404 CharSequence label = info.title;
405 if (label != null) {
406 mLastOriginalText = label;
407 mLastModifiedText = mLastOriginalText;
408 mBreakPointsIntArray = StringMatcherUtility.getListOfBreakpoints(label, MATCHER);
409 setText(label);
410 }
Sunny Goyalf4204382016-07-13 10:46:07 -0700411 if (info.contentDescription != null) {
412 setContentDescription(info.isDisabled()
413 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
414 : info.contentDescription);
415 }
416 }
417
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800418 /** This is used for testing to forcefully set the display to ALL_APPS */
419 @VisibleForTesting
420 public void setDisplayAllApps() {
421 mDisplay = DISPLAY_ALL_APPS;
422 }
423
Winson Chung7501adf2015-06-02 11:24:28 -0700424 /**
425 * Overrides the default long press timeout.
426 */
Tony2ca999c2018-09-24 17:24:51 -0400427 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
428 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700429 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800432 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700433 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800434 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800435 }
Sunny Goyal508da152014-08-14 10:53:27 -0700436 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800437
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800438 @Override
439 protected int[] onCreateDrawableState(int extraSpace) {
440 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
441 if (mStayPressed) {
442 mergeDrawableStates(drawableState, STATE_PRESSED);
443 }
444 return drawableState;
445 }
446
Winson Chungb745afb2015-03-02 11:51:23 -0800447 /** Returns the icon for this view. */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700448 public FastBitmapDrawable getIcon() {
Winson Chungb745afb2015-03-02 11:51:23 -0800449 return mIcon;
450 }
451
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700452 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800453 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700454 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700455 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700456 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700457 return false;
458 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700459 if (isLongClickable()) {
460 super.onTouchEvent(event);
461 mLongPressHelper.onTouchEvent(event);
462 // Keep receiving the rest of the events
463 return true;
464 } else {
465 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700466 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800467 }
468
Sunny Goyal44a3b202020-07-09 08:27:06 -0700469 /**
470 * Returns true if the touch down at the provided position be ignored
471 */
472 protected boolean shouldIgnoreTouchDown(float x, float y) {
Tony Wickham42b03952021-08-31 10:41:36 -0700473 if (mDisplay == DISPLAY_TASKBAR) {
474 // Allow touching within padding on taskbar, given icon sizes are smaller.
475 return false;
476 }
Sunny Goyal44a3b202020-07-09 08:27:06 -0700477 return y < getPaddingTop()
478 || x < getPaddingLeft()
479 || y > getHeight() - getPaddingBottom()
480 || x > getWidth() - getPaddingRight();
481 }
482
Michael Jurkaddd62e92011-02-16 17:49:14 -0800483 void setStayPressed(boolean stayPressed) {
484 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800485 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800486 }
487
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700488 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800489 public void onVisibilityAggregated(boolean isVisible) {
490 super.onVisibilityAggregated(isVisible);
491 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800492 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800493 }
494 }
495
Tony Wickham8ac277e2021-05-24 15:47:38 -0700496 public void clearPressedBackground() {
Sunny Goyal508da152014-08-14 10:53:27 -0700497 setPressed(false);
498 setStayPressed(false);
499 }
500
501 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700502 public boolean onKeyUp(int keyCode, KeyEvent event) {
503 // Unlike touch events, keypress event propagate pressed state change immediately,
504 // without waiting for onClickHandler to execute. Disable pressed state changes here
505 // to avoid flickering.
506 mIgnorePressedStateChange = true;
507 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700508 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800509 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700510 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800511 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800512
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700513 @Override
514 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
515 super.onSizeChanged(w, h, oldw, oldh);
516 checkForEllipsis();
517 }
518
519 @Override
520 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
521 super.onTextChanged(text, start, lengthBefore, lengthAfter);
522 checkForEllipsis();
523 }
524
525 private void checkForEllipsis() {
526 if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) {
527 return;
528 }
529 float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
530 if (width <= 0) {
531 return;
532 }
533 setLetterSpacing(0);
534
535 String text = getText().toString();
536 TextPaint paint = getPaint();
537 if (paint.measureText(text) < width) {
538 return;
539 }
540
541 float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING);
542 // Reset the paint value so that the call to TextView does appropriate diff.
543 paint.setLetterSpacing(0);
544 setLetterSpacing(spacing);
545 }
546
547 /**
548 * Find the appropriate text spacing to display the provided text
549 * @param paint the paint used by the text view
550 * @param text the text to display
551 * @param allowedWidthPx available space to render the text
552 * @param minSpacingEm minimum spacing allowed between characters
553 * @return the final textSpacing value
554 *
555 * @see #setLetterSpacing(float)
556 */
557 private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx,
558 float minSpacingEm) {
559 paint.setLetterSpacing(minSpacingEm);
560 if (paint.measureText(text) > allowedWidthPx) {
561 // If there is no result at high limit, we can do anything more
562 return minSpacingEm;
563 }
564
565 float lowLimit = 0;
566 float highLimit = minSpacingEm;
567
568 for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) {
569 float value = (lowLimit + highLimit) / 2;
570 paint.setLetterSpacing(value);
571 if (paint.measureText(text) < allowedWidthPx) {
572 highLimit = value;
573 } else {
574 lowLimit = value;
575 }
576 }
577
578 // At the end error on the higher side
579 return highLimit;
580 }
581
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700582 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800583 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700584 super.onDraw(canvas);
585 }
586
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700588 public void onDraw(Canvas canvas) {
589 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800590 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700591 }
592
Tony Wickham1237df02017-02-24 08:59:36 -0800593 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800594 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700595 *
Tony Wickham1237df02017-02-24 08:59:36 -0800596 * @param canvas The canvas to draw to.
597 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800598 protected void drawDotIfNecessary(Canvas canvas) {
599 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800600 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700601 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
602 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800603 final int scrollX = getScrollX();
604 final int scrollY = getScrollY();
605 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800606 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800607 canvas.translate(-scrollX, -scrollY);
608 }
609 }
610
Jon Miranda9b78e192019-08-30 18:42:01 -0700611 @Override
612 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800613 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800614 return;
615 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800616 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800617
Tony Wickhamf34bee82018-12-03 18:11:39 -0800618 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800619 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800620 } else if (hasDot()) {
621 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800622 }
623 }
624
Tony Wickhamf34bee82018-12-03 18:11:39 -0800625 private boolean hasDot() {
626 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800627 }
628
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700629 /**
630 * Get the icon bounds on the view depending on the layout type.
631 */
Tony Wickham1237df02017-02-24 08:59:36 -0800632 public void getIconBounds(Rect outBounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700633 getIconBounds(mIconSize, outBounds);
Tony05d98c22018-07-23 08:01:15 -0700634 }
635
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700636 /**
637 * Get the icon bounds on the view depending on the layout type.
638 */
639 public void getIconBounds(int iconSize, Rect outBounds) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700640 outBounds.set(0, 0, iconSize, iconSize);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700641 if (mLayoutHorizontal) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700642 int top = (getHeight() - iconSize) / 2;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700643 if (mIsRtl) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700644 outBounds.offsetTo(getWidth() - iconSize - getPaddingRight(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700645 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700646 outBounds.offsetTo(getPaddingLeft(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700647 }
648 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700649 outBounds.offset((getWidth() - iconSize) / 2, getPaddingTop());
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400652
Jon Miranda228877d2021-02-09 11:05:00 -0500653 /**
654 * Sets whether to vertically center the content.
655 */
656 public void setCenterVertically(boolean centerVertically) {
657 mCenterVertically = centerVertically;
658 }
659
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 @Override
Winson1f064272016-07-18 17:18:02 -0700661 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
662 if (mCenterVertically) {
663 Paint.FontMetrics fm = getPaint().getFontMetrics();
664 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
665 (int) Math.ceil(fm.bottom - fm.top);
666 int height = MeasureSpec.getSize(heightMeasureSpec);
667 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
668 getPaddingBottom());
669 }
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800670 // only apply two line for all_apps
Brandon Dayauon2b334302023-03-12 18:46:29 -0700671 if (((FeatureFlags.ENABLE_TWOLINE_ALLAPPS.get() && mDisplay == DISPLAY_ALL_APPS)
672 || (FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()
673 && mDisplay == DISPLAY_SEARCH_RESULT)) && (mLastOriginalText != null)) {
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800674 CharSequence modifiedString = modifyTitleToSupportMultiLine(
675 MeasureSpec.getSize(widthMeasureSpec) - getCompoundPaddingLeft()
676 - getCompoundPaddingRight(),
677 mLastOriginalText,
678 getPaint(), mBreakPointsIntArray);
679 if (!TextUtils.equals(modifiedString, mLastModifiedText)) {
680 mLastModifiedText = modifiedString;
681 setText(modifiedString);
682 // if text contains NEW_LINE, set max lines to 2
683 if (TextUtils.indexOf(modifiedString, NEW_LINE) != -1) {
684 setSingleLine(false);
685 setMaxLines(2);
686 } else {
687 setSingleLine(true);
688 setMaxLines(1);
689 }
690 }
691 }
Winson1f064272016-07-18 17:18:02 -0700692 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
693 }
694
695 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700696 public void setTextColor(int color) {
697 mTextColor = color;
Jon Spivack68862142021-10-28 18:37:19 -0700698 mTextColorStateList = null;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700699 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700700 }
701
Adam Cohen96bb7982014-07-07 11:58:56 -0700702 @Override
703 public void setTextColor(ColorStateList colors) {
704 mTextColor = colors.getDefaultColor();
Jon Spivack68862142021-10-28 18:37:19 -0700705 mTextColorStateList = colors;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700706 if (Float.compare(mTextAlpha, 1) == 0) {
707 super.setTextColor(colors);
708 } else {
709 super.setTextColor(getModifiedColor());
710 }
Adam Cohen477828c2013-09-20 12:05:49 -0700711 }
712
Tony7308cde2017-06-27 22:38:33 -0700713 public boolean shouldTextBeVisible() {
714 // Text should be visible everywhere but the hotseat.
715 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
716 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700717 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
718 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700719 }
720
Winson Chung5f8afe62013-08-12 16:19:28 -0700721 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700722 setTextAlpha(visible ? 1 : 0);
723 }
724
725 private void setTextAlpha(float alpha) {
726 mTextAlpha = alpha;
Jon Spivack68862142021-10-28 18:37:19 -0700727 if (mTextColorStateList != null) {
728 setTextColor(mTextColorStateList);
729 } else {
730 super.setTextColor(getModifiedColor());
731 }
Sunny Goyaled7a6932018-04-13 11:41:50 -0700732 }
733
734 private int getModifiedColor() {
735 if (mTextAlpha == 0) {
736 // Special case to prevent text shadows in high contrast mode
737 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700738 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800739 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700740 }
741
742 /**
743 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700744 *
Tony8f402802017-06-16 17:24:54 -0700745 * @param fadeIn Whether the text should fade in or fade out.
746 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700747 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700748 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700749 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700750 }
751
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800752 /**
753 * Generate a new string that will support two line text depending on the current string.
754 * This method calculates the limited width of a text view and creates a string to fit as
755 * many words as it can until the limit is reached. Once the limit is reached, we decide to
756 * either return the original title or continue on a new line. How to get the new string is by
757 * iterating through the list of break points and determining if the strings between the break
758 * points can fit within the line it is in.
759 * Example assuming each character takes up one spot:
760 * title = "Battery Stats", breakpoint = [6], stringPtr = 0, limitedWidth = 7
761 * We get the current word -> from sublist(0, breakpoint[i]+1) so sublist (0,7) -> Battery,
762 * now stringPtr = 7 then from sublist(7) the current string is " Stats" and the runningWidth
763 * at this point exceeds limitedWidth and so we put " Stats" onto the next line (after checking
764 * if the first char is a SPACE, we trim to append "Stats". So resulting string would be
765 * "Battery\nStats"
766 */
767 public static CharSequence modifyTitleToSupportMultiLine(int limitedWidth, CharSequence title,
768 TextPaint paint, IntArray breakPoints) {
769 // current title is less than the width allowed so we can just skip
770 if (title == null || paint.measureText(title, 0, title.length()) <= limitedWidth) {
771 return title;
772 }
773 float currentWordWidth, runningWidth = 0;
774 CharSequence currentWord;
775 StringBuilder newString = new StringBuilder();
776 int stringPtr = 0;
777 for (int i = 0; i < breakPoints.size()+1; i++) {
778 if (i < breakPoints.size()) {
779 currentWord = title.subSequence(stringPtr, breakPoints.get(i)+1);
780 } else {
781 // last word from recent breakpoint until the end of the string
782 currentWord = title.subSequence(stringPtr, title.length());
783 }
784 currentWordWidth = paint.measureText(currentWord,0, currentWord.length());
785 runningWidth += currentWordWidth;
786 if (runningWidth <= limitedWidth) {
787 newString.append(currentWord);
788 } else {
789 // there is no more space
790 if (i == 0) {
791 // if the first words exceeds width, just return as the first line will ellipse
792 return title;
793 } else {
794 // If putting word onto a new line, make sure there is no space or new line
795 // character in the beginning of the current word and just put in the rest of
796 // the characters.
797 CharSequence lastCharacters = title.subSequence(stringPtr, title.length());
798 int beginningLetterType =
799 Character.getType(Character.codePointAt(lastCharacters,0));
800 if (beginningLetterType == Character.SPACE_SEPARATOR
801 || beginningLetterType == Character.LINE_SEPARATOR) {
802 lastCharacters = lastCharacters.length() > 1
803 ? lastCharacters.subSequence(1, lastCharacters.length())
804 : EMPTY;
805 }
806 newString.append(NEW_LINE).append(lastCharacters);
807 return newString.toString();
808 }
809 }
810 if (i >= breakPoints.size()) {
811 // no need to look forward into the string if we've already finished processing
812 break;
813 }
814 stringPtr = breakPoints.get(i)+1;
815 }
816 return newString.toString();
817 }
818
Winson Chungaffd7b42010-08-20 15:11:56 -0700819 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800820 public void cancelLongPress() {
821 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800822 mLongPressHelper.cancelLongPress();
823 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500824
Samuel Fufac2820182021-01-22 11:47:25 -0600825 /**
826 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000827 *
828 * If this app is installing, the progress bar will be updated with the installation progress.
829 * If this app is installed and downloading incrementally, the progress bar will be updated
830 * with the total download progress.
831 */
Federico Baron4eb502a2022-12-14 00:04:53 -0800832 public void applyLoadingState(PreloadIconDrawable icon) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800833 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700834 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Federico Baron4eb502a2022-12-14 00:04:53 -0800835 if ((info.runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0
836 || info.hasPromiseIconUi()
837 || (info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0
838 || (ENABLE_DOWNLOAD_APP_UX_V2.get() && icon != null)) {
Federico Barondb599b22023-03-09 14:44:41 -0800839 updateProgressBarUi(info.getProgressLevel() == 100 ? icon : null);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700840 }
841 }
842 }
843
Federico Baron4eb502a2022-12-14 00:04:53 -0800844 private void updateProgressBarUi(PreloadIconDrawable oldIcon) {
845 FastBitmapDrawable originalIcon = mIcon;
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800846 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Federico Baron4eb502a2022-12-14 00:04:53 -0800847 if (preloadDrawable != null && oldIcon != null) {
848 preloadDrawable.maybePerformFinishedAnimation(oldIcon, () -> setIcon(originalIcon));
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000849 }
850 }
851
852 /** Applies the given progress level to the this icon's progress bar. */
853 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800854 public PreloadIconDrawable applyProgressLevel() {
855 if (!(getTag() instanceof ItemInfoWithIcon)) {
856 return null;
857 }
858
859 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
860 int progressLevel = info.getProgressLevel();
861 if (progressLevel >= 100) {
862 setContentDescription(info.contentDescription != null
863 ? info.contentDescription : "");
864 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800865 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800866 } else {
867 setContentDescription(getContext()
868 .getString(R.string.app_waiting_download_title, info.title));
869 }
870 if (mIcon != null) {
871 PreloadIconDrawable preloadIconDrawable;
872 if (mIcon instanceof PreloadIconDrawable) {
873 preloadIconDrawable = (PreloadIconDrawable) mIcon;
874 preloadIconDrawable.setLevel(progressLevel);
875 preloadIconDrawable.setIsDisabled(!info.isAppStartable());
Mario Bertschler230612f2017-09-27 17:05:21 -0700876 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800877 preloadIconDrawable = makePreloadIcon();
878 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700879 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800880 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400881 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700882 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400883 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700884
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800885 /**
886 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
887 * object.
888 */
889 @Nullable
890 public PreloadIconDrawable makePreloadIcon() {
891 if (!(getTag() instanceof ItemInfoWithIcon)) {
892 return null;
893 }
894
895 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
896 int progressLevel = info.getProgressLevel();
897 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
898
899 preloadDrawable.setLevel(progressLevel);
900 preloadDrawable.setIsDisabled(!info.isAppStartable());
901
902 return preloadDrawable;
903 }
904
Tony Wickhamf34bee82018-12-03 18:11:39 -0800905 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800906 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800907 boolean wasDotted = mDotInfo != null;
908 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
909 boolean isDotted = mDotInfo != null;
910 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700911 if (mDisplay == DISPLAY_ALL_APPS) {
912 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
913 } else {
914 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
915 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800916 if (wasDotted || isDotted) {
917 // Animate when a dot is first added or when it is removed.
918 if (animate && (wasDotted ^ isDotted) && isShown()) {
919 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800920 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800921 cancelDotScaleAnim();
922 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800923 invalidate();
924 }
925 }
Andy Wickham56be73b2022-01-18 09:13:43 -0800926 if (!TextUtils.isEmpty(itemInfo.contentDescription)) {
vadimtda6dad52019-05-21 13:24:23 -0700927 if (itemInfo.isDisabled()) {
928 setContentDescription(getContext().getString(R.string.disabled_app_label,
929 itemInfo.contentDescription));
930 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800931 int count = mDotInfo.getNotificationCount();
Steven Ng707b1182021-09-13 11:54:58 +0100932 setContentDescription(
933 getAppLabelPluralString(itemInfo.contentDescription.toString(), count));
Tony Wickham305e9202018-01-23 17:46:47 -0800934 } else {
935 setContentDescription(itemInfo.contentDescription);
936 }
937 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800938 }
939 }
940
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800941 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
942 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
943 != 0) {
944 String percentageString = NumberFormat.getPercentInstance()
945 .format(progressLevel * 0.01);
Federico Baron4eb502a2022-12-14 00:04:53 -0800946 if ((info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800947 setContentDescription(getContext()
948 .getString(
949 R.string.app_installing_title, info.title, percentageString));
950 } else if ((info.runtimeStatusFlags
Federico Baron4eb502a2022-12-14 00:04:53 -0800951 & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800952 setContentDescription(getContext()
953 .getString(
954 R.string.app_downloading_title, info.title, percentageString));
955 }
956 }
957 }
958
Winson Chungb745afb2015-03-02 11:51:23 -0800959 /**
960 * Sets the icon for this view based on the layout direction.
961 */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700962 protected void setIcon(FastBitmapDrawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700963 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700964 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700965 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700966 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800967 if (mIcon != null) {
968 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
969 }
Tony17b7f9b2017-05-23 12:19:09 -0700970 }
971
Jon Miranda9b78e192019-08-30 18:42:01 -0700972 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700973 public void setIconVisible(boolean visible) {
974 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700975 if (!mIsIconVisible) {
976 resetIconScale();
977 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700978 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700979 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700980 }
981
Tony Wickham1e29b042023-03-16 19:58:30 +0000982 /** Sets the icon visual state to disabled or not. */
983 public void setIconDisabled(boolean isDisabled) {
984 if (mIcon != null) {
985 mIcon.setIsDisabled(isDisabled);
986 }
987 }
988
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400989 protected boolean iconUpdateAnimationEnabled() {
990 return mEnableIconUpdateAnimation;
991 }
992
Tony Wickham377ed3f2016-07-20 15:21:04 -0700993 protected void applyCompoundDrawables(Drawable icon) {
Jon Mirandafe749972022-03-15 10:02:39 -0700994 if (icon == null) {
995 // Icon can be null when we use the BubbleTextView for text only.
996 return;
997 }
998
Sunny Goyal66dccad2018-03-19 12:00:51 -0700999 // If we had already set an icon before, disable relayout as the icon size is the
1000 // same as before.
1001 mDisableRelayout = mIcon != null;
1002
1003 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001004
1005 updateIcon(icon);
1006
1007 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -04001008 if (mIcon != null
1009 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001010 && iconUpdateAnimationEnabled()) {
Steven Nge92bc552021-02-10 17:10:15 +00001011 ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon);
Winson Chungb745afb2015-03-02 11:51:23 -08001012 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001013
Sunny Goyal66dccad2018-03-19 12:00:51 -07001014 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -08001015 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001016
Sunny Goyal69b75642015-05-15 17:00:24 -07001017 @Override
1018 public void requestLayout() {
1019 if (!mDisableRelayout) {
1020 super.requestLayout();
1021 }
1022 }
1023
Sunny Goyal34b65272015-03-11 16:56:52 -07001024 /**
1025 * Applies the item info if it is same as what the view is pointing to currently.
1026 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001027 @Override
1028 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001029 if (getTag() == info) {
1030 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -07001031 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001032 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -07001033
Jon Miranda7f522a22017-07-28 11:56:47 -07001034 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -07001035 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -07001036
Sunny Goyal34b65272015-03-11 16:56:52 -07001037 if (info instanceof AppInfo) {
1038 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -07001039 } else if (info instanceof WorkspaceItemInfo) {
1040 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001041 mActivity.invalidateParent(info);
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001042 } else if (info != null) {
1043 applyFromItemInfoWithIcon(info);
Sunny Goyal34b65272015-03-11 16:56:52 -07001044 }
Winsonc0880492015-08-21 11:16:27 -07001045
Sunny Goyal69b75642015-05-15 17:00:24 -07001046 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001047 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -07001048 }
1049 }
1050
1051 /**
1052 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
1053 */
1054 public void verifyHighRes() {
1055 if (mIconLoadRequest != null) {
1056 mIconLoadRequest.cancel();
1057 mIconLoadRequest = null;
1058 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001059 if (getTag() instanceof ItemInfoWithIcon) {
1060 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -07001061 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001062 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -07001063 .updateIconInBackground(BubbleTextView.this, info);
1064 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001065 }
1066 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -07001067
Jon Miranda47170112017-03-03 14:57:14 -08001068 public int getIconSize() {
1069 return mIconSize;
1070 }
Adam Cohen65086992020-02-19 08:40:49 -08001071
Brandon Dayauond3755f42022-08-29 11:48:27 -07001072 public boolean isDisplaySearchResult() {
1073 return mDisplay == DISPLAY_SEARCH_RESULT ||
1074 mDisplay == DISPLAY_SEARCH_RESULT_SMALL;
1075 }
1076
Sunny Goyal82dfc152023-02-24 16:50:09 -08001077 @Override
1078 public MultiTranslateDelegate getTranslateDelegate() {
1079 return mTranslateDelegate;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001080 }
1081
1082 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001083 public void setReorderBounceScale(float scale) {
1084 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -07001085 super.setScaleX(scale);
1086 super.setScaleY(scale);
1087 }
1088
Sunny Goyal82dfc152023-02-24 16:50:09 -08001089 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001090 public float getReorderBounceScale() {
1091 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -07001092 }
1093
Adam Cohen65086992020-02-19 08:40:49 -08001094 @Override
1095 public int getViewType() {
1096 return DRAGGABLE_ICON;
1097 }
1098
1099 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -07001100 public void getWorkspaceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001101 getIconBounds(mIconSize, bounds);
Adam Cohen65086992020-02-19 08:40:49 -08001102 }
1103
Adam Cohenc77bc452020-05-07 11:55:19 -07001104 public void getSourceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001105 getIconBounds(mIconSize, bounds);
Adam Cohenc77bc452020-05-07 11:55:19 -07001106 }
1107
Adam Cohen65086992020-02-19 08:40:49 -08001108 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -07001109 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001110 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -08001111 setForceHideDot(true);
Sunny Goyal8b9919d2021-04-07 14:45:17 -07001112 return () -> { };
Adam Cohen65086992020-02-19 08:40:49 -08001113 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001114
1115 private void resetIconScale() {
Sunny Goyal52f4c162022-04-23 16:02:03 +00001116 if (mIcon != null) {
1117 mIcon.resetScale();
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001118 }
1119 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001120
1121 private void updateIcon(Drawable newIcon) {
1122 if (mLayoutHorizontal) {
1123 setCompoundDrawablesRelative(newIcon, null, null, null);
1124 } else {
1125 setCompoundDrawables(null, newIcon, null, null);
1126 }
1127 }
Steven Ng707b1182021-09-13 11:54:58 +01001128
1129 private String getAppLabelPluralString(String appName, int notificationCount) {
1130 MessageFormat icuCountFormat = new MessageFormat(
1131 getResources().getString(R.string.dotted_app_label),
1132 Locale.getDefault());
1133 HashMap<String, Object> args = new HashMap();
1134 args.put("app_name", appName);
1135 args.put("count", notificationCount);
1136 return icuCountFormat.format(args);
1137 }
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001138
1139 /**
1140 * Starts a long press action and returns the corresponding pre-drag condition
1141 */
1142 public PreDragCondition startLongPressAction() {
1143 PopupContainerWithArrow popup = PopupContainerWithArrow.showForIcon(this);
1144 return popup != null ? popup.createPreDragCondition(true) : null;
1145 }
1146
1147 /**
1148 * Returns true if the view can show long-press popup
1149 */
1150 public boolean canShowLongPressPopup() {
1151 return getTag() instanceof ItemInfo && ShortcutUtil.supportsShortcuts((ItemInfo) getTag());
1152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153}