blob: 91da7e640435196261d7f3a1d55be61050d984a7 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -070019import static android.text.Layout.Alignment.ALIGN_NORMAL;
Pat Manning4c27e8c2023-10-19 12:37:55 +010020
21import static com.android.launcher3.Flags.enableCursorHoverStates;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070022import static com.android.launcher3.config.FeatureFlags.ENABLE_ICON_LABEL_AUTO_SCALING;
Sunny Goyal14168432019-10-24 15:59:49 -070023import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon;
Sunny Goyald872a972021-11-24 18:07:04 -080024import static com.android.launcher3.icons.BitmapInfo.FLAG_NO_BADGE;
25import static com.android.launcher3.icons.BitmapInfo.FLAG_THEMED;
Sunny Goyal066ace12018-11-05 11:08:31 -080026import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
Federico Baron4eb502a2022-12-14 00:04:53 -080027import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE;
28import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal066ace12018-11-05 11:08:31 -080029
Tony4043b052018-08-17 13:47:39 -070030import android.animation.Animator;
31import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080032import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070034import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070035import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070037import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070038import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080039import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070040import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.drawable.Drawable;
Steven Ng707b1182021-09-13 11:54:58 +010042import android.icu.text.MessageFormat;
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -070043import android.text.StaticLayout;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070044import android.text.TextPaint;
Andy Wickham56be73b2022-01-18 09:13:43 -080045import android.text.TextUtils;
Sunny Goyal66dccad2018-03-19 12:00:51 -070046import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080047import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080048import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070049import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070050import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080051import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070052import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080053import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080054import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070055
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000056import androidx.annotation.Nullable;
Hyunyoung Song5809a402021-02-02 01:19:18 -080057import androidx.annotation.UiThread;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080058import androidx.annotation.VisibleForTesting;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070059
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -070060import com.android.launcher3.accessibility.BaseAccessibilityDelegate;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080061import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf34bee82018-12-03 18:11:39 -080062import com.android.launcher3.dot.DotInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070063import com.android.launcher3.dragndrop.DragOptions.PreDragCondition;
Adam Cohen65086992020-02-19 08:40:49 -080064import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070065import com.android.launcher3.folder.FolderIcon;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070066import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080067import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080068import com.android.launcher3.icons.DotRenderer;
Sunny Goyal572aca42021-03-24 15:21:39 -070069import com.android.launcher3.icons.FastBitmapDrawable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080070import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal572aca42021-03-24 15:21:39 -070071import com.android.launcher3.icons.PlaceHolderIconDrawable;
Sunny Goyal79e52fc2021-02-03 10:22:28 -080072import com.android.launcher3.icons.cache.HandlerRunnable;
Sunny Goyale396abf2020-04-06 15:11:17 -070073import com.android.launcher3.model.data.AppInfo;
74import com.android.launcher3.model.data.ItemInfo;
75import com.android.launcher3.model.data.ItemInfoWithIcon;
Sunny Goyale396abf2020-04-06 15:11:17 -070076import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070077import com.android.launcher3.popup.PopupContainerWithArrow;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -080078import com.android.launcher3.search.StringMatcherUtility;
79import com.android.launcher3.util.IntArray;
Tony Wickham1e29b042023-03-16 19:58:30 +000080import com.android.launcher3.util.MultiTranslateDelegate;
Samuel Fufa167210a2020-05-12 18:40:11 -070081import com.android.launcher3.util.SafeCloseable;
Sunny Goyalf0ff70a2022-05-11 07:23:10 -070082import com.android.launcher3.util.ShortcutUtil;
fbaron1406a0f2023-05-11 08:46:46 -070083import com.android.launcher3.util.Themes;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080084import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070085import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070086
Sunny Goyalc469aad2015-10-01 11:24:23 -070087import java.text.NumberFormat;
Steven Ng707b1182021-09-13 11:54:58 +010088import java.util.HashMap;
89import java.util.Locale;
Sunny Goyalc469aad2015-10-01 11:24:23 -070090
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091/**
92 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
93 * because we want to make the bubble taller than the text and TextView's clip is
94 * too aggressive.
95 */
Sunny Goyalb65d7662021-03-07 15:09:11 -080096public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver,
Hyunyoung Songdb9a53d2021-02-17 00:35:10 -080097 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070098
Sunny Goyaldfaccf62015-05-11 16:30:44 -070099 private static final int DISPLAY_WORKSPACE = 0;
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700100 public static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700101 private static final int DISPLAY_FOLDER = 2;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700102 protected static final int DISPLAY_TASKBAR = 5;
Brandon Dayauon7e376162023-07-10 16:17:26 -0700103 public static final int DISPLAY_SEARCH_RESULT = 6;
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700104 public static final int DISPLAY_SEARCH_RESULT_SMALL = 7;
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700105 public static final int DISPLAY_PREDICTION_ROW = 8;
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700106 public static final int DISPLAY_SEARCH_RESULT_APP_ROW = 9;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700107
Sunny Goyal486adb02021-10-21 11:29:52 -0700108 private static final float MIN_LETTER_SPACING = -0.05f;
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700109 private static final int MAX_SEARCH_LOOP_COUNT = 20;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800110 private static final Character NEW_LINE = '\n';
111 private static final String EMPTY = "";
112 private static final StringMatcherUtility.StringMatcher MATCHER =
113 StringMatcherUtility.StringMatcher.getInstance();
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700114
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700115 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -0700116
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700117 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800118
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800119 private IntArray mBreakPointsIntArray;
120 private CharSequence mLastOriginalText;
121 private CharSequence mLastModifiedText;
122
Tony Wickhamf34bee82018-12-03 18:11:39 -0800123 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
124 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800125 @Override
126 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800127 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800128 }
129
130 @Override
131 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800132 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800133 bubbleTextView.invalidate();
134 }
135 };
136
Sunny Goyaled7a6932018-04-13 11:41:50 -0700137 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
138 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700139 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700140 public Float get(BubbleTextView bubbleTextView) {
141 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700142 }
143
144 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700145 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700146 bubbleTextView.setTextAlpha(alpha);
147 }
148 };
149
Sunny Goyal82dfc152023-02-24 16:50:09 -0800150 private final MultiTranslateDelegate mTranslateDelegate = new MultiTranslateDelegate(this);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800151 private final ActivityContext mActivity;
Sunny Goyalc4c32692021-05-21 14:23:29 -0700152 private FastBitmapDrawable mIcon;
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700153 private DeviceProfile mDeviceProfile;
Adam Cohen65086992020-02-19 08:40:49 -0800154 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700155
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800156 protected int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700157
Sunny Goyaled7a6932018-04-13 11:41:50 -0700158 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700159
Kevin Limb3b467c2023-07-21 02:15:34 +0000160 private boolean mLayoutHorizontal;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700161 private final boolean mIsRtl;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700162 private final int mIconSize;
163
164 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyald872a972021-11-24 18:07:04 -0800165 private boolean mHideBadge = false;
166 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700167 private boolean mIsIconVisible = true;
168 @ViewDebug.ExportedProperty(category = "launcher")
169 private int mTextColor;
170 @ViewDebug.ExportedProperty(category = "launcher")
Jon Spivack68862142021-10-28 18:37:19 -0700171 private ColorStateList mTextColorStateList;
172 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700173 private float mTextAlpha = 1;
174
Tony4043b052018-08-17 13:47:39 -0700175 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800176 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800177 private DotRenderer mDotRenderer;
Brandon Dayauon812664d2023-11-30 08:51:38 -0800178 private Locale mCurrentLocale;
Tony Wickham8912b042018-11-29 15:28:53 -0800179 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
Sunny Goyal67555872021-05-21 12:56:55 -0700180 protected DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800181 private Animator mDotScaleAnim;
182 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700183
Sunny Goyal4ffec482016-02-09 11:28:52 -0800184 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800185 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800186 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700187 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800188 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700189 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500190
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800191 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700192
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400193 private boolean mEnableIconUpdateAnimation = false;
194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700196 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 }
198
199 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700200 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 }
202
203 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
204 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800205 mActivity = ActivityContext.lookupContext(context);
Pat Manningc49d60b2023-09-13 16:40:27 +0100206 FastBitmapDrawable.setFlagHoverEnabled(enableCursorHoverStates());
Adam Cohen96bb7982014-07-07 11:58:56 -0700207
Adam Cohen96bb7982014-07-07 11:58:56 -0700208 TypedArray a = context.obtainStyledAttributes(attrs,
209 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800210 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700211 mIsRtl = (getResources().getConfiguration().getLayoutDirection()
212 == View.LAYOUT_DIRECTION_RTL);
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700213 mDeviceProfile = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700214
Samuel Fufac96fa242019-09-26 23:06:32 -0700215 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700216 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700217 if (mDisplay == DISPLAY_WORKSPACE) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700218 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.iconTextSizePx);
219 setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
220 defaultIconSize = mDeviceProfile.iconSizePx;
221 setCenterVertically(mDeviceProfile.iconCenterVertically);
Brandon Dayauon47000ae2023-09-11 15:28:11 -0700222 } else if (mDisplay == DISPLAY_ALL_APPS || mDisplay == DISPLAY_PREDICTION_ROW
223 || mDisplay == DISPLAY_SEARCH_RESULT_APP_ROW) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700224 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.allAppsIconTextSizePx);
225 setCompoundDrawablePadding(mDeviceProfile.allAppsIconDrawablePaddingPx);
226 defaultIconSize = mDeviceProfile.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700227 } else if (mDisplay == DISPLAY_FOLDER) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700228 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.folderChildTextSizePx);
229 setCompoundDrawablePadding(mDeviceProfile.folderChildDrawablePaddingPx);
230 defaultIconSize = mDeviceProfile.folderChildIconSizePx;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700231 } else if (mDisplay == DISPLAY_SEARCH_RESULT) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700232 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDeviceProfile.allAppsIconTextSizePx);
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700233 defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size);
234 } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
235 defaultIconSize = getResources().getDimensionPixelSize(
236 R.dimen.search_row_small_icon_size);
Tony Wickham794fe4f2021-01-11 14:54:23 -0600237 } else if (mDisplay == DISPLAY_TASKBAR) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700238 defaultIconSize = mDeviceProfile.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700239 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700240 // widget_selection or shortcut_popup
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700241 defaultIconSize = mDeviceProfile.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700242 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700243
Winson1f064272016-07-18 17:18:02 -0700244 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700245
246 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
247 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700248 a.recycle();
249
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800250 mLongPressHelper = new CheckLongPressHelper(this);
251
Tony Wickham8912b042018-11-29 15:28:53 -0800252 mDotParams = new DotRenderer.DrawParams();
253
Brandon Dayauon812664d2023-11-30 08:51:38 -0800254 mCurrentLocale = context.getResources().getConfiguration().locale;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700255 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700256 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700257 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 }
259
Sunny Goyal66dccad2018-03-19 12:00:51 -0700260 @Override
261 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
262 // Disable marques when not focused to that, so that updating text does not cause relayout.
263 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
264 super.onFocusChanged(focused, direction, previouslyFocusedRect);
265 }
266
Sunny Goyald872a972021-11-24 18:07:04 -0800267 public void setHideBadge(boolean hideBadge) {
268 mHideBadge = hideBadge;
269 }
270
Jon Miranda52549442017-10-26 11:28:06 -0700271 /**
272 * Resets the view so it can be recycled.
273 */
274 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800275 mDotInfo = null;
James O'Leary1557ba32022-05-04 16:49:03 +0000276 mDotParams.dotColor = Color.TRANSPARENT;
277 mDotParams.appColor = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800278 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800279 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800280 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800281 setBackground(null);
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700282 if (FeatureFlags.enableTwolineAllapps() || FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()) {
hyunyoungs825480f2023-03-24 10:11:15 -0700283 setMaxLines(1);
284 }
Sunny Goyal52f4c162022-04-23 16:02:03 +0000285
286 setTag(null);
287 if (mIconLoadRequest != null) {
288 mIconLoadRequest.cancel();
289 mIconLoadRequest = null;
290 }
Jon Miranda52549442017-10-26 11:28:06 -0700291 }
292
Tony Wickhamf34bee82018-12-03 18:11:39 -0800293 private void cancelDotScaleAnim() {
294 if (mDotScaleAnim != null) {
295 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700296 }
297 }
298
Jagrut Desai47bf3222023-10-30 12:15:22 -0700299 public void animateDotScale(float... dotScales) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800300 cancelDotScaleAnim();
301 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
302 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700303 @Override
304 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800305 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700306 }
307 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800308 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700309 }
310
Hyunyoung Song5809a402021-02-02 01:19:18 -0800311 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700312 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
Tony Wickham175d1522021-08-25 09:18:41 -0700313 applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0);
314 }
315
316 @UiThread
317 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) {
Federico Baron4eb502a2022-12-14 00:04:53 -0800318 applyFromWorkspaceItem(info, null);
Winson Chung5f8afe62013-08-12 16:19:28 -0700319 }
320
Tony Wickham175d1522021-08-25 09:18:41 -0700321 /**
322 * Returns whether the newInfo differs from the current getTag().
323 */
324 public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) {
325 WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo
326 ? (WorkspaceItemInfo) getTag()
327 : null;
328 boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null
329 && newInfo.getTargetComponent() != null
330 && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent());
331 return changedIcons && isShown();
332 }
333
Adam Cohen29794c52019-06-21 12:50:30 -0700334 @Override
335 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -0700336 if (delegate instanceof BaseAccessibilityDelegate) {
Adam Cohen29794c52019-06-21 12:50:30 -0700337 super.setAccessibilityDelegate(delegate);
338 } else {
339 // NO-OP
340 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
341 // delegate incorrectly. There are no cases when we shouldn't be using the
342 // LauncherAccessibilityDelegate for BubbleTextView.
343 }
344 }
345
Hyunyoung Song5809a402021-02-02 01:19:18 -0800346 @UiThread
Federico Baron4eb502a2022-12-14 00:04:53 -0800347 public void applyFromWorkspaceItem(WorkspaceItemInfo info, PreloadIconDrawable icon) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800348 applyIconAndLabel(info);
sfufa@google.comca76de02021-09-24 08:55:48 -0700349 setItemInfo(info);
Federico Baron4eb502a2022-12-14 00:04:53 -0800350 applyLoadingState(icon);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800351 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800352 setDownloadStateContentDescription(info, info.getProgressLevel());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800353 }
354
Hyunyoung Song5809a402021-02-02 01:19:18 -0800355 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700356 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800357 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700358
Sunny Goyal95899162019-03-27 16:03:06 -0700359 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700360 setItemInfo(info);
361
Sunny Goyal34b65272015-03-11 16:56:52 -0700362
363 // Verify high res immediately
364 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800365
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000366 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800367 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700368 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800369 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800370 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal508da152014-08-14 10:53:27 -0700371 }
372
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700373 /**
374 * Apply label and tag using a generic {@link ItemInfoWithIcon}
375 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800376 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700377 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800378 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700379 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700380 setItemInfo(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700381
382 // Verify high res immediately
383 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800384
385 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700386 }
387
Sunny Goyalf0ff70a2022-05-11 07:23:10 -0700388 protected void setItemInfo(ItemInfoWithIcon itemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700389 setTag(itemInfo);
sfufa@google.com0fe464d2021-10-14 14:34:50 -0700390 }
391
Brandon Dayauon0ae52c92023-09-06 17:27:18 -0700392 @VisibleForTesting
Hyunyoung Song5809a402021-02-02 01:19:18 -0800393 @UiThread
Brandon Dayauon0ae52c92023-09-06 17:27:18 -0700394 public void applyIconAndLabel(ItemInfoWithIcon info) {
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800395 int flags = shouldUseTheme() ? FLAG_THEMED : 0;
Brandon Dayauon0ae52c92023-09-06 17:27:18 -0700396 // Remove badge on icons smaller than 48dp.
397 if (mHideBadge || mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
Sunny Goyald872a972021-11-24 18:07:04 -0800398 flags |= FLAG_NO_BADGE;
399 }
400 FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags);
James O'Leary1557ba32022-05-04 16:49:03 +0000401 mDotParams.appColor = iconDrawable.getIconColor();
fbaron1406a0f2023-05-11 08:46:46 -0700402 mDotParams.dotColor = Themes.getAttrColor(getContext(), R.attr.notificationDotColor);
Sunny Goyalf4204382016-07-13 10:46:07 -0700403 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000404 applyLabel(info);
405 }
406
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800407 protected boolean shouldUseTheme() {
Brandon Dayauon08cdbc52023-09-14 19:05:09 +0000408 return (mDisplay == DISPLAY_WORKSPACE || mDisplay == DISPLAY_FOLDER
409 || mDisplay == DISPLAY_TASKBAR) && Themes.isThemedIconEnabled(getContext());
Brandon Dayauone640a9e2023-02-03 14:12:25 -0800410 }
411
Brandon Dayauon7d673312023-04-03 12:32:24 -0700412 /**
413 * Only if actual text can be displayed in two line, the {@code true} value will be effective.
414 */
415 protected boolean shouldUseTwoLine() {
Brandon Dayauon812664d2023-11-30 08:51:38 -0800416 return (FeatureFlags.enableTwolineAllapps() && isCurrentLanguageEnglish())
417 && (mDisplay == DISPLAY_ALL_APPS || mDisplay == DISPLAY_PREDICTION_ROW);
418 }
419
420 protected boolean isCurrentLanguageEnglish() {
421 return mCurrentLocale.equals(Locale.US);
Brandon Dayauon7d673312023-04-03 12:32:24 -0700422 }
423
Hyunyoung Song5809a402021-02-02 01:19:18 -0800424 @UiThread
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800425 @VisibleForTesting
426 public void applyLabel(ItemInfoWithIcon info) {
427 CharSequence label = info.title;
428 if (label != null) {
429 mLastOriginalText = label;
430 mLastModifiedText = mLastOriginalText;
431 mBreakPointsIntArray = StringMatcherUtility.getListOfBreakpoints(label, MATCHER);
432 setText(label);
433 }
Sunny Goyalf4204382016-07-13 10:46:07 -0700434 if (info.contentDescription != null) {
435 setContentDescription(info.isDisabled()
436 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
437 : info.contentDescription);
438 }
439 }
440
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700441 /** This is used for testing to forcefully set the display. */
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800442 @VisibleForTesting
Brandon Dayauonf2ea3992023-06-26 12:13:30 -0700443 public void setDisplay(int display) {
444 mDisplay = display;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800445 }
446
Winson Chung7501adf2015-06-02 11:24:28 -0700447 /**
448 * Overrides the default long press timeout.
449 */
Tony2ca999c2018-09-24 17:24:51 -0400450 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
451 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700452 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800455 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700456 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800457 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800458 }
Sunny Goyal508da152014-08-14 10:53:27 -0700459 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800460
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800461 @Override
462 protected int[] onCreateDrawableState(int extraSpace) {
463 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
464 if (mStayPressed) {
465 mergeDrawableStates(drawableState, STATE_PRESSED);
466 }
467 return drawableState;
468 }
469
Winson Chungb745afb2015-03-02 11:51:23 -0800470 /** Returns the icon for this view. */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700471 public FastBitmapDrawable getIcon() {
Winson Chungb745afb2015-03-02 11:51:23 -0800472 return mIcon;
473 }
474
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700475 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800476 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700477 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700478 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700479 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700480 return false;
481 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700482 if (isLongClickable()) {
483 super.onTouchEvent(event);
484 mLongPressHelper.onTouchEvent(event);
485 // Keep receiving the rest of the events
486 return true;
487 } else {
488 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700489 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800490 }
491
Sunny Goyal44a3b202020-07-09 08:27:06 -0700492 /**
493 * Returns true if the touch down at the provided position be ignored
494 */
495 protected boolean shouldIgnoreTouchDown(float x, float y) {
Tony Wickham42b03952021-08-31 10:41:36 -0700496 if (mDisplay == DISPLAY_TASKBAR) {
497 // Allow touching within padding on taskbar, given icon sizes are smaller.
498 return false;
499 }
Sunny Goyal44a3b202020-07-09 08:27:06 -0700500 return y < getPaddingTop()
501 || x < getPaddingLeft()
502 || y > getHeight() - getPaddingBottom()
503 || x > getWidth() - getPaddingRight();
504 }
505
Michael Jurkaddd62e92011-02-16 17:49:14 -0800506 void setStayPressed(boolean stayPressed) {
507 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800508 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800509 }
510
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700511 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800512 public void onVisibilityAggregated(boolean isVisible) {
513 super.onVisibilityAggregated(isVisible);
514 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800515 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800516 }
517 }
518
Tony Wickham8ac277e2021-05-24 15:47:38 -0700519 public void clearPressedBackground() {
Sunny Goyal508da152014-08-14 10:53:27 -0700520 setPressed(false);
521 setStayPressed(false);
522 }
523
524 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700525 public boolean onKeyUp(int keyCode, KeyEvent event) {
526 // Unlike touch events, keypress event propagate pressed state change immediately,
527 // without waiting for onClickHandler to execute. Disable pressed state changes here
528 // to avoid flickering.
529 mIgnorePressedStateChange = true;
530 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700531 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800532 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700533 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800534 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800535
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700536 @Override
537 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
538 super.onSizeChanged(w, h, oldw, oldh);
539 checkForEllipsis();
540 }
541
542 @Override
543 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
544 super.onTextChanged(text, start, lengthBefore, lengthAfter);
545 checkForEllipsis();
546 }
547
548 private void checkForEllipsis() {
549 if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) {
550 return;
551 }
552 float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
553 if (width <= 0) {
554 return;
555 }
556 setLetterSpacing(0);
557
558 String text = getText().toString();
559 TextPaint paint = getPaint();
560 if (paint.measureText(text) < width) {
561 return;
562 }
563
564 float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING);
565 // Reset the paint value so that the call to TextView does appropriate diff.
566 paint.setLetterSpacing(0);
567 setLetterSpacing(spacing);
568 }
569
570 /**
571 * Find the appropriate text spacing to display the provided text
572 * @param paint the paint used by the text view
573 * @param text the text to display
574 * @param allowedWidthPx available space to render the text
575 * @param minSpacingEm minimum spacing allowed between characters
576 * @return the final textSpacing value
577 *
578 * @see #setLetterSpacing(float)
579 */
580 private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx,
581 float minSpacingEm) {
582 paint.setLetterSpacing(minSpacingEm);
583 if (paint.measureText(text) > allowedWidthPx) {
584 // If there is no result at high limit, we can do anything more
585 return minSpacingEm;
586 }
587
588 float lowLimit = 0;
589 float highLimit = minSpacingEm;
590
591 for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) {
592 float value = (lowLimit + highLimit) / 2;
593 paint.setLetterSpacing(value);
594 if (paint.measureText(text) < allowedWidthPx) {
595 highLimit = value;
596 } else {
597 lowLimit = value;
598 }
599 }
600
601 // At the end error on the higher side
602 return highLimit;
603 }
604
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700605 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800606 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700607 super.onDraw(canvas);
608 }
609
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700611 public void onDraw(Canvas canvas) {
612 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800613 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700614 }
615
Tony Wickham1237df02017-02-24 08:59:36 -0800616 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800617 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700618 *
Tony Wickham1237df02017-02-24 08:59:36 -0800619 * @param canvas The canvas to draw to.
620 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800621 protected void drawDotIfNecessary(Canvas canvas) {
622 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800623 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700624 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
625 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800626 final int scrollX = getScrollX();
627 final int scrollY = getScrollY();
628 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800629 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800630 canvas.translate(-scrollX, -scrollY);
631 }
632 }
633
Jon Miranda9b78e192019-08-30 18:42:01 -0700634 @Override
635 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800636 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800637 return;
638 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800639 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800640
Tony Wickhamf34bee82018-12-03 18:11:39 -0800641 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800642 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800643 } else if (hasDot()) {
644 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800645 }
646 }
647
Brandon Dayauon7e376162023-07-10 16:17:26 -0700648 @VisibleForTesting
649 public boolean getForceHideDot() {
650 return mForceHideDot;
651 }
652
Jagrut Desai47bf3222023-10-30 12:15:22 -0700653 public boolean hasDot() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800654 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800655 }
656
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700657 /**
658 * Get the icon bounds on the view depending on the layout type.
659 */
Tony Wickham1237df02017-02-24 08:59:36 -0800660 public void getIconBounds(Rect outBounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700661 getIconBounds(mIconSize, outBounds);
Tony05d98c22018-07-23 08:01:15 -0700662 }
663
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700664 /**
665 * Get the icon bounds on the view depending on the layout type.
666 */
667 public void getIconBounds(int iconSize, Rect outBounds) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700668 outBounds.set(0, 0, iconSize, iconSize);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700669 if (mLayoutHorizontal) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700670 int top = (getHeight() - iconSize) / 2;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700671 if (mIsRtl) {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700672 outBounds.offsetTo(getWidth() - iconSize - getPaddingRight(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700673 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700674 outBounds.offsetTo(getPaddingLeft(), top);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700675 }
676 } else {
Sunny Goyal47d4d812022-11-04 16:43:42 -0700677 outBounds.offset((getWidth() - iconSize) / 2, getPaddingTop());
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400680
Jon Miranda228877d2021-02-09 11:05:00 -0500681 /**
Kevin Limb3b467c2023-07-21 02:15:34 +0000682 * Sets whether the layout is horizontal.
683 */
684 public void setLayoutHorizontal(boolean layoutHorizontal) {
685 if (mLayoutHorizontal == layoutHorizontal) {
686 return;
687 }
688
689 mLayoutHorizontal = layoutHorizontal;
690 applyCompoundDrawables(getIconOrTransparentColor());
691 }
692
693 /**
Jon Miranda228877d2021-02-09 11:05:00 -0500694 * Sets whether to vertically center the content.
695 */
696 public void setCenterVertically(boolean centerVertically) {
697 mCenterVertically = centerVertically;
698 }
699
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 @Override
Winson1f064272016-07-18 17:18:02 -0700701 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700702 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson1f064272016-07-18 17:18:02 -0700703 if (mCenterVertically) {
704 Paint.FontMetrics fm = getPaint().getFontMetrics();
705 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
706 (int) Math.ceil(fm.bottom - fm.top);
Winson1f064272016-07-18 17:18:02 -0700707 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
708 getPaddingBottom());
709 }
Brandon Dayauon7d673312023-04-03 12:32:24 -0700710 // Only apply two line for all_apps and device search only if necessary.
711 if (shouldUseTwoLine() && (mLastOriginalText != null)) {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700712 int allowedVerticalSpace = height - getPaddingTop() - getPaddingBottom()
713 - mDeviceProfile.allAppsIconSizePx
714 - mDeviceProfile.allAppsIconDrawablePaddingPx;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800715 CharSequence modifiedString = modifyTitleToSupportMultiLine(
716 MeasureSpec.getSize(widthMeasureSpec) - getCompoundPaddingLeft()
717 - getCompoundPaddingRight(),
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700718 allowedVerticalSpace,
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800719 mLastOriginalText,
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700720 getPaint(),
721 mBreakPointsIntArray,
722 getLineSpacingMultiplier(),
723 getLineSpacingExtra());
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800724 if (!TextUtils.equals(modifiedString, mLastModifiedText)) {
725 mLastModifiedText = modifiedString;
726 setText(modifiedString);
727 // if text contains NEW_LINE, set max lines to 2
728 if (TextUtils.indexOf(modifiedString, NEW_LINE) != -1) {
729 setSingleLine(false);
730 setMaxLines(2);
731 } else {
732 setSingleLine(true);
733 setMaxLines(1);
734 }
735 }
736 }
Winson1f064272016-07-18 17:18:02 -0700737 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
738 }
739
740 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700741 public void setTextColor(int color) {
742 mTextColor = color;
Jon Spivack68862142021-10-28 18:37:19 -0700743 mTextColorStateList = null;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700744 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700745 }
746
Adam Cohen96bb7982014-07-07 11:58:56 -0700747 @Override
748 public void setTextColor(ColorStateList colors) {
749 mTextColor = colors.getDefaultColor();
Jon Spivack68862142021-10-28 18:37:19 -0700750 mTextColorStateList = colors;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700751 if (Float.compare(mTextAlpha, 1) == 0) {
752 super.setTextColor(colors);
753 } else {
754 super.setTextColor(getModifiedColor());
755 }
Adam Cohen477828c2013-09-20 12:05:49 -0700756 }
757
Tony7308cde2017-06-27 22:38:33 -0700758 public boolean shouldTextBeVisible() {
759 // Text should be visible everywhere but the hotseat.
760 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
761 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700762 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
763 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700764 }
765
Winson Chung5f8afe62013-08-12 16:19:28 -0700766 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700767 setTextAlpha(visible ? 1 : 0);
768 }
769
770 private void setTextAlpha(float alpha) {
771 mTextAlpha = alpha;
Jon Spivack68862142021-10-28 18:37:19 -0700772 if (mTextColorStateList != null) {
773 setTextColor(mTextColorStateList);
774 } else {
775 super.setTextColor(getModifiedColor());
776 }
Sunny Goyaled7a6932018-04-13 11:41:50 -0700777 }
778
779 private int getModifiedColor() {
780 if (mTextAlpha == 0) {
781 // Special case to prevent text shadows in high contrast mode
782 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700783 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800784 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700785 }
786
787 /**
788 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700789 *
Tony8f402802017-06-16 17:24:54 -0700790 * @param fadeIn Whether the text should fade in or fade out.
791 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700792 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700793 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700794 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700795 }
796
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800797 /**
798 * Generate a new string that will support two line text depending on the current string.
799 * This method calculates the limited width of a text view and creates a string to fit as
800 * many words as it can until the limit is reached. Once the limit is reached, we decide to
801 * either return the original title or continue on a new line. How to get the new string is by
802 * iterating through the list of break points and determining if the strings between the break
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700803 * points can fit within the line it is in. We will show the modified string if there is enough
804 * horizontal and vertical space, otherwise this method will just return the original string.
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800805 * Example assuming each character takes up one spot:
806 * title = "Battery Stats", breakpoint = [6], stringPtr = 0, limitedWidth = 7
807 * We get the current word -> from sublist(0, breakpoint[i]+1) so sublist (0,7) -> Battery,
808 * now stringPtr = 7 then from sublist(7) the current string is " Stats" and the runningWidth
809 * at this point exceeds limitedWidth and so we put " Stats" onto the next line (after checking
810 * if the first char is a SPACE, we trim to append "Stats". So resulting string would be
811 * "Battery\nStats"
812 */
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700813 public static CharSequence modifyTitleToSupportMultiLine(int limitedWidth, int limitedHeight,
814 CharSequence title, TextPaint paint, IntArray breakPoints, float spacingMultiplier,
815 float spacingExtra) {
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800816 // current title is less than the width allowed so we can just skip
817 if (title == null || paint.measureText(title, 0, title.length()) <= limitedWidth) {
818 return title;
819 }
820 float currentWordWidth, runningWidth = 0;
821 CharSequence currentWord;
822 StringBuilder newString = new StringBuilder();
Brandon Dayauon6c345f42023-10-03 16:38:42 -0700823 // TODO: Remove when ENABLE_ICON_LABEL_AUTO_SCALING feature flag is being cleaned up.
824 paint.setLetterSpacing(MIN_LETTER_SPACING);
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800825 int stringPtr = 0;
826 for (int i = 0; i < breakPoints.size()+1; i++) {
827 if (i < breakPoints.size()) {
828 currentWord = title.subSequence(stringPtr, breakPoints.get(i)+1);
829 } else {
830 // last word from recent breakpoint until the end of the string
831 currentWord = title.subSequence(stringPtr, title.length());
832 }
833 currentWordWidth = paint.measureText(currentWord,0, currentWord.length());
834 runningWidth += currentWordWidth;
835 if (runningWidth <= limitedWidth) {
836 newString.append(currentWord);
837 } else {
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700838 if (i != 0) {
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800839 // If putting word onto a new line, make sure there is no space or new line
840 // character in the beginning of the current word and just put in the rest of
841 // the characters.
842 CharSequence lastCharacters = title.subSequence(stringPtr, title.length());
843 int beginningLetterType =
844 Character.getType(Character.codePointAt(lastCharacters,0));
845 if (beginningLetterType == Character.SPACE_SEPARATOR
846 || beginningLetterType == Character.LINE_SEPARATOR) {
847 lastCharacters = lastCharacters.length() > 1
848 ? lastCharacters.subSequence(1, lastCharacters.length())
849 : EMPTY;
850 }
851 newString.append(NEW_LINE).append(lastCharacters);
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700852 StaticLayout staticLayout = new StaticLayout(newString, paint, limitedWidth,
853 ALIGN_NORMAL, spacingMultiplier, spacingExtra, false);
854 if (staticLayout.getHeight() < limitedHeight) {
855 return newString.toString();
856 }
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800857 }
Brandon Dayauon3cdb32c2023-08-22 21:26:37 -0700858 // if the first words exceeds width, just return as the first line will ellipse
859 return title;
Brandon Dayauoncf88ea12023-01-06 12:38:55 -0800860 }
861 if (i >= breakPoints.size()) {
862 // no need to look forward into the string if we've already finished processing
863 break;
864 }
865 stringPtr = breakPoints.get(i)+1;
866 }
867 return newString.toString();
868 }
869
Winson Chungaffd7b42010-08-20 15:11:56 -0700870 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800871 public void cancelLongPress() {
872 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800873 mLongPressHelper.cancelLongPress();
874 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500875
Samuel Fufac2820182021-01-22 11:47:25 -0600876 /**
877 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000878 *
879 * If this app is installing, the progress bar will be updated with the installation progress.
880 * If this app is installed and downloading incrementally, the progress bar will be updated
881 * with the total download progress.
882 */
Federico Baron4eb502a2022-12-14 00:04:53 -0800883 public void applyLoadingState(PreloadIconDrawable icon) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800884 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700885 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Federico Baron4eb502a2022-12-14 00:04:53 -0800886 if ((info.runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0
887 || info.hasPromiseIconUi()
888 || (info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0
fbaron461a9aa2023-09-27 16:13:27 -0700889 || (icon != null)) {
Federico Barondb599b22023-03-09 14:44:41 -0800890 updateProgressBarUi(info.getProgressLevel() == 100 ? icon : null);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700891 }
892 }
893 }
894
Federico Baron4eb502a2022-12-14 00:04:53 -0800895 private void updateProgressBarUi(PreloadIconDrawable oldIcon) {
896 FastBitmapDrawable originalIcon = mIcon;
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800897 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Federico Baron4eb502a2022-12-14 00:04:53 -0800898 if (preloadDrawable != null && oldIcon != null) {
899 preloadDrawable.maybePerformFinishedAnimation(oldIcon, () -> setIcon(originalIcon));
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000900 }
901 }
902
903 /** Applies the given progress level to the this icon's progress bar. */
904 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800905 public PreloadIconDrawable applyProgressLevel() {
906 if (!(getTag() instanceof ItemInfoWithIcon)) {
907 return null;
908 }
909
910 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
911 int progressLevel = info.getProgressLevel();
912 if (progressLevel >= 100) {
913 setContentDescription(info.contentDescription != null
914 ? info.contentDescription : "");
915 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800916 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800917 } else {
918 setContentDescription(getContext()
919 .getString(R.string.app_waiting_download_title, info.title));
920 }
921 if (mIcon != null) {
922 PreloadIconDrawable preloadIconDrawable;
923 if (mIcon instanceof PreloadIconDrawable) {
924 preloadIconDrawable = (PreloadIconDrawable) mIcon;
925 preloadIconDrawable.setLevel(progressLevel);
fbaron461a9aa2023-09-27 16:13:27 -0700926 preloadIconDrawable.setIsDisabled(info.getProgressLevel() == 0);
Mario Bertschler230612f2017-09-27 17:05:21 -0700927 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800928 preloadIconDrawable = makePreloadIcon();
929 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700930 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800931 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400932 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700933 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400934 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700935
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800936 /**
937 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
938 * object.
939 */
940 @Nullable
941 public PreloadIconDrawable makePreloadIcon() {
942 if (!(getTag() instanceof ItemInfoWithIcon)) {
943 return null;
944 }
945
946 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
947 int progressLevel = info.getProgressLevel();
948 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
949
950 preloadDrawable.setLevel(progressLevel);
fbaron461a9aa2023-09-27 16:13:27 -0700951 preloadDrawable.setIsDisabled(info.getProgressLevel() == 0);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800952 return preloadDrawable;
953 }
954
Tony Wickhamf34bee82018-12-03 18:11:39 -0800955 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800956 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800957 boolean wasDotted = mDotInfo != null;
958 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
959 boolean isDotted = mDotInfo != null;
960 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700961 if (mDisplay == DISPLAY_ALL_APPS) {
962 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
963 } else {
964 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
965 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800966 if (wasDotted || isDotted) {
967 // Animate when a dot is first added or when it is removed.
968 if (animate && (wasDotted ^ isDotted) && isShown()) {
969 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800970 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800971 cancelDotScaleAnim();
972 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800973 invalidate();
974 }
975 }
Andy Wickham56be73b2022-01-18 09:13:43 -0800976 if (!TextUtils.isEmpty(itemInfo.contentDescription)) {
vadimtda6dad52019-05-21 13:24:23 -0700977 if (itemInfo.isDisabled()) {
978 setContentDescription(getContext().getString(R.string.disabled_app_label,
979 itemInfo.contentDescription));
980 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800981 int count = mDotInfo.getNotificationCount();
Steven Ng707b1182021-09-13 11:54:58 +0100982 setContentDescription(
983 getAppLabelPluralString(itemInfo.contentDescription.toString(), count));
Tony Wickham305e9202018-01-23 17:46:47 -0800984 } else {
985 setContentDescription(itemInfo.contentDescription);
986 }
987 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800988 }
989 }
990
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800991 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
992 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
993 != 0) {
994 String percentageString = NumberFormat.getPercentInstance()
995 .format(progressLevel * 0.01);
Federico Baron4eb502a2022-12-14 00:04:53 -0800996 if ((info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800997 setContentDescription(getContext()
998 .getString(
999 R.string.app_installing_title, info.title, percentageString));
1000 } else if ((info.runtimeStatusFlags
Federico Baron4eb502a2022-12-14 00:04:53 -08001001 & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -08001002 setContentDescription(getContext()
1003 .getString(
1004 R.string.app_downloading_title, info.title, percentageString));
1005 }
1006 }
1007 }
1008
Winson Chungb745afb2015-03-02 11:51:23 -08001009 /**
1010 * Sets the icon for this view based on the layout direction.
1011 */
Sunny Goyalc4c32692021-05-21 14:23:29 -07001012 protected void setIcon(FastBitmapDrawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -07001013 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -07001014 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -07001015 }
Sunny Goyal66dccad2018-03-19 12:00:51 -07001016 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -08001017 if (mIcon != null) {
1018 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
1019 }
Tony17b7f9b2017-05-23 12:19:09 -07001020 }
1021
Jon Miranda9b78e192019-08-30 18:42:01 -07001022 @Override
Tony17b7f9b2017-05-23 12:19:09 -07001023 public void setIconVisible(boolean visible) {
1024 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001025 if (!mIsIconVisible) {
1026 resetIconScale();
1027 }
Kevin Limb3b467c2023-07-21 02:15:34 +00001028 Drawable icon = getIconOrTransparentColor();
Tony17b7f9b2017-05-23 12:19:09 -07001029 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -07001030 }
1031
Kevin Limb3b467c2023-07-21 02:15:34 +00001032 private Drawable getIconOrTransparentColor() {
1033 return mIsIconVisible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
1034 }
1035
Tony Wickham1e29b042023-03-16 19:58:30 +00001036 /** Sets the icon visual state to disabled or not. */
1037 public void setIconDisabled(boolean isDisabled) {
1038 if (mIcon != null) {
1039 mIcon.setIsDisabled(isDisabled);
1040 }
1041 }
1042
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001043 protected boolean iconUpdateAnimationEnabled() {
1044 return mEnableIconUpdateAnimation;
1045 }
1046
Tony Wickham377ed3f2016-07-20 15:21:04 -07001047 protected void applyCompoundDrawables(Drawable icon) {
Jon Mirandafe749972022-03-15 10:02:39 -07001048 if (icon == null) {
1049 // Icon can be null when we use the BubbleTextView for text only.
1050 return;
1051 }
1052
Sunny Goyal66dccad2018-03-19 12:00:51 -07001053 // If we had already set an icon before, disable relayout as the icon size is the
1054 // same as before.
1055 mDisableRelayout = mIcon != null;
1056
1057 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001058
1059 updateIcon(icon);
1060
1061 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -04001062 if (mIcon != null
1063 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001064 && iconUpdateAnimationEnabled()) {
Steven Nge92bc552021-02-10 17:10:15 +00001065 ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon);
Winson Chungb745afb2015-03-02 11:51:23 -08001066 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001067
Sunny Goyal66dccad2018-03-19 12:00:51 -07001068 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -08001069 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001070
Sunny Goyal69b75642015-05-15 17:00:24 -07001071 @Override
1072 public void requestLayout() {
1073 if (!mDisableRelayout) {
1074 super.requestLayout();
1075 }
1076 }
1077
Sunny Goyal34b65272015-03-11 16:56:52 -07001078 /**
1079 * Applies the item info if it is same as what the view is pointing to currently.
1080 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001081 @Override
1082 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001083 if (getTag() == info) {
1084 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -07001085 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001086 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -07001087
Jon Miranda7f522a22017-07-28 11:56:47 -07001088 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -07001089 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -07001090
Sunny Goyal34b65272015-03-11 16:56:52 -07001091 if (info instanceof AppInfo) {
1092 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -07001093 } else if (info instanceof WorkspaceItemInfo) {
1094 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001095 mActivity.invalidateParent(info);
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001096 } else if (info != null) {
1097 applyFromItemInfoWithIcon(info);
Sunny Goyal34b65272015-03-11 16:56:52 -07001098 }
Winsonc0880492015-08-21 11:16:27 -07001099
Sunny Goyal69b75642015-05-15 17:00:24 -07001100 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -04001101 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -07001102 }
1103 }
1104
1105 /**
1106 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
1107 */
1108 public void verifyHighRes() {
1109 if (mIconLoadRequest != null) {
1110 mIconLoadRequest.cancel();
1111 mIconLoadRequest = null;
1112 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -08001113 if (getTag() instanceof ItemInfoWithIcon) {
1114 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -07001115 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001116 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -07001117 .updateIconInBackground(BubbleTextView.this, info);
1118 }
Sunny Goyal34b65272015-03-11 16:56:52 -07001119 }
1120 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -07001121
Jon Miranda47170112017-03-03 14:57:14 -08001122 public int getIconSize() {
1123 return mIconSize;
1124 }
Adam Cohen65086992020-02-19 08:40:49 -08001125
Brandon Dayauond3755f42022-08-29 11:48:27 -07001126 public boolean isDisplaySearchResult() {
Brandon Dayauon47000ae2023-09-11 15:28:11 -07001127 return mDisplay == DISPLAY_SEARCH_RESULT
1128 || mDisplay == DISPLAY_SEARCH_RESULT_SMALL
1129 || mDisplay == DISPLAY_SEARCH_RESULT_APP_ROW;
1130 }
1131
1132 public int getIconDisplay() {
1133 return mDisplay;
Brandon Dayauond3755f42022-08-29 11:48:27 -07001134 }
1135
Sunny Goyal82dfc152023-02-24 16:50:09 -08001136 @Override
1137 public MultiTranslateDelegate getTranslateDelegate() {
1138 return mTranslateDelegate;
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001139 }
1140
1141 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001142 public void setReorderBounceScale(float scale) {
1143 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -07001144 super.setScaleX(scale);
1145 super.setScaleY(scale);
1146 }
1147
Sunny Goyal82dfc152023-02-24 16:50:09 -08001148 @Override
Adam Cohen1d13c0b2020-04-21 16:29:12 -07001149 public float getReorderBounceScale() {
1150 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -07001151 }
1152
Adam Cohen65086992020-02-19 08:40:49 -08001153 @Override
1154 public int getViewType() {
1155 return DRAGGABLE_ICON;
1156 }
1157
1158 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -07001159 public void getWorkspaceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001160 getIconBounds(mIconSize, bounds);
Adam Cohen65086992020-02-19 08:40:49 -08001161 }
1162
Adam Cohenc77bc452020-05-07 11:55:19 -07001163 public void getSourceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001164 getIconBounds(mIconSize, bounds);
Adam Cohenc77bc452020-05-07 11:55:19 -07001165 }
1166
Adam Cohen65086992020-02-19 08:40:49 -08001167 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -07001168 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001169 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -08001170 setForceHideDot(true);
Sunny Goyal8b9919d2021-04-07 14:45:17 -07001171 return () -> { };
Adam Cohen65086992020-02-19 08:40:49 -08001172 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001173
1174 private void resetIconScale() {
Sunny Goyal52f4c162022-04-23 16:02:03 +00001175 if (mIcon != null) {
1176 mIcon.resetScale();
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001177 }
1178 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001179
1180 private void updateIcon(Drawable newIcon) {
1181 if (mLayoutHorizontal) {
1182 setCompoundDrawablesRelative(newIcon, null, null, null);
1183 } else {
1184 setCompoundDrawables(null, newIcon, null, null);
1185 }
1186 }
Steven Ng707b1182021-09-13 11:54:58 +01001187
1188 private String getAppLabelPluralString(String appName, int notificationCount) {
1189 MessageFormat icuCountFormat = new MessageFormat(
1190 getResources().getString(R.string.dotted_app_label),
1191 Locale.getDefault());
1192 HashMap<String, Object> args = new HashMap();
1193 args.put("app_name", appName);
1194 args.put("count", notificationCount);
1195 return icuCountFormat.format(args);
1196 }
Sunny Goyalf0ff70a2022-05-11 07:23:10 -07001197
1198 /**
1199 * Starts a long press action and returns the corresponding pre-drag condition
1200 */
1201 public PreDragCondition startLongPressAction() {
1202 PopupContainerWithArrow popup = PopupContainerWithArrow.showForIcon(this);
1203 return popup != null ? popup.createPreDragCondition(true) : null;
1204 }
1205
1206 /**
1207 * Returns true if the view can show long-press popup
1208 */
1209 public boolean canShowLongPressPopup() {
1210 return getTag() instanceof ItemInfo && ShortcutUtil.supportsShortcuts((ItemInfo) getTag());
1211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212}