blob: 041bee95a27139455a319a542817c053a8776d24 [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
Sunny Goyal0d69cb62021-10-04 15:31:38 -070019import static com.android.launcher3.config.FeatureFlags.ENABLE_ICON_LABEL_AUTO_SCALING;
Sunny Goyal14168432019-10-24 15:59:49 -070020import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon;
Sunny Goyald872a972021-11-24 18:07:04 -080021import static com.android.launcher3.icons.BitmapInfo.FLAG_NO_BADGE;
22import static com.android.launcher3.icons.BitmapInfo.FLAG_THEMED;
Sunny Goyal066ace12018-11-05 11:08:31 -080023import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
24
Tony4043b052018-08-17 13:47:39 -070025import android.animation.Animator;
26import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080027import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070029import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070030import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070032import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070033import android.graphics.Paint;
Adam Cohend9162062020-03-24 16:35:35 -070034import android.graphics.PointF;
Tony Wickham1237df02017-02-24 08:59:36 -080035import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070036import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.drawable.Drawable;
Steven Ng707b1182021-09-13 11:54:58 +010038import android.icu.text.MessageFormat;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070039import android.text.TextPaint;
Andy Wickham56be73b2022-01-18 09:13:43 -080040import android.text.TextUtils;
Sunny Goyal66dccad2018-03-19 12:00:51 -070041import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080042import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080043import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070044import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070045import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080046import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070047import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080048import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080049import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070050
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000051import androidx.annotation.Nullable;
Hyunyoung Song5809a402021-02-02 01:19:18 -080052import androidx.annotation.UiThread;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070053
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -070054import com.android.launcher3.accessibility.BaseAccessibilityDelegate;
Tony Wickhamf34bee82018-12-03 18:11:39 -080055import com.android.launcher3.dot.DotInfo;
Adam Cohen65086992020-02-19 08:40:49 -080056import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070057import com.android.launcher3.folder.FolderIcon;
Tony Wickham1237df02017-02-24 08:59:36 -080058import com.android.launcher3.graphics.IconPalette;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070059import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080060import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080061import com.android.launcher3.icons.DotRenderer;
Sunny Goyal572aca42021-03-24 15:21:39 -070062import com.android.launcher3.icons.FastBitmapDrawable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080063import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal572aca42021-03-24 15:21:39 -070064import com.android.launcher3.icons.PlaceHolderIconDrawable;
Sunny Goyal79e52fc2021-02-03 10:22:28 -080065import com.android.launcher3.icons.cache.HandlerRunnable;
Sunny Goyale396abf2020-04-06 15:11:17 -070066import com.android.launcher3.model.data.AppInfo;
67import com.android.launcher3.model.data.ItemInfo;
68import com.android.launcher3.model.data.ItemInfoWithIcon;
69import com.android.launcher3.model.data.PackageItemInfo;
Samuel Fufac2820182021-01-22 11:47:25 -060070import com.android.launcher3.model.data.SearchActionItemInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070071import com.android.launcher3.model.data.WorkspaceItemInfo;
Samuel Fufa167210a2020-05-12 18:40:11 -070072import com.android.launcher3.util.SafeCloseable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080073import com.android.launcher3.views.ActivityContext;
sfufa@google.comd7bdd972021-10-19 12:41:56 -070074import com.android.launcher3.views.BubbleTextHolder;
Jon Miranda9b78e192019-08-30 18:42:01 -070075import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070076
Sunny Goyalc469aad2015-10-01 11:24:23 -070077import java.text.NumberFormat;
Steven Ng707b1182021-09-13 11:54:58 +010078import java.util.HashMap;
79import java.util.Locale;
Sunny Goyalc469aad2015-10-01 11:24:23 -070080
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081/**
82 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
83 * because we want to make the bubble taller than the text and TextView's clip is
84 * too aggressive.
85 */
Sunny Goyalb65d7662021-03-07 15:09:11 -080086public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver,
Hyunyoung Songdb9a53d2021-02-17 00:35:10 -080087 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070088
Sunny Goyaldfaccf62015-05-11 16:30:44 -070089 private static final int DISPLAY_WORKSPACE = 0;
90 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070091 private static final int DISPLAY_FOLDER = 2;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -070092 protected static final int DISPLAY_TASKBAR = 5;
93 private static final int DISPLAY_SEARCH_RESULT = 6;
94 private static final int DISPLAY_SEARCH_RESULT_SMALL = 7;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070095
Sunny Goyal486adb02021-10-21 11:29:52 -070096 private static final float MIN_LETTER_SPACING = -0.05f;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070097 private static final int MAX_SEARCH_LOOP_COUNT = 20;
98
Samuel Fufad6bacdc2020-08-23 21:56:07 -070099 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -0700100 private static final float HIGHLIGHT_SCALE = 1.16f;
101
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700102 private final PointF mTranslationForReorderBounce = new PointF(0, 0);
103 private final PointF mTranslationForReorderPreview = new PointF(0, 0);
104
Nick Chameyev4dd41592021-08-27 14:11:53 +0100105 private float mTranslationXForTaskbarAlignmentAnimation = 0f;
106
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100107 private final PointF mTranslationForMoveFromCenterAnimation = new PointF(0, 0);
108
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700109 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800110
Tony Wickhamf34bee82018-12-03 18:11:39 -0800111 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
112 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800113 @Override
114 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800115 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800116 }
117
118 @Override
119 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800120 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800121 bubbleTextView.invalidate();
122 }
123 };
124
Sunny Goyaled7a6932018-04-13 11:41:50 -0700125 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
126 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700127 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700128 public Float get(BubbleTextView bubbleTextView) {
129 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700130 }
131
132 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700133 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700134 bubbleTextView.setTextAlpha(alpha);
135 }
136 };
137
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800138 private final ActivityContext mActivity;
Sunny Goyalc4c32692021-05-21 14:23:29 -0700139 private FastBitmapDrawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800140 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700141
Tony Wickham794fe4f2021-01-11 14:54:23 -0600142 protected final int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700143
Sunny Goyaled7a6932018-04-13 11:41:50 -0700144 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700145
146 private final boolean mLayoutHorizontal;
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700147 private final boolean mIsRtl;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700148 private final int mIconSize;
149
150 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyald872a972021-11-24 18:07:04 -0800151 private boolean mHideBadge = false;
152 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700153 private boolean mIsIconVisible = true;
154 @ViewDebug.ExportedProperty(category = "launcher")
155 private int mTextColor;
156 @ViewDebug.ExportedProperty(category = "launcher")
Jon Spivack68862142021-10-28 18:37:19 -0700157 private ColorStateList mTextColorStateList;
158 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700159 private float mTextAlpha = 1;
160
Tony4043b052018-08-17 13:47:39 -0700161 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800162 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800163 private DotRenderer mDotRenderer;
164 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
Sunny Goyal67555872021-05-21 12:56:55 -0700165 protected DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800166 private Animator mDotScaleAnim;
167 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700168
Sunny Goyal4ffec482016-02-09 11:28:52 -0800169 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800170 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800171 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700172 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800173 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700174 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500175
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800176 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700177
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400178 private boolean mEnableIconUpdateAnimation = false;
sfufa@google.comd7bdd972021-10-19 12:41:56 -0700179 private BubbleTextHolder mBubbleTextHolder;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700182 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 }
184
185 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700186 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 }
188
189 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
190 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800191 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700192
Adam Cohen96bb7982014-07-07 11:58:56 -0700193 TypedArray a = context.obtainStyledAttributes(attrs,
194 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800195 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700196 mIsRtl = (getResources().getConfiguration().getLayoutDirection()
197 == View.LAYOUT_DIRECTION_RTL);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700198 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700199
Samuel Fufac96fa242019-09-26 23:06:32 -0700200 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700201 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700202 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700203 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700204 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700205 defaultIconSize = grid.iconSizePx;
Jon Mirandae126d722021-02-25 10:45:20 -0500206 setCenterVertically(grid.isScalableGrid);
Samuel Fufac96fa242019-09-26 23:06:32 -0700207 } else if (mDisplay == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700208 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
209 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700210 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700211 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700212 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700213 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700214 defaultIconSize = grid.folderChildIconSizePx;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700215 } else if (mDisplay == DISPLAY_SEARCH_RESULT) {
216 defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size);
217 } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
218 defaultIconSize = getResources().getDimensionPixelSize(
219 R.dimen.search_row_small_icon_size);
Tony Wickham794fe4f2021-01-11 14:54:23 -0600220 } else if (mDisplay == DISPLAY_TASKBAR) {
221 defaultIconSize = grid.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700222 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700223 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700224 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700225 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700226
Winson1f064272016-07-18 17:18:02 -0700227 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700228
229 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
230 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700231 a.recycle();
232
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800233 mLongPressHelper = new CheckLongPressHelper(this);
234
Tony Wickham8912b042018-11-29 15:28:53 -0800235 mDotParams = new DotRenderer.DrawParams();
236
Sunny Goyal66dccad2018-03-19 12:00:51 -0700237 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700238 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700239 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 }
241
Sunny Goyal66dccad2018-03-19 12:00:51 -0700242 @Override
243 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
244 // Disable marques when not focused to that, so that updating text does not cause relayout.
245 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
246 super.onFocusChanged(focused, direction, previouslyFocusedRect);
247 }
248
Sunny Goyald872a972021-11-24 18:07:04 -0800249 public void setHideBadge(boolean hideBadge) {
250 mHideBadge = hideBadge;
251 }
252
Jon Miranda52549442017-10-26 11:28:06 -0700253 /**
254 * Resets the view so it can be recycled.
255 */
256 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800257 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800258 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800259 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800260 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800261 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800262 setBackground(null);
Jon Miranda52549442017-10-26 11:28:06 -0700263 }
264
Tony Wickhamf34bee82018-12-03 18:11:39 -0800265 private void cancelDotScaleAnim() {
266 if (mDotScaleAnim != null) {
267 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700268 }
269 }
270
Tony Wickhamf34bee82018-12-03 18:11:39 -0800271 private void animateDotScale(float... dotScales) {
272 cancelDotScaleAnim();
273 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
274 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700275 @Override
276 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800277 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700278 }
279 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800280 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700281 }
282
Hyunyoung Song5809a402021-02-02 01:19:18 -0800283 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700284 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
Tony Wickham175d1522021-08-25 09:18:41 -0700285 applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0);
286 }
287
288 @UiThread
289 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) {
Sunny Goyal95899162019-03-27 16:03:06 -0700290 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700291 }
292
Tony Wickham175d1522021-08-25 09:18:41 -0700293 /**
294 * Returns whether the newInfo differs from the current getTag().
295 */
296 public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) {
297 WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo
298 ? (WorkspaceItemInfo) getTag()
299 : null;
300 boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null
301 && newInfo.getTargetComponent() != null
302 && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent());
303 return changedIcons && isShown();
304 }
305
Adam Cohen29794c52019-06-21 12:50:30 -0700306 @Override
307 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
Schneider Victor-tuliasbdb0dd82021-11-03 16:26:41 -0700308 if (delegate instanceof BaseAccessibilityDelegate) {
Adam Cohen29794c52019-06-21 12:50:30 -0700309 super.setAccessibilityDelegate(delegate);
310 } else {
311 // NO-OP
312 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
313 // delegate incorrectly. There are no cases when we shouldn't be using the
314 // LauncherAccessibilityDelegate for BubbleTextView.
315 }
316 }
317
Hyunyoung Song5809a402021-02-02 01:19:18 -0800318 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700319 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800320 applyIconAndLabel(info);
sfufa@google.comca76de02021-09-24 08:55:48 -0700321 setItemInfo(info);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000322 applyLoadingState(promiseStateChanged);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800323 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800324 setDownloadStateContentDescription(info, info.getProgressLevel());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800325 }
326
Hyunyoung Song5809a402021-02-02 01:19:18 -0800327 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700328 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800329 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700330
Sunny Goyal95899162019-03-27 16:03:06 -0700331 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700332 setItemInfo(info);
333
Sunny Goyal34b65272015-03-11 16:56:52 -0700334
335 // Verify high res immediately
336 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800337
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000338 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800339 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700340 }
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());
Sunny Goyal508da152014-08-14 10:53:27 -0700343 }
344
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700345 /**
346 * Apply label and tag using a generic {@link ItemInfoWithIcon}
347 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800348 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700349 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800350 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700351 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700352 setItemInfo(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700353
354 // Verify high res immediately
355 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800356
357 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700358 }
359
sfufa@google.com0fe464d2021-10-14 14:34:50 -0700360 private void setItemInfo(ItemInfoWithIcon itemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700361 setTag(itemInfo);
sfufa@google.comd7bdd972021-10-19 12:41:56 -0700362 if (mBubbleTextHolder != null) {
363 mBubbleTextHolder.onItemInfoUpdated(itemInfo);
sfufa@google.comca76de02021-09-24 08:55:48 -0700364 }
Samuel Fufa02be2a42020-09-23 00:45:27 -0700365 }
366
sfufa@google.comd7bdd972021-10-19 12:41:56 -0700367 public void setBubbleTextHolder(
368 BubbleTextHolder bubbleTextHolder) {
369 mBubbleTextHolder = bubbleTextHolder;
sfufa@google.com0fe464d2021-10-14 14:34:50 -0700370 }
371
Hyunyoung Song5809a402021-02-02 01:19:18 -0800372 @UiThread
Samuel Fufac2820182021-01-22 11:47:25 -0600373 protected void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal59f19152021-08-03 12:25:02 -0700374 boolean useTheme = mDisplay == DISPLAY_WORKSPACE || mDisplay == DISPLAY_FOLDER
375 || mDisplay == DISPLAY_TASKBAR;
Sunny Goyald872a972021-11-24 18:07:04 -0800376 int flags = useTheme ? FLAG_THEMED : 0;
377 if (mHideBadge) {
378 flags |= FLAG_NO_BADGE;
379 }
380 FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags);
Sunny Goyal67555872021-05-21 12:56:55 -0700381 mDotParams.color = IconPalette.getMutedColor(iconDrawable.getIconColor(), 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800382
Sunny Goyalf4204382016-07-13 10:46:07 -0700383 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000384 applyLabel(info);
385 }
386
Hyunyoung Song5809a402021-02-02 01:19:18 -0800387 @UiThread
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000388 private void applyLabel(ItemInfoWithIcon info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700389 setText(info.title);
390 if (info.contentDescription != null) {
391 setContentDescription(info.isDisabled()
392 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
393 : info.contentDescription);
394 }
395 }
396
Winson Chung7501adf2015-06-02 11:24:28 -0700397 /**
398 * Overrides the default long press timeout.
399 */
Tony2ca999c2018-09-24 17:24:51 -0400400 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
401 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700402 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800405 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700406 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800407 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800408 }
Sunny Goyal508da152014-08-14 10:53:27 -0700409 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800410
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800411 @Override
412 protected int[] onCreateDrawableState(int extraSpace) {
413 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
414 if (mStayPressed) {
415 mergeDrawableStates(drawableState, STATE_PRESSED);
416 }
417 return drawableState;
418 }
419
Winson Chungb745afb2015-03-02 11:51:23 -0800420 /** Returns the icon for this view. */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700421 public FastBitmapDrawable getIcon() {
Winson Chungb745afb2015-03-02 11:51:23 -0800422 return mIcon;
423 }
424
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700425 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800426 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700427 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700428 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700429 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700430 return false;
431 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700432 if (isLongClickable()) {
433 super.onTouchEvent(event);
434 mLongPressHelper.onTouchEvent(event);
435 // Keep receiving the rest of the events
436 return true;
437 } else {
438 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700439 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800440 }
441
Sunny Goyal44a3b202020-07-09 08:27:06 -0700442 /**
443 * Returns true if the touch down at the provided position be ignored
444 */
445 protected boolean shouldIgnoreTouchDown(float x, float y) {
Tony Wickham42b03952021-08-31 10:41:36 -0700446 if (mDisplay == DISPLAY_TASKBAR) {
447 // Allow touching within padding on taskbar, given icon sizes are smaller.
448 return false;
449 }
Sunny Goyal44a3b202020-07-09 08:27:06 -0700450 return y < getPaddingTop()
451 || x < getPaddingLeft()
452 || y > getHeight() - getPaddingBottom()
453 || x > getWidth() - getPaddingRight();
454 }
455
Michael Jurkaddd62e92011-02-16 17:49:14 -0800456 void setStayPressed(boolean stayPressed) {
457 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800458 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800459 }
460
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700461 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800462 public void onVisibilityAggregated(boolean isVisible) {
463 super.onVisibilityAggregated(isVisible);
464 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800465 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800466 }
467 }
468
Tony Wickham8ac277e2021-05-24 15:47:38 -0700469 public void clearPressedBackground() {
Sunny Goyal508da152014-08-14 10:53:27 -0700470 setPressed(false);
471 setStayPressed(false);
472 }
473
474 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700475 public boolean onKeyUp(int keyCode, KeyEvent event) {
476 // Unlike touch events, keypress event propagate pressed state change immediately,
477 // without waiting for onClickHandler to execute. Disable pressed state changes here
478 // to avoid flickering.
479 mIgnorePressedStateChange = true;
480 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700481 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800482 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700483 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800484 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800485
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700486 @Override
487 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
488 super.onSizeChanged(w, h, oldw, oldh);
489 checkForEllipsis();
490 }
491
492 @Override
493 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
494 super.onTextChanged(text, start, lengthBefore, lengthAfter);
495 checkForEllipsis();
496 }
497
498 private void checkForEllipsis() {
499 if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) {
500 return;
501 }
502 float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
503 if (width <= 0) {
504 return;
505 }
506 setLetterSpacing(0);
507
508 String text = getText().toString();
509 TextPaint paint = getPaint();
510 if (paint.measureText(text) < width) {
511 return;
512 }
513
514 float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING);
515 // Reset the paint value so that the call to TextView does appropriate diff.
516 paint.setLetterSpacing(0);
517 setLetterSpacing(spacing);
518 }
519
520 /**
521 * Find the appropriate text spacing to display the provided text
522 * @param paint the paint used by the text view
523 * @param text the text to display
524 * @param allowedWidthPx available space to render the text
525 * @param minSpacingEm minimum spacing allowed between characters
526 * @return the final textSpacing value
527 *
528 * @see #setLetterSpacing(float)
529 */
530 private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx,
531 float minSpacingEm) {
532 paint.setLetterSpacing(minSpacingEm);
533 if (paint.measureText(text) > allowedWidthPx) {
534 // If there is no result at high limit, we can do anything more
535 return minSpacingEm;
536 }
537
538 float lowLimit = 0;
539 float highLimit = minSpacingEm;
540
541 for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) {
542 float value = (lowLimit + highLimit) / 2;
543 paint.setLetterSpacing(value);
544 if (paint.measureText(text) < allowedWidthPx) {
545 highLimit = value;
546 } else {
547 lowLimit = value;
548 }
549 }
550
551 // At the end error on the higher side
552 return highLimit;
553 }
554
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700555 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800556 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700557 super.onDraw(canvas);
558 }
559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700561 public void onDraw(Canvas canvas) {
562 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800563 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700564 }
565
Tony Wickham1237df02017-02-24 08:59:36 -0800566 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800567 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700568 *
Tony Wickham1237df02017-02-24 08:59:36 -0800569 * @param canvas The canvas to draw to.
570 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800571 protected void drawDotIfNecessary(Canvas canvas) {
572 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800573 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700574 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
575 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800576 final int scrollX = getScrollX();
577 final int scrollY = getScrollY();
578 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800579 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800580 canvas.translate(-scrollX, -scrollY);
581 }
582 }
583
Jon Miranda9b78e192019-08-30 18:42:01 -0700584 @Override
585 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800586 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800587 return;
588 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800589 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800590
Tony Wickhamf34bee82018-12-03 18:11:39 -0800591 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800592 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800593 } else if (hasDot()) {
594 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800595 }
596 }
597
Tony Wickhamf34bee82018-12-03 18:11:39 -0800598 private boolean hasDot() {
599 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800600 }
601
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700602 /**
603 * Get the icon bounds on the view depending on the layout type.
604 */
Tony Wickham1237df02017-02-24 08:59:36 -0800605 public void getIconBounds(Rect outBounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700606 getIconBounds(mIconSize, outBounds);
Tony05d98c22018-07-23 08:01:15 -0700607 }
608
Hyunyoung Songc2f024b2021-11-03 23:16:53 -0700609 /**
610 * Get the icon bounds on the view depending on the layout type.
611 */
612 public void getIconBounds(int iconSize, Rect outBounds) {
613 Utilities.setRectToViewCenter(this, iconSize, outBounds);
614 if (mLayoutHorizontal) {
615 if (mIsRtl) {
616 outBounds.offsetTo(getWidth() - iconSize - getPaddingRight(), outBounds.top);
617 } else {
618 outBounds.offsetTo(getPaddingLeft(), outBounds.top);
619 }
620 } else {
621 outBounds.offsetTo(outBounds.left, getPaddingTop());
622 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400624
Jon Miranda228877d2021-02-09 11:05:00 -0500625 /**
626 * Sets whether to vertically center the content.
627 */
628 public void setCenterVertically(boolean centerVertically) {
629 mCenterVertically = centerVertically;
630 }
631
Joe Onorato9c1289c2009-08-17 11:03:03 -0400632 @Override
Winson1f064272016-07-18 17:18:02 -0700633 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
634 if (mCenterVertically) {
635 Paint.FontMetrics fm = getPaint().getFontMetrics();
636 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
637 (int) Math.ceil(fm.bottom - fm.top);
638 int height = MeasureSpec.getSize(heightMeasureSpec);
639 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
640 getPaddingBottom());
641 }
642 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
643 }
644
645 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700646 public void setTextColor(int color) {
647 mTextColor = color;
Jon Spivack68862142021-10-28 18:37:19 -0700648 mTextColorStateList = null;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700649 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700650 }
651
Adam Cohen96bb7982014-07-07 11:58:56 -0700652 @Override
653 public void setTextColor(ColorStateList colors) {
654 mTextColor = colors.getDefaultColor();
Jon Spivack68862142021-10-28 18:37:19 -0700655 mTextColorStateList = colors;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700656 if (Float.compare(mTextAlpha, 1) == 0) {
657 super.setTextColor(colors);
658 } else {
659 super.setTextColor(getModifiedColor());
660 }
Adam Cohen477828c2013-09-20 12:05:49 -0700661 }
662
Tony7308cde2017-06-27 22:38:33 -0700663 public boolean shouldTextBeVisible() {
664 // Text should be visible everywhere but the hotseat.
665 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
666 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700667 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
668 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700669 }
670
Winson Chung5f8afe62013-08-12 16:19:28 -0700671 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700672 setTextAlpha(visible ? 1 : 0);
673 }
674
675 private void setTextAlpha(float alpha) {
676 mTextAlpha = alpha;
Jon Spivack68862142021-10-28 18:37:19 -0700677 if (mTextColorStateList != null) {
678 setTextColor(mTextColorStateList);
679 } else {
680 super.setTextColor(getModifiedColor());
681 }
Sunny Goyaled7a6932018-04-13 11:41:50 -0700682 }
683
684 private int getModifiedColor() {
685 if (mTextAlpha == 0) {
686 // Special case to prevent text shadows in high contrast mode
687 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700688 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800689 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700690 }
691
692 /**
693 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700694 *
Tony8f402802017-06-16 17:24:54 -0700695 * @param fadeIn Whether the text should fade in or fade out.
696 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700697 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700698 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700699 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700700 }
701
Winson Chungaffd7b42010-08-20 15:11:56 -0700702 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800703 public void cancelLongPress() {
704 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800705 mLongPressHelper.cancelLongPress();
706 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500707
Samuel Fufac2820182021-01-22 11:47:25 -0600708 /**
709 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000710 *
711 * If this app is installing, the progress bar will be updated with the installation progress.
712 * If this app is installed and downloading incrementally, the progress bar will be updated
713 * with the total download progress.
714 */
715 public void applyLoadingState(boolean promiseStateChanged) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800716 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700717 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000718 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE)
719 != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800720 updateProgressBarUi(info.getProgressLevel() == 100);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000721 } else if (info.hasPromiseIconUi() || (info.runtimeStatusFlags
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800722 & ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE) != 0) {
723 updateProgressBarUi(promiseStateChanged);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700724 }
725 }
726 }
727
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800728 private void updateProgressBarUi(boolean maybePerformFinishedAnimation) {
729 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000730 if (preloadDrawable != null && maybePerformFinishedAnimation) {
731 preloadDrawable.maybePerformFinishedAnimation();
732 }
733 }
734
735 /** Applies the given progress level to the this icon's progress bar. */
736 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800737 public PreloadIconDrawable applyProgressLevel() {
738 if (!(getTag() instanceof ItemInfoWithIcon)) {
739 return null;
740 }
741
742 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
743 int progressLevel = info.getProgressLevel();
744 if (progressLevel >= 100) {
745 setContentDescription(info.contentDescription != null
746 ? info.contentDescription : "");
747 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800748 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800749 } else {
750 setContentDescription(getContext()
751 .getString(R.string.app_waiting_download_title, info.title));
752 }
753 if (mIcon != null) {
754 PreloadIconDrawable preloadIconDrawable;
755 if (mIcon instanceof PreloadIconDrawable) {
756 preloadIconDrawable = (PreloadIconDrawable) mIcon;
757 preloadIconDrawable.setLevel(progressLevel);
758 preloadIconDrawable.setIsDisabled(!info.isAppStartable());
Mario Bertschler230612f2017-09-27 17:05:21 -0700759 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800760 preloadIconDrawable = makePreloadIcon();
761 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700762 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800763 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400764 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700765 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400766 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700767
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800768 /**
769 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
770 * object.
771 */
772 @Nullable
773 public PreloadIconDrawable makePreloadIcon() {
774 if (!(getTag() instanceof ItemInfoWithIcon)) {
775 return null;
776 }
777
778 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
779 int progressLevel = info.getProgressLevel();
780 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
781
782 preloadDrawable.setLevel(progressLevel);
783 preloadDrawable.setIsDisabled(!info.isAppStartable());
784
785 return preloadDrawable;
786 }
787
Tony Wickhamf34bee82018-12-03 18:11:39 -0800788 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800789 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800790 boolean wasDotted = mDotInfo != null;
791 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
792 boolean isDotted = mDotInfo != null;
793 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700794 if (mDisplay == DISPLAY_ALL_APPS) {
795 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
796 } else {
797 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
798 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800799 if (wasDotted || isDotted) {
800 // Animate when a dot is first added or when it is removed.
801 if (animate && (wasDotted ^ isDotted) && isShown()) {
802 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800803 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800804 cancelDotScaleAnim();
805 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800806 invalidate();
807 }
808 }
Andy Wickham56be73b2022-01-18 09:13:43 -0800809 if (!TextUtils.isEmpty(itemInfo.contentDescription)) {
vadimtda6dad52019-05-21 13:24:23 -0700810 if (itemInfo.isDisabled()) {
811 setContentDescription(getContext().getString(R.string.disabled_app_label,
812 itemInfo.contentDescription));
813 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800814 int count = mDotInfo.getNotificationCount();
Steven Ng707b1182021-09-13 11:54:58 +0100815 setContentDescription(
816 getAppLabelPluralString(itemInfo.contentDescription.toString(), count));
Tony Wickham305e9202018-01-23 17:46:47 -0800817 } else {
818 setContentDescription(itemInfo.contentDescription);
819 }
820 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800821 }
822 }
823
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800824 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
825 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
826 != 0) {
827 String percentageString = NumberFormat.getPercentInstance()
828 .format(progressLevel * 0.01);
829 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE) != 0) {
830 setContentDescription(getContext()
831 .getString(
832 R.string.app_installing_title, info.title, percentageString));
833 } else if ((info.runtimeStatusFlags
834 & ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
835 setContentDescription(getContext()
836 .getString(
837 R.string.app_downloading_title, info.title, percentageString));
838 }
839 }
840 }
841
Winson Chungb745afb2015-03-02 11:51:23 -0800842 /**
843 * Sets the icon for this view based on the layout direction.
844 */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700845 protected void setIcon(FastBitmapDrawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700846 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700847 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700848 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700849 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800850 if (mIcon != null) {
851 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
852 }
Tony17b7f9b2017-05-23 12:19:09 -0700853 }
854
Jon Miranda9b78e192019-08-30 18:42:01 -0700855 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700856 public void setIconVisible(boolean visible) {
857 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700858 if (!mIsIconVisible) {
859 resetIconScale();
860 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700861 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700862 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700863 }
864
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400865 protected boolean iconUpdateAnimationEnabled() {
866 return mEnableIconUpdateAnimation;
867 }
868
Tony Wickham377ed3f2016-07-20 15:21:04 -0700869 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700870 // If we had already set an icon before, disable relayout as the icon size is the
871 // same as before.
872 mDisableRelayout = mIcon != null;
873
874 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700875
876 updateIcon(icon);
877
878 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -0400879 if (mIcon != null
880 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400881 && iconUpdateAnimationEnabled()) {
Steven Nge92bc552021-02-10 17:10:15 +0000882 ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon);
Winson Chungb745afb2015-03-02 11:51:23 -0800883 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700884
Sunny Goyal66dccad2018-03-19 12:00:51 -0700885 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800886 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700887
Sunny Goyal69b75642015-05-15 17:00:24 -0700888 @Override
889 public void requestLayout() {
890 if (!mDisableRelayout) {
891 super.requestLayout();
892 }
893 }
894
Sunny Goyal34b65272015-03-11 16:56:52 -0700895 /**
896 * Applies the item info if it is same as what the view is pointing to currently.
897 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800898 @Override
899 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700900 if (getTag() == info) {
901 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700902 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400903 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -0700904
Jon Miranda7f522a22017-07-28 11:56:47 -0700905 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -0700906 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -0700907
Sunny Goyal34b65272015-03-11 16:56:52 -0700908 if (info instanceof AppInfo) {
909 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700910 } else if (info instanceof WorkspaceItemInfo) {
911 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700912 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700913 } else if (info instanceof PackageItemInfo) {
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700914 applyFromItemInfoWithIcon((PackageItemInfo) info);
Samuel Fufac2820182021-01-22 11:47:25 -0600915 } else if (info instanceof SearchActionItemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700916 applyFromItemInfoWithIcon((SearchActionItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700917 }
Winsonc0880492015-08-21 11:16:27 -0700918
Sunny Goyal69b75642015-05-15 17:00:24 -0700919 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400920 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700921 }
922 }
923
924 /**
925 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
926 */
927 public void verifyHighRes() {
928 if (mIconLoadRequest != null) {
929 mIconLoadRequest.cancel();
930 mIconLoadRequest = null;
931 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800932 if (getTag() instanceof ItemInfoWithIcon) {
933 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700934 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800935 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700936 .updateIconInBackground(BubbleTextView.this, info);
937 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700938 }
939 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700940
Jon Miranda47170112017-03-03 14:57:14 -0800941 public int getIconSize() {
942 return mIconSize;
943 }
Adam Cohen65086992020-02-19 08:40:49 -0800944
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700945 private void updateTranslation() {
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100946 super.setTranslationX(mTranslationForReorderBounce.x + mTranslationForReorderPreview.x
Nick Chameyev4dd41592021-08-27 14:11:53 +0100947 + mTranslationForMoveFromCenterAnimation.x
948 + mTranslationXForTaskbarAlignmentAnimation);
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100949 super.setTranslationY(mTranslationForReorderBounce.y + mTranslationForReorderPreview.y
950 + mTranslationForMoveFromCenterAnimation.y);
Adam Cohend9162062020-03-24 16:35:35 -0700951 }
952
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700953 public void setReorderBounceOffset(float x, float y) {
954 mTranslationForReorderBounce.set(x, y);
955 updateTranslation();
Adam Cohend9162062020-03-24 16:35:35 -0700956 }
957
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700958 public void getReorderBounceOffset(PointF offset) {
959 offset.set(mTranslationForReorderBounce);
960 }
961
962 @Override
963 public void setReorderPreviewOffset(float x, float y) {
964 mTranslationForReorderPreview.set(x, y);
965 updateTranslation();
966 }
967
968 @Override
969 public void getReorderPreviewOffset(PointF offset) {
970 offset.set(mTranslationForReorderPreview);
971 }
972
973 public void setReorderBounceScale(float scale) {
974 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -0700975 super.setScaleX(scale);
976 super.setScaleY(scale);
977 }
978
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700979 public float getReorderBounceScale() {
980 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -0700981 }
982
Nick Chameyev4dd41592021-08-27 14:11:53 +0100983 /**
984 * Sets translation values for move from center animation
985 */
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100986 public void setTranslationForMoveFromCenterAnimation(float x, float y) {
987 mTranslationForMoveFromCenterAnimation.set(x, y);
988 updateTranslation();
989 }
990
Nick Chameyev4dd41592021-08-27 14:11:53 +0100991 /**
992 * Sets translationX for taskbar to launcher alignment animation
993 */
994 public void setTranslationXForTaskbarAlignmentAnimation(float translationX) {
995 mTranslationXForTaskbarAlignmentAnimation = translationX;
996 updateTranslation();
997 }
998
999 /**
1000 * Returns translationX value for taskbar to launcher alignment animation
1001 */
1002 public float getTranslationXForTaskbarAlignmentAnimation() {
1003 return mTranslationXForTaskbarAlignmentAnimation;
1004 }
1005
Adam Cohend9162062020-03-24 16:35:35 -07001006 public View getView() {
1007 return this;
1008 }
1009
Adam Cohen65086992020-02-19 08:40:49 -08001010 @Override
1011 public int getViewType() {
1012 return DRAGGABLE_ICON;
1013 }
1014
1015 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -07001016 public void getWorkspaceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001017 getIconBounds(mIconSize, bounds);
Adam Cohen65086992020-02-19 08:40:49 -08001018 }
1019
Adam Cohenc77bc452020-05-07 11:55:19 -07001020 private int getIconSizeForDisplay(int display) {
1021 DeviceProfile grid = mActivity.getDeviceProfile();
1022 switch (display) {
1023 case DISPLAY_ALL_APPS:
1024 return grid.allAppsIconSizePx;
Adam Cohenc77bc452020-05-07 11:55:19 -07001025 case DISPLAY_FOLDER:
Tony Wickham8ac277e2021-05-24 15:47:38 -07001026 return grid.folderChildIconSizePx;
1027 case DISPLAY_WORKSPACE:
Adam Cohenc77bc452020-05-07 11:55:19 -07001028 default:
1029 return grid.iconSizePx;
1030 }
1031 }
1032
1033 public void getSourceVisualDragBounds(Rect bounds) {
Hyunyoung Songc2f024b2021-11-03 23:16:53 -07001034 getIconBounds(mIconSize, bounds);
Adam Cohenc77bc452020-05-07 11:55:19 -07001035 }
1036
Adam Cohen65086992020-02-19 08:40:49 -08001037 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -07001038 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001039 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -08001040 setForceHideDot(true);
Sunny Goyal8b9919d2021-04-07 14:45:17 -07001041 return () -> { };
Adam Cohen65086992020-02-19 08:40:49 -08001042 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001043
1044 private void resetIconScale() {
1045 if (mIcon instanceof FastBitmapDrawable) {
Sunny Goyal572aca42021-03-24 15:21:39 -07001046 ((FastBitmapDrawable) mIcon).resetScale();
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001047 }
1048 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001049
1050 private void updateIcon(Drawable newIcon) {
1051 if (mLayoutHorizontal) {
1052 setCompoundDrawablesRelative(newIcon, null, null, null);
1053 } else {
1054 setCompoundDrawables(null, newIcon, null, null);
1055 }
1056 }
Steven Ng707b1182021-09-13 11:54:58 +01001057
1058 private String getAppLabelPluralString(String appName, int notificationCount) {
1059 MessageFormat icuCountFormat = new MessageFormat(
1060 getResources().getString(R.string.dotted_app_label),
1061 Locale.getDefault());
1062 HashMap<String, Object> args = new HashMap();
1063 args.put("app_name", appName);
1064 args.put("count", notificationCount);
1065 return icuCountFormat.format(args);
1066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067}