blob: eec6e0ad2e56a56602e371677a3421921fca0564 [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 Goyal066ace12018-11-05 11:08:31 -080021import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
22
Tony4043b052018-08-17 13:47:39 -070023import android.animation.Animator;
24import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080025import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070027import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070028import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070030import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070031import android.graphics.Paint;
Adam Cohend9162062020-03-24 16:35:35 -070032import android.graphics.PointF;
Tony Wickham1237df02017-02-24 08:59:36 -080033import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070034import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.drawable.Drawable;
Steven Ng707b1182021-09-13 11:54:58 +010036import android.icu.text.MessageFormat;
Sunny Goyal0d69cb62021-10-04 15:31:38 -070037import android.text.TextPaint;
Sunny Goyal66dccad2018-03-19 12:00:51 -070038import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080039import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080040import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070041import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070042import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080043import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070044import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080045import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080046import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070047
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000048import androidx.annotation.Nullable;
Hyunyoung Song5809a402021-02-02 01:19:18 -080049import androidx.annotation.UiThread;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070050
Adam Cohen29794c52019-06-21 12:50:30 -070051import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Tony Wickhamf34bee82018-12-03 18:11:39 -080052import com.android.launcher3.dot.DotInfo;
Adam Cohen65086992020-02-19 08:40:49 -080053import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070054import com.android.launcher3.folder.FolderIcon;
Tony Wickham1237df02017-02-24 08:59:36 -080055import com.android.launcher3.graphics.IconPalette;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070056import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080057import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080058import com.android.launcher3.icons.DotRenderer;
Sunny Goyal572aca42021-03-24 15:21:39 -070059import com.android.launcher3.icons.FastBitmapDrawable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080060import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal572aca42021-03-24 15:21:39 -070061import com.android.launcher3.icons.PlaceHolderIconDrawable;
Sunny Goyal79e52fc2021-02-03 10:22:28 -080062import com.android.launcher3.icons.cache.HandlerRunnable;
Sunny Goyale396abf2020-04-06 15:11:17 -070063import com.android.launcher3.model.data.AppInfo;
64import com.android.launcher3.model.data.ItemInfo;
65import com.android.launcher3.model.data.ItemInfoWithIcon;
66import com.android.launcher3.model.data.PackageItemInfo;
Samuel Fufac2820182021-01-22 11:47:25 -060067import com.android.launcher3.model.data.SearchActionItemInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070068import com.android.launcher3.model.data.WorkspaceItemInfo;
Samuel Fufa167210a2020-05-12 18:40:11 -070069import com.android.launcher3.util.SafeCloseable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080070import com.android.launcher3.views.ActivityContext;
sfufa@google.comca76de02021-09-24 08:55:48 -070071import com.android.launcher3.views.BubbleTextHolder;
Jon Miranda9b78e192019-08-30 18:42:01 -070072import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070073
Sunny Goyalc469aad2015-10-01 11:24:23 -070074import java.text.NumberFormat;
Steven Ng707b1182021-09-13 11:54:58 +010075import java.util.HashMap;
76import java.util.Locale;
Sunny Goyalc469aad2015-10-01 11:24:23 -070077
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078/**
79 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
80 * because we want to make the bubble taller than the text and TextView's clip is
81 * too aggressive.
82 */
Sunny Goyalb65d7662021-03-07 15:09:11 -080083public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver,
Hyunyoung Songdb9a53d2021-02-17 00:35:10 -080084 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070085
Sunny Goyaldfaccf62015-05-11 16:30:44 -070086 private static final int DISPLAY_WORKSPACE = 0;
87 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070088 private static final int DISPLAY_FOLDER = 2;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -070089 protected static final int DISPLAY_TASKBAR = 5;
90 private static final int DISPLAY_SEARCH_RESULT = 6;
91 private static final int DISPLAY_SEARCH_RESULT_SMALL = 7;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070092
Sunny Goyal0d69cb62021-10-04 15:31:38 -070093 private static final float MIN_LETTER_SPACING = -0.5f;
94 private static final int MAX_SEARCH_LOOP_COUNT = 20;
95
Samuel Fufad6bacdc2020-08-23 21:56:07 -070096 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -070097 private static final float HIGHLIGHT_SCALE = 1.16f;
98
Adam Cohen1d13c0b2020-04-21 16:29:12 -070099 private final PointF mTranslationForReorderBounce = new PointF(0, 0);
100 private final PointF mTranslationForReorderPreview = new PointF(0, 0);
101
Nick Chameyev4dd41592021-08-27 14:11:53 +0100102 private float mTranslationXForTaskbarAlignmentAnimation = 0f;
103
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100104 private final PointF mTranslationForMoveFromCenterAnimation = new PointF(0, 0);
105
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700106 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800107
Tony Wickhamf34bee82018-12-03 18:11:39 -0800108 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
109 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800110 @Override
111 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800112 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800113 }
114
115 @Override
116 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800117 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800118 bubbleTextView.invalidate();
119 }
120 };
121
Sunny Goyaled7a6932018-04-13 11:41:50 -0700122 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
123 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700124 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700125 public Float get(BubbleTextView bubbleTextView) {
126 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700127 }
128
129 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700130 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700131 bubbleTextView.setTextAlpha(alpha);
132 }
133 };
134
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800135 private final ActivityContext mActivity;
Sunny Goyalc4c32692021-05-21 14:23:29 -0700136 private FastBitmapDrawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800137 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700138
Tony Wickham794fe4f2021-01-11 14:54:23 -0600139 protected final int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700140
Sunny Goyaled7a6932018-04-13 11:41:50 -0700141 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700142
143 private final boolean mLayoutHorizontal;
144 private final int mIconSize;
145
146 @ViewDebug.ExportedProperty(category = "launcher")
147 private boolean mIsIconVisible = true;
148 @ViewDebug.ExportedProperty(category = "launcher")
149 private int mTextColor;
150 @ViewDebug.ExportedProperty(category = "launcher")
151 private float mTextAlpha = 1;
152
Tony4043b052018-08-17 13:47:39 -0700153 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800154 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800155 private DotRenderer mDotRenderer;
156 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
Sunny Goyal67555872021-05-21 12:56:55 -0700157 protected DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800158 private Animator mDotScaleAnim;
159 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700160
Sunny Goyal4ffec482016-02-09 11:28:52 -0800161 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800162 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800163 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700164 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800165 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700166 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500167
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800168 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700169
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400170 private boolean mEnableIconUpdateAnimation = false;
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700173 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 }
175
176 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700177 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 }
179
180 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
181 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800182 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700183
Adam Cohen96bb7982014-07-07 11:58:56 -0700184 TypedArray a = context.obtainStyledAttributes(attrs,
185 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800186 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700187 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700188
Samuel Fufac96fa242019-09-26 23:06:32 -0700189 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700190 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700191 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700192 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700193 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700194 defaultIconSize = grid.iconSizePx;
Jon Mirandae126d722021-02-25 10:45:20 -0500195 setCenterVertically(grid.isScalableGrid);
Samuel Fufac96fa242019-09-26 23:06:32 -0700196 } else if (mDisplay == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700197 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
198 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700199 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700200 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700201 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700202 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700203 defaultIconSize = grid.folderChildIconSizePx;
Hyunyoung Songe3d32b52021-06-03 21:20:18 -0700204 } else if (mDisplay == DISPLAY_SEARCH_RESULT) {
205 defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size);
206 } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) {
207 defaultIconSize = getResources().getDimensionPixelSize(
208 R.dimen.search_row_small_icon_size);
Tony Wickham794fe4f2021-01-11 14:54:23 -0600209 } else if (mDisplay == DISPLAY_TASKBAR) {
210 defaultIconSize = grid.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700211 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700212 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700213 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700214 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700215
Winson1f064272016-07-18 17:18:02 -0700216 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700217
218 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
219 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700220 a.recycle();
221
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800222 mLongPressHelper = new CheckLongPressHelper(this);
223
Tony Wickham8912b042018-11-29 15:28:53 -0800224 mDotParams = new DotRenderer.DrawParams();
225
Sunny Goyal66dccad2018-03-19 12:00:51 -0700226 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700227 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700228 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 }
230
Sunny Goyal66dccad2018-03-19 12:00:51 -0700231 @Override
232 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
233 // Disable marques when not focused to that, so that updating text does not cause relayout.
234 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
235 super.onFocusChanged(focused, direction, previouslyFocusedRect);
236 }
237
Jon Miranda52549442017-10-26 11:28:06 -0700238 /**
239 * Resets the view so it can be recycled.
240 */
241 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800242 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800243 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800244 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800245 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800246 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800247 setBackground(null);
Jon Miranda52549442017-10-26 11:28:06 -0700248 }
249
Tony Wickhamf34bee82018-12-03 18:11:39 -0800250 private void cancelDotScaleAnim() {
251 if (mDotScaleAnim != null) {
252 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700253 }
254 }
255
Tony Wickhamf34bee82018-12-03 18:11:39 -0800256 private void animateDotScale(float... dotScales) {
257 cancelDotScaleAnim();
258 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
259 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700260 @Override
261 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800262 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700263 }
264 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800265 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700266 }
267
Hyunyoung Song5809a402021-02-02 01:19:18 -0800268 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700269 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
Tony Wickham175d1522021-08-25 09:18:41 -0700270 applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0);
271 }
272
273 @UiThread
274 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) {
Sunny Goyal95899162019-03-27 16:03:06 -0700275 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700276 }
277
Tony Wickham175d1522021-08-25 09:18:41 -0700278 /**
279 * Returns whether the newInfo differs from the current getTag().
280 */
281 public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) {
282 WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo
283 ? (WorkspaceItemInfo) getTag()
284 : null;
285 boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null
286 && newInfo.getTargetComponent() != null
287 && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent());
288 return changedIcons && isShown();
289 }
290
Adam Cohen29794c52019-06-21 12:50:30 -0700291 @Override
292 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
293 if (delegate instanceof LauncherAccessibilityDelegate) {
294 super.setAccessibilityDelegate(delegate);
295 } else {
296 // NO-OP
297 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
298 // delegate incorrectly. There are no cases when we shouldn't be using the
299 // LauncherAccessibilityDelegate for BubbleTextView.
300 }
301 }
302
Hyunyoung Song5809a402021-02-02 01:19:18 -0800303 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700304 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800305 applyIconAndLabel(info);
sfufa@google.comca76de02021-09-24 08:55:48 -0700306 setItemInfo(info);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000307 applyLoadingState(promiseStateChanged);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800308 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800309 setDownloadStateContentDescription(info, info.getProgressLevel());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800310 }
311
Hyunyoung Song5809a402021-02-02 01:19:18 -0800312 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700313 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800314 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700315
Sunny Goyal95899162019-03-27 16:03:06 -0700316 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700317 setItemInfo(info);
318
Sunny Goyal34b65272015-03-11 16:56:52 -0700319
320 // Verify high res immediately
321 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800322
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000323 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800324 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700325 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800326 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800327 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal508da152014-08-14 10:53:27 -0700328 }
329
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700330 /**
331 * Apply label and tag using a generic {@link ItemInfoWithIcon}
332 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800333 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700334 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800335 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700336 // We don't need to check the info since it's not a WorkspaceItemInfo
sfufa@google.comca76de02021-09-24 08:55:48 -0700337 setItemInfo(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700338
339 // Verify high res immediately
340 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800341
342 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700343 }
344
sfufa@google.comca76de02021-09-24 08:55:48 -0700345 private void setItemInfo(ItemInfo itemInfo) {
346 setTag(itemInfo);
347 if (getParent() instanceof BubbleTextHolder) {
348 ((BubbleTextHolder) getParent()).onItemInfoChanged(itemInfo);
349 }
Samuel Fufa02be2a42020-09-23 00:45:27 -0700350 }
351
Hyunyoung Song5809a402021-02-02 01:19:18 -0800352 @UiThread
Samuel Fufac2820182021-01-22 11:47:25 -0600353 protected void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal59f19152021-08-03 12:25:02 -0700354 boolean useTheme = mDisplay == DISPLAY_WORKSPACE || mDisplay == DISPLAY_FOLDER
355 || mDisplay == DISPLAY_TASKBAR;
Sunny Goyal9ab43772021-04-23 11:40:58 -0700356 FastBitmapDrawable iconDrawable = info.newIcon(getContext(), useTheme);
Sunny Goyal67555872021-05-21 12:56:55 -0700357 mDotParams.color = IconPalette.getMutedColor(iconDrawable.getIconColor(), 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800358
Sunny Goyalf4204382016-07-13 10:46:07 -0700359 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000360 applyLabel(info);
361 }
362
Hyunyoung Song5809a402021-02-02 01:19:18 -0800363 @UiThread
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000364 private void applyLabel(ItemInfoWithIcon info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700365 setText(info.title);
366 if (info.contentDescription != null) {
367 setContentDescription(info.isDisabled()
368 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
369 : info.contentDescription);
370 }
371 }
372
Winson Chung7501adf2015-06-02 11:24:28 -0700373 /**
374 * Overrides the default long press timeout.
375 */
Tony2ca999c2018-09-24 17:24:51 -0400376 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
377 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700378 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800381 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700382 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800383 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800384 }
Sunny Goyal508da152014-08-14 10:53:27 -0700385 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800386
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800387 @Override
388 protected int[] onCreateDrawableState(int extraSpace) {
389 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
390 if (mStayPressed) {
391 mergeDrawableStates(drawableState, STATE_PRESSED);
392 }
393 return drawableState;
394 }
395
Winson Chungb745afb2015-03-02 11:51:23 -0800396 /** Returns the icon for this view. */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700397 public FastBitmapDrawable getIcon() {
Winson Chungb745afb2015-03-02 11:51:23 -0800398 return mIcon;
399 }
400
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700401 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800402 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700403 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700404 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700405 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700406 return false;
407 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700408 if (isLongClickable()) {
409 super.onTouchEvent(event);
410 mLongPressHelper.onTouchEvent(event);
411 // Keep receiving the rest of the events
412 return true;
413 } else {
414 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700415 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800416 }
417
Sunny Goyal44a3b202020-07-09 08:27:06 -0700418 /**
419 * Returns true if the touch down at the provided position be ignored
420 */
421 protected boolean shouldIgnoreTouchDown(float x, float y) {
Tony Wickham42b03952021-08-31 10:41:36 -0700422 if (mDisplay == DISPLAY_TASKBAR) {
423 // Allow touching within padding on taskbar, given icon sizes are smaller.
424 return false;
425 }
Sunny Goyal44a3b202020-07-09 08:27:06 -0700426 return y < getPaddingTop()
427 || x < getPaddingLeft()
428 || y > getHeight() - getPaddingBottom()
429 || x > getWidth() - getPaddingRight();
430 }
431
Michael Jurkaddd62e92011-02-16 17:49:14 -0800432 void setStayPressed(boolean stayPressed) {
433 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800434 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800435 }
436
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700437 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800438 public void onVisibilityAggregated(boolean isVisible) {
439 super.onVisibilityAggregated(isVisible);
440 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800441 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800442 }
443 }
444
Tony Wickham8ac277e2021-05-24 15:47:38 -0700445 public void clearPressedBackground() {
Sunny Goyal508da152014-08-14 10:53:27 -0700446 setPressed(false);
447 setStayPressed(false);
448 }
449
450 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700451 public boolean onKeyUp(int keyCode, KeyEvent event) {
452 // Unlike touch events, keypress event propagate pressed state change immediately,
453 // without waiting for onClickHandler to execute. Disable pressed state changes here
454 // to avoid flickering.
455 mIgnorePressedStateChange = true;
456 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700457 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800458 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700459 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800460 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800461
Sunny Goyal0d69cb62021-10-04 15:31:38 -0700462 @Override
463 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
464 super.onSizeChanged(w, h, oldw, oldh);
465 checkForEllipsis();
466 }
467
468 @Override
469 protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
470 super.onTextChanged(text, start, lengthBefore, lengthAfter);
471 checkForEllipsis();
472 }
473
474 private void checkForEllipsis() {
475 if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) {
476 return;
477 }
478 float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
479 if (width <= 0) {
480 return;
481 }
482 setLetterSpacing(0);
483
484 String text = getText().toString();
485 TextPaint paint = getPaint();
486 if (paint.measureText(text) < width) {
487 return;
488 }
489
490 float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING);
491 // Reset the paint value so that the call to TextView does appropriate diff.
492 paint.setLetterSpacing(0);
493 setLetterSpacing(spacing);
494 }
495
496 /**
497 * Find the appropriate text spacing to display the provided text
498 * @param paint the paint used by the text view
499 * @param text the text to display
500 * @param allowedWidthPx available space to render the text
501 * @param minSpacingEm minimum spacing allowed between characters
502 * @return the final textSpacing value
503 *
504 * @see #setLetterSpacing(float)
505 */
506 private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx,
507 float minSpacingEm) {
508 paint.setLetterSpacing(minSpacingEm);
509 if (paint.measureText(text) > allowedWidthPx) {
510 // If there is no result at high limit, we can do anything more
511 return minSpacingEm;
512 }
513
514 float lowLimit = 0;
515 float highLimit = minSpacingEm;
516
517 for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) {
518 float value = (lowLimit + highLimit) / 2;
519 paint.setLetterSpacing(value);
520 if (paint.measureText(text) < allowedWidthPx) {
521 highLimit = value;
522 } else {
523 lowLimit = value;
524 }
525 }
526
527 // At the end error on the higher side
528 return highLimit;
529 }
530
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700531 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800532 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700533 super.onDraw(canvas);
534 }
535
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700537 public void onDraw(Canvas canvas) {
538 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800539 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700540 }
541
Tony Wickham1237df02017-02-24 08:59:36 -0800542 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800543 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700544 *
Tony Wickham1237df02017-02-24 08:59:36 -0800545 * @param canvas The canvas to draw to.
546 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800547 protected void drawDotIfNecessary(Canvas canvas) {
548 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800549 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700550 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
551 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800552 final int scrollX = getScrollX();
553 final int scrollY = getScrollY();
554 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800555 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800556 canvas.translate(-scrollX, -scrollY);
557 }
558 }
559
Jon Miranda9b78e192019-08-30 18:42:01 -0700560 @Override
561 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800562 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800563 return;
564 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800565 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800566
Tony Wickhamf34bee82018-12-03 18:11:39 -0800567 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800568 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800569 } else if (hasDot()) {
570 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800571 }
572 }
573
Tony Wickhamf34bee82018-12-03 18:11:39 -0800574 private boolean hasDot() {
575 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800576 }
577
578 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700579 getIconBounds(this, outBounds, mIconSize);
580 }
581
582 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
583 int top = iconView.getPaddingTop();
584 int left = (iconView.getWidth() - iconSize) / 2;
585 int right = left + iconSize;
586 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800587 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400589
Jon Miranda228877d2021-02-09 11:05:00 -0500590
591 /**
592 * Sets whether to vertically center the content.
593 */
594 public void setCenterVertically(boolean centerVertically) {
595 mCenterVertically = centerVertically;
596 }
597
Joe Onorato9c1289c2009-08-17 11:03:03 -0400598 @Override
Winson1f064272016-07-18 17:18:02 -0700599 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
600 if (mCenterVertically) {
601 Paint.FontMetrics fm = getPaint().getFontMetrics();
602 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
603 (int) Math.ceil(fm.bottom - fm.top);
604 int height = MeasureSpec.getSize(heightMeasureSpec);
605 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
606 getPaddingBottom());
607 }
608 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
609 }
610
611 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700612 public void setTextColor(int color) {
613 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700614 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700615 }
616
Adam Cohen96bb7982014-07-07 11:58:56 -0700617 @Override
618 public void setTextColor(ColorStateList colors) {
619 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700620 if (Float.compare(mTextAlpha, 1) == 0) {
621 super.setTextColor(colors);
622 } else {
623 super.setTextColor(getModifiedColor());
624 }
Adam Cohen477828c2013-09-20 12:05:49 -0700625 }
626
Tony7308cde2017-06-27 22:38:33 -0700627 public boolean shouldTextBeVisible() {
628 // Text should be visible everywhere but the hotseat.
629 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
630 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700631 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
632 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700633 }
634
Winson Chung5f8afe62013-08-12 16:19:28 -0700635 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700636 setTextAlpha(visible ? 1 : 0);
637 }
638
639 private void setTextAlpha(float alpha) {
640 mTextAlpha = alpha;
641 super.setTextColor(getModifiedColor());
642 }
643
644 private int getModifiedColor() {
645 if (mTextAlpha == 0) {
646 // Special case to prevent text shadows in high contrast mode
647 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700648 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800649 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700650 }
651
652 /**
653 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700654 *
Tony8f402802017-06-16 17:24:54 -0700655 * @param fadeIn Whether the text should fade in or fade out.
656 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700657 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700658 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700659 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700660 }
661
Winson Chungaffd7b42010-08-20 15:11:56 -0700662 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800663 public void cancelLongPress() {
664 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800665 mLongPressHelper.cancelLongPress();
666 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500667
Samuel Fufac2820182021-01-22 11:47:25 -0600668 /**
669 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000670 *
671 * If this app is installing, the progress bar will be updated with the installation progress.
672 * If this app is installed and downloading incrementally, the progress bar will be updated
673 * with the total download progress.
674 */
675 public void applyLoadingState(boolean promiseStateChanged) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800676 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700677 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000678 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE)
679 != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800680 updateProgressBarUi(info.getProgressLevel() == 100);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000681 } else if (info.hasPromiseIconUi() || (info.runtimeStatusFlags
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800682 & ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE) != 0) {
683 updateProgressBarUi(promiseStateChanged);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700684 }
685 }
686 }
687
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800688 private void updateProgressBarUi(boolean maybePerformFinishedAnimation) {
689 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000690 if (preloadDrawable != null && maybePerformFinishedAnimation) {
691 preloadDrawable.maybePerformFinishedAnimation();
692 }
693 }
694
695 /** Applies the given progress level to the this icon's progress bar. */
696 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800697 public PreloadIconDrawable applyProgressLevel() {
698 if (!(getTag() instanceof ItemInfoWithIcon)) {
699 return null;
700 }
701
702 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
703 int progressLevel = info.getProgressLevel();
704 if (progressLevel >= 100) {
705 setContentDescription(info.contentDescription != null
706 ? info.contentDescription : "");
707 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800708 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800709 } else {
710 setContentDescription(getContext()
711 .getString(R.string.app_waiting_download_title, info.title));
712 }
713 if (mIcon != null) {
714 PreloadIconDrawable preloadIconDrawable;
715 if (mIcon instanceof PreloadIconDrawable) {
716 preloadIconDrawable = (PreloadIconDrawable) mIcon;
717 preloadIconDrawable.setLevel(progressLevel);
718 preloadIconDrawable.setIsDisabled(!info.isAppStartable());
Mario Bertschler230612f2017-09-27 17:05:21 -0700719 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800720 preloadIconDrawable = makePreloadIcon();
721 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700722 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800723 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400724 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700725 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400726 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700727
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800728 /**
729 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
730 * object.
731 */
732 @Nullable
733 public PreloadIconDrawable makePreloadIcon() {
734 if (!(getTag() instanceof ItemInfoWithIcon)) {
735 return null;
736 }
737
738 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
739 int progressLevel = info.getProgressLevel();
740 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
741
742 preloadDrawable.setLevel(progressLevel);
743 preloadDrawable.setIsDisabled(!info.isAppStartable());
744
745 return preloadDrawable;
746 }
747
Tony Wickhamf34bee82018-12-03 18:11:39 -0800748 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800749 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800750 boolean wasDotted = mDotInfo != null;
751 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
752 boolean isDotted = mDotInfo != null;
753 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700754 if (mDisplay == DISPLAY_ALL_APPS) {
755 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
756 } else {
757 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
758 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800759 if (wasDotted || isDotted) {
760 // Animate when a dot is first added or when it is removed.
761 if (animate && (wasDotted ^ isDotted) && isShown()) {
762 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800763 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800764 cancelDotScaleAnim();
765 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800766 invalidate();
767 }
768 }
Tony Wickham305e9202018-01-23 17:46:47 -0800769 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700770 if (itemInfo.isDisabled()) {
771 setContentDescription(getContext().getString(R.string.disabled_app_label,
772 itemInfo.contentDescription));
773 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800774 int count = mDotInfo.getNotificationCount();
Steven Ng707b1182021-09-13 11:54:58 +0100775 setContentDescription(
776 getAppLabelPluralString(itemInfo.contentDescription.toString(), count));
Tony Wickham305e9202018-01-23 17:46:47 -0800777 } else {
778 setContentDescription(itemInfo.contentDescription);
779 }
780 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800781 }
782 }
783
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800784 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
785 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
786 != 0) {
787 String percentageString = NumberFormat.getPercentInstance()
788 .format(progressLevel * 0.01);
789 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE) != 0) {
790 setContentDescription(getContext()
791 .getString(
792 R.string.app_installing_title, info.title, percentageString));
793 } else if ((info.runtimeStatusFlags
794 & ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
795 setContentDescription(getContext()
796 .getString(
797 R.string.app_downloading_title, info.title, percentageString));
798 }
799 }
800 }
801
Winson Chungb745afb2015-03-02 11:51:23 -0800802 /**
803 * Sets the icon for this view based on the layout direction.
804 */
Sunny Goyalc4c32692021-05-21 14:23:29 -0700805 protected void setIcon(FastBitmapDrawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700806 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700807 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700808 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700809 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800810 if (mIcon != null) {
811 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
812 }
Tony17b7f9b2017-05-23 12:19:09 -0700813 }
814
Jon Miranda9b78e192019-08-30 18:42:01 -0700815 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700816 public void setIconVisible(boolean visible) {
817 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700818 if (!mIsIconVisible) {
819 resetIconScale();
820 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700821 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700822 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700823 }
824
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400825 protected boolean iconUpdateAnimationEnabled() {
826 return mEnableIconUpdateAnimation;
827 }
828
Tony Wickham377ed3f2016-07-20 15:21:04 -0700829 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700830 // If we had already set an icon before, disable relayout as the icon size is the
831 // same as before.
832 mDisableRelayout = mIcon != null;
833
834 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700835
836 updateIcon(icon);
837
838 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -0400839 if (mIcon != null
840 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400841 && iconUpdateAnimationEnabled()) {
Steven Nge92bc552021-02-10 17:10:15 +0000842 ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon);
Winson Chungb745afb2015-03-02 11:51:23 -0800843 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700844
Sunny Goyal66dccad2018-03-19 12:00:51 -0700845 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800846 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700847
Sunny Goyal69b75642015-05-15 17:00:24 -0700848 @Override
849 public void requestLayout() {
850 if (!mDisableRelayout) {
851 super.requestLayout();
852 }
853 }
854
Sunny Goyal34b65272015-03-11 16:56:52 -0700855 /**
856 * Applies the item info if it is same as what the view is pointing to currently.
857 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800858 @Override
859 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700860 if (getTag() == info) {
861 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700862 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400863 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -0700864
Jon Miranda7f522a22017-07-28 11:56:47 -0700865 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -0700866 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -0700867
Sunny Goyal34b65272015-03-11 16:56:52 -0700868 if (info instanceof AppInfo) {
869 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700870 } else if (info instanceof WorkspaceItemInfo) {
871 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700872 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700873 } else if (info instanceof PackageItemInfo) {
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700874 applyFromItemInfoWithIcon((PackageItemInfo) info);
Samuel Fufac2820182021-01-22 11:47:25 -0600875 } else if (info instanceof SearchActionItemInfo) {
sfufa@google.comca76de02021-09-24 08:55:48 -0700876 applyFromItemInfoWithIcon((SearchActionItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700877 }
Winsonc0880492015-08-21 11:16:27 -0700878
Sunny Goyal69b75642015-05-15 17:00:24 -0700879 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400880 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700881 }
882 }
883
884 /**
885 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
886 */
887 public void verifyHighRes() {
888 if (mIconLoadRequest != null) {
889 mIconLoadRequest.cancel();
890 mIconLoadRequest = null;
891 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800892 if (getTag() instanceof ItemInfoWithIcon) {
893 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700894 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800895 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700896 .updateIconInBackground(BubbleTextView.this, info);
897 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700898 }
899 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700900
Jon Miranda47170112017-03-03 14:57:14 -0800901 public int getIconSize() {
902 return mIconSize;
903 }
Adam Cohen65086992020-02-19 08:40:49 -0800904
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700905 private void updateTranslation() {
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100906 super.setTranslationX(mTranslationForReorderBounce.x + mTranslationForReorderPreview.x
Nick Chameyev4dd41592021-08-27 14:11:53 +0100907 + mTranslationForMoveFromCenterAnimation.x
908 + mTranslationXForTaskbarAlignmentAnimation);
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100909 super.setTranslationY(mTranslationForReorderBounce.y + mTranslationForReorderPreview.y
910 + mTranslationForMoveFromCenterAnimation.y);
Adam Cohend9162062020-03-24 16:35:35 -0700911 }
912
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700913 public void setReorderBounceOffset(float x, float y) {
914 mTranslationForReorderBounce.set(x, y);
915 updateTranslation();
Adam Cohend9162062020-03-24 16:35:35 -0700916 }
917
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700918 public void getReorderBounceOffset(PointF offset) {
919 offset.set(mTranslationForReorderBounce);
920 }
921
922 @Override
923 public void setReorderPreviewOffset(float x, float y) {
924 mTranslationForReorderPreview.set(x, y);
925 updateTranslation();
926 }
927
928 @Override
929 public void getReorderPreviewOffset(PointF offset) {
930 offset.set(mTranslationForReorderPreview);
931 }
932
933 public void setReorderBounceScale(float scale) {
934 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -0700935 super.setScaleX(scale);
936 super.setScaleY(scale);
937 }
938
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700939 public float getReorderBounceScale() {
940 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -0700941 }
942
Nick Chameyev4dd41592021-08-27 14:11:53 +0100943 /**
944 * Sets translation values for move from center animation
945 */
Nick Chameyev5e8dbe72021-07-26 14:56:50 +0100946 public void setTranslationForMoveFromCenterAnimation(float x, float y) {
947 mTranslationForMoveFromCenterAnimation.set(x, y);
948 updateTranslation();
949 }
950
Nick Chameyev4dd41592021-08-27 14:11:53 +0100951 /**
952 * Sets translationX for taskbar to launcher alignment animation
953 */
954 public void setTranslationXForTaskbarAlignmentAnimation(float translationX) {
955 mTranslationXForTaskbarAlignmentAnimation = translationX;
956 updateTranslation();
957 }
958
959 /**
960 * Returns translationX value for taskbar to launcher alignment animation
961 */
962 public float getTranslationXForTaskbarAlignmentAnimation() {
963 return mTranslationXForTaskbarAlignmentAnimation;
964 }
965
Adam Cohend9162062020-03-24 16:35:35 -0700966 public View getView() {
967 return this;
968 }
969
Adam Cohen65086992020-02-19 08:40:49 -0800970 @Override
971 public int getViewType() {
972 return DRAGGABLE_ICON;
973 }
974
975 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -0700976 public void getWorkspaceVisualDragBounds(Rect bounds) {
Adam Cohen65086992020-02-19 08:40:49 -0800977 DeviceProfile grid = mActivity.getDeviceProfile();
978 BubbleTextView.getIconBounds(this, bounds, grid.iconSizePx);
979 }
980
Adam Cohenc77bc452020-05-07 11:55:19 -0700981 private int getIconSizeForDisplay(int display) {
982 DeviceProfile grid = mActivity.getDeviceProfile();
983 switch (display) {
984 case DISPLAY_ALL_APPS:
985 return grid.allAppsIconSizePx;
Adam Cohenc77bc452020-05-07 11:55:19 -0700986 case DISPLAY_FOLDER:
Tony Wickham8ac277e2021-05-24 15:47:38 -0700987 return grid.folderChildIconSizePx;
988 case DISPLAY_WORKSPACE:
Adam Cohenc77bc452020-05-07 11:55:19 -0700989 default:
990 return grid.iconSizePx;
991 }
992 }
993
994 public void getSourceVisualDragBounds(Rect bounds) {
995 BubbleTextView.getIconBounds(this, bounds, getIconSizeForDisplay(mDisplay));
996 }
997
Adam Cohen65086992020-02-19 08:40:49 -0800998 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -0700999 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001000 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -08001001 setForceHideDot(true);
Sunny Goyal8b9919d2021-04-07 14:45:17 -07001002 return () -> { };
Adam Cohen65086992020-02-19 08:40:49 -08001003 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001004
1005 private void resetIconScale() {
1006 if (mIcon instanceof FastBitmapDrawable) {
Sunny Goyal572aca42021-03-24 15:21:39 -07001007 ((FastBitmapDrawable) mIcon).resetScale();
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001008 }
1009 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -07001010
1011 private void updateIcon(Drawable newIcon) {
1012 if (mLayoutHorizontal) {
1013 setCompoundDrawablesRelative(newIcon, null, null, null);
1014 } else {
1015 setCompoundDrawables(null, newIcon, null, null);
1016 }
1017 }
Steven Ng707b1182021-09-13 11:54:58 +01001018
1019 private String getAppLabelPluralString(String appName, int notificationCount) {
1020 MessageFormat icuCountFormat = new MessageFormat(
1021 getResources().getString(R.string.dotted_app_label),
1022 Locale.getDefault());
1023 HashMap<String, Object> args = new HashMap();
1024 args.put("app_name", appName);
1025 args.put("count", notificationCount);
1026 return icuCountFormat.format(args);
1027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028}