blob: f44f88b098db041d1edaed6106fe743064ba938e [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 Goyal14168432019-10-24 15:59:49 -070019import static com.android.launcher3.FastBitmapDrawable.newIcon;
Samuel Fufa314761a2020-10-14 10:15:07 -070020import static com.android.launcher3.graphics.IconShape.getShape;
Sunny Goyal14168432019-10-24 15:59:49 -070021import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon;
Sunny Goyal066ace12018-11-05 11:08:31 -080022import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
23
Tony4043b052018-08-17 13:47:39 -070024import android.animation.Animator;
25import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080026import android.animation.ObjectAnimator;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070027import android.animation.ValueAnimator;
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;
Samuel Fufa314761a2020-10-14 10:15:07 -070031import android.graphics.BlurMaskFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070033import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070034import android.graphics.Paint;
Samuel Fufa314761a2020-10-14 10:15:07 -070035import android.graphics.Path;
Adam Cohend9162062020-03-24 16:35:35 -070036import android.graphics.PointF;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070037import android.graphics.PorterDuff.Mode;
38import android.graphics.PorterDuffColorFilter;
Tony Wickham1237df02017-02-24 08:59:36 -080039import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070040import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.drawable.Drawable;
Samuel Fufa314761a2020-10-14 10:15:07 -070042import android.os.Process;
Sunny Goyal66dccad2018-03-19 12:00:51 -070043import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080044import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080045import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070046import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070047import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080048import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070049import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080050import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080051import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070052
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000053import androidx.annotation.Nullable;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070054import androidx.core.graphics.ColorUtils;
55
Sunny Goyal3dce5f32017-10-05 11:40:05 -070056import com.android.launcher3.Launcher.OnResumeCallback;
Adam Cohen29794c52019-06-21 12:50:30 -070057import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Samuel Fufa314761a2020-10-14 10:15:07 -070058import com.android.launcher3.allapps.AllAppsSectionDecorator;
59import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf34bee82018-12-03 18:11:39 -080060import com.android.launcher3.dot.DotInfo;
Adam Cohen65086992020-02-19 08:40:49 -080061import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070062import com.android.launcher3.folder.FolderIcon;
Tony Wickham1237df02017-02-24 08:59:36 -080063import com.android.launcher3.graphics.IconPalette;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070064import com.android.launcher3.graphics.IconShape;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070065import com.android.launcher3.graphics.PlaceHolderIconDrawable;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080066import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080067import com.android.launcher3.icons.DotRenderer;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080068import com.android.launcher3.icons.IconCache.IconLoadRequest;
69import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Samuel Fufa314761a2020-10-14 10:15:07 -070070import com.android.launcher3.icons.LauncherIcons;
Sunny Goyale396abf2020-04-06 15:11:17 -070071import com.android.launcher3.model.data.AppInfo;
72import com.android.launcher3.model.data.ItemInfo;
73import com.android.launcher3.model.data.ItemInfoWithIcon;
74import com.android.launcher3.model.data.PackageItemInfo;
Samuel Fufa02be2a42020-09-23 00:45:27 -070075import com.android.launcher3.model.data.RemoteActionItemInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070076import com.android.launcher3.model.data.WorkspaceItemInfo;
Samuel Fufa167210a2020-05-12 18:40:11 -070077import com.android.launcher3.util.SafeCloseable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080078import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070079import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070080
Sunny Goyalc469aad2015-10-01 11:24:23 -070081import java.text.NumberFormat;
82
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083/**
84 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
85 * because we want to make the bubble taller than the text and TextView's clip is
86 * too aggressive.
87 */
Jon Miranda9b78e192019-08-30 18:42:01 -070088public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback,
Samuel Fufa314761a2020-10-14 10:15:07 -070089 IconLabelDotView, DraggableView, Reorderable, AllAppsSectionDecorator.SelfDecoratingView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070090
Sunny Goyaldfaccf62015-05-11 16:30:44 -070091 private static final int DISPLAY_WORKSPACE = 0;
92 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070093 private static final int DISPLAY_FOLDER = 2;
Samuel Fufa4a53c702020-08-04 14:06:55 -070094 private static final int DISPLAY_HERO_APP = 5;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070095
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
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080099
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700100 private final PointF mTranslationForReorderBounce = new PointF(0, 0);
101 private final PointF mTranslationForReorderPreview = new PointF(0, 0);
102
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700103 private static final int ICON_UPDATE_ANIMATION_DURATION = 375;
104
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700105 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800106
Samuel Fufa314761a2020-10-14 10:15:07 -0700107 protected final Paint mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
108 private final Path mHighlightPath = new Path();
109 protected int mHighlightColor = Color.TRANSPARENT;
110 private final BlurMaskFilter mHighlightShadowFilter;
111
Tony Wickhamf34bee82018-12-03 18:11:39 -0800112 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
113 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800114 @Override
115 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800116 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800117 }
118
119 @Override
120 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800121 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800122 bubbleTextView.invalidate();
123 }
124 };
125
Sunny Goyaled7a6932018-04-13 11:41:50 -0700126 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
127 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700128 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700129 public Float get(BubbleTextView bubbleTextView) {
130 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700131 }
132
133 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700134 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700135 bubbleTextView.setTextAlpha(alpha);
136 }
137 };
138
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800139 private final ActivityContext mActivity;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700140 private Drawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800141 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700142
Samuel Fufac96fa242019-09-26 23:06:32 -0700143 private final int mDisplay;
144
Sunny Goyaled7a6932018-04-13 11:41:50 -0700145 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700146
147 private final boolean mLayoutHorizontal;
148 private final int mIconSize;
149
150 @ViewDebug.ExportedProperty(category = "launcher")
151 private boolean mIsIconVisible = true;
152 @ViewDebug.ExportedProperty(category = "launcher")
153 private int mTextColor;
154 @ViewDebug.ExportedProperty(category = "launcher")
155 private float mTextAlpha = 1;
156
Tony4043b052018-08-17 13:47:39 -0700157 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800158 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800159 private DotRenderer mDotRenderer;
160 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
161 private DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800162 private Animator mDotScaleAnim;
163 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700164
Sunny Goyal4ffec482016-02-09 11:28:52 -0800165 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800166 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800167 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700168 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800169 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700170 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500171
Sunny Goyal34b65272015-03-11 16:56:52 -0700172 private IconLoadRequest mIconLoadRequest;
173
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400174 private boolean mEnableIconUpdateAnimation = false;
175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700177 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 }
179
180 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700181 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 }
183
184 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
185 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800186 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700187
Adam Cohen96bb7982014-07-07 11:58:56 -0700188 TypedArray a = context.obtainStyledAttributes(attrs,
189 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800190 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700191 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700192
Samuel Fufac96fa242019-09-26 23:06:32 -0700193 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700194 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700195 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700196 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700197 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700198 defaultIconSize = grid.iconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700199 } else if (mDisplay == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700200 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
201 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700202 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700203 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700204 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700205 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700206 defaultIconSize = grid.folderChildIconSizePx;
Samuel Fufa4a53c702020-08-04 14:06:55 -0700207 } else if (mDisplay == DISPLAY_HERO_APP) {
208 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700209 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700210 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700211 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700212 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700213
Winson1f064272016-07-18 17:18:02 -0700214 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700215
216 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
217 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700218 a.recycle();
219
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800220 mLongPressHelper = new CheckLongPressHelper(this);
221
Tony Wickham8912b042018-11-29 15:28:53 -0800222 mDotParams = new DotRenderer.DrawParams();
223
Sunny Goyal66dccad2018-03-19 12:00:51 -0700224 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700225 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700226 setTextAlpha(1f);
Samuel Fufa314761a2020-10-14 10:15:07 -0700227
228 int shadowSize = context.getResources().getDimensionPixelSize(
229 R.dimen.blur_size_click_shadow);
230 mHighlightShadowFilter = new BlurMaskFilter(shadowSize, BlurMaskFilter.Blur.INNER);
231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 }
233
Sunny Goyal66dccad2018-03-19 12:00:51 -0700234 @Override
235 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
236 // Disable marques when not focused to that, so that updating text does not cause relayout.
237 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
238 super.onFocusChanged(focused, direction, previouslyFocusedRect);
239 }
240
Jon Miranda52549442017-10-26 11:28:06 -0700241 /**
242 * Resets the view so it can be recycled.
243 */
244 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800245 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800246 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800247 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800248 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800249 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800250 setBackground(null);
Jon Miranda52549442017-10-26 11:28:06 -0700251 }
252
Tony Wickhamf34bee82018-12-03 18:11:39 -0800253 private void cancelDotScaleAnim() {
254 if (mDotScaleAnim != null) {
255 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700256 }
257 }
258
Tony Wickhamf34bee82018-12-03 18:11:39 -0800259 private void animateDotScale(float... dotScales) {
260 cancelDotScaleAnim();
261 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
262 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700263 @Override
264 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800265 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700266 }
267 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800268 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700269 }
270
Sunny Goyal95899162019-03-27 16:03:06 -0700271 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
272 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700273 }
274
Adam Cohen29794c52019-06-21 12:50:30 -0700275 @Override
276 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
277 if (delegate instanceof LauncherAccessibilityDelegate) {
278 super.setAccessibilityDelegate(delegate);
279 } else {
280 // NO-OP
281 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
282 // delegate incorrectly. There are no cases when we shouldn't be using the
283 // LauncherAccessibilityDelegate for BubbleTextView.
284 }
285 }
286
Sunny Goyal95899162019-03-27 16:03:06 -0700287 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800288 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800289 setTag(info);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000290 applyLoadingState(promiseStateChanged);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800291 applyDotState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800292 }
293
Sunny Goyal508da152014-08-14 10:53:27 -0700294 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800295 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700296
Sunny Goyal95899162019-03-27 16:03:06 -0700297 // We don't need to check the info since it's not a WorkspaceItemInfo
Winson Chung888b3a12015-03-13 11:14:16 -0700298 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700299
300 // Verify high res immediately
301 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800302
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000303 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
304 applyProgressLevel(info.getProgressLevel());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700305 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800306 applyDotState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700307 }
308
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700309 /**
310 * Apply label and tag using a generic {@link ItemInfoWithIcon}
311 */
312 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800313 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700314 // We don't need to check the info since it's not a WorkspaceItemInfo
Sunny Goyal0e08f162015-05-12 11:32:39 -0700315 super.setTag(info);
316
317 // Verify high res immediately
318 verifyHighRes();
319 }
320
Samuel Fufa02be2a42020-09-23 00:45:27 -0700321 /**
322 * Apply label and tag using a {@link RemoteActionItemInfo}
323 */
324 public void applyFromRemoteActionInfo(RemoteActionItemInfo remoteActionItemInfo) {
325 applyIconAndLabel(remoteActionItemInfo);
326 setTag(remoteActionItemInfo);
327 }
328
Sunny Goyal179249d2017-12-19 16:49:24 -0800329 private void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal14168432019-10-24 15:59:49 -0700330 FastBitmapDrawable iconDrawable = newIcon(getContext(), info);
Sunny Goyal3808a692019-10-25 13:41:28 -0700331 mDotParams.color = IconPalette.getMutedColor(info.bitmap.color, 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800332
Sunny Goyalf4204382016-07-13 10:46:07 -0700333 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000334 applyLabel(info);
335 }
336
337 private void applyLabel(ItemInfoWithIcon info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700338 setText(info.title);
339 if (info.contentDescription != null) {
340 setContentDescription(info.isDisabled()
341 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
342 : info.contentDescription);
343 }
344 }
345
Winson Chung7501adf2015-06-02 11:24:28 -0700346 /**
347 * Overrides the default long press timeout.
348 */
Tony2ca999c2018-09-24 17:24:51 -0400349 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
350 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700351 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800354 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700355 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800356 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800357 }
Sunny Goyal508da152014-08-14 10:53:27 -0700358 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800359
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800360 @Override
361 protected int[] onCreateDrawableState(int extraSpace) {
362 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
363 if (mStayPressed) {
364 mergeDrawableStates(drawableState, STATE_PRESSED);
365 }
366 return drawableState;
367 }
368
Winson Chungb745afb2015-03-02 11:51:23 -0800369 /** Returns the icon for this view. */
370 public Drawable getIcon() {
371 return mIcon;
372 }
373
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700374 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800375 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700376 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700377 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700378 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700379 return false;
380 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700381 if (isLongClickable()) {
382 super.onTouchEvent(event);
383 mLongPressHelper.onTouchEvent(event);
384 // Keep receiving the rest of the events
385 return true;
386 } else {
387 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700388 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800389 }
390
Sunny Goyal44a3b202020-07-09 08:27:06 -0700391 /**
392 * Returns true if the touch down at the provided position be ignored
393 */
394 protected boolean shouldIgnoreTouchDown(float x, float y) {
395 return y < getPaddingTop()
396 || x < getPaddingLeft()
397 || y > getHeight() - getPaddingBottom()
398 || x > getWidth() - getPaddingRight();
399 }
400
Michael Jurkaddd62e92011-02-16 17:49:14 -0800401 void setStayPressed(boolean stayPressed) {
402 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800403 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800404 }
405
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700406 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800407 public void onVisibilityAggregated(boolean isVisible) {
408 super.onVisibilityAggregated(isVisible);
409 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800410 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800411 }
412 }
413
414 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700415 public void onLauncherResume() {
416 // Reset the pressed state of icon that was locked in the press state while activity
417 // was launching
418 setStayPressed(false);
419 }
420
Sunny Goyal508da152014-08-14 10:53:27 -0700421 void clearPressedBackground() {
422 setPressed(false);
423 setStayPressed(false);
424 }
425
426 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700427 public boolean onKeyUp(int keyCode, KeyEvent event) {
428 // Unlike touch events, keypress event propagate pressed state change immediately,
429 // without waiting for onClickHandler to execute. Disable pressed state changes here
430 // to avoid flickering.
431 mIgnorePressedStateChange = true;
432 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700433 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800434 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700435 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800436 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800437
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700438 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800439 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700440 super.onDraw(canvas);
441 }
442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700444 public void onDraw(Canvas canvas) {
Samuel Fufa314761a2020-10-14 10:15:07 -0700445 if (FeatureFlags.ENABLE_DEVICE_SEARCH.get() && mHighlightColor != Color.TRANSPARENT) {
446 int count = canvas.save();
447 drawFocusHighlight(canvas);
448 canvas.restoreToCount(count);
449 }
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700450 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800451 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700452 }
453
454 protected void drawFocusHighlight(Canvas canvas) {
455 boolean isBadged = getTag() instanceof ItemInfo && !Process.myUserHandle().equals(
456 ((ItemInfo) getTag()).user);
457 float insetScale = (HIGHLIGHT_SCALE - 1) / 2;
458 canvas.translate(-getIconSize() * insetScale, -insetScale * getIconSize());
459 float outlineSize = getIconSize() * HIGHLIGHT_SCALE;
460 mHighlightPath.reset();
461 mHighlightPaint.reset();
462 getIconBounds(mDotParams.iconBounds);
463 getShape().addToPath(mHighlightPath, mDotParams.iconBounds.left, mDotParams.iconBounds.top,
464 outlineSize / 2);
465 if (isBadged) {
466 float borderSize = outlineSize - getIconSize();
467 float badgeSize = LauncherIcons.getBadgeSizeForIconSize(getIconSize()) + borderSize;
468 float badgeInset = outlineSize - badgeSize;
469 getShape().addToPath(mHighlightPath, mDotParams.iconBounds.left + badgeInset,
470 mDotParams.iconBounds.top + badgeInset, badgeSize / 2);
471 }
472 mHighlightPaint.setMaskFilter(mHighlightShadowFilter);
473 mHighlightPaint.setColor(mDotParams.color);
474 canvas.drawPath(mHighlightPath, mHighlightPaint);
475 mHighlightPaint.setMaskFilter(null);
476 mHighlightPaint.setColor(mHighlightColor);
477 canvas.drawPath(mHighlightPath, mHighlightPaint);
Tony Wickham1237df02017-02-24 08:59:36 -0800478 }
479
480 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800481 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700482 *
Tony Wickham1237df02017-02-24 08:59:36 -0800483 * @param canvas The canvas to draw to.
484 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800485 protected void drawDotIfNecessary(Canvas canvas) {
486 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800487 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700488 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
489 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800490 final int scrollX = getScrollX();
491 final int scrollY = getScrollY();
492 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800493 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800494 canvas.translate(-scrollX, -scrollY);
495 }
496 }
497
Jon Miranda9b78e192019-08-30 18:42:01 -0700498 @Override
499 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800500 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800501 return;
502 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800503 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800504
Tony Wickhamf34bee82018-12-03 18:11:39 -0800505 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800506 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800507 } else if (hasDot()) {
508 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800509 }
510 }
511
Tony Wickhamf34bee82018-12-03 18:11:39 -0800512 private boolean hasDot() {
513 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800514 }
515
516 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700517 getIconBounds(this, outBounds, mIconSize);
518 }
519
520 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
521 int top = iconView.getPaddingTop();
522 int left = (iconView.getWidth() - iconSize) / 2;
523 int right = left + iconSize;
524 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800525 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400527
528 @Override
Winson1f064272016-07-18 17:18:02 -0700529 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
530 if (mCenterVertically) {
531 Paint.FontMetrics fm = getPaint().getFontMetrics();
532 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
533 (int) Math.ceil(fm.bottom - fm.top);
534 int height = MeasureSpec.getSize(heightMeasureSpec);
535 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
536 getPaddingBottom());
537 }
538 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
539 }
540
541 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700542 public void setTextColor(int color) {
543 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700544 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700545 }
546
Adam Cohen96bb7982014-07-07 11:58:56 -0700547 @Override
548 public void setTextColor(ColorStateList colors) {
549 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700550 if (Float.compare(mTextAlpha, 1) == 0) {
551 super.setTextColor(colors);
552 } else {
553 super.setTextColor(getModifiedColor());
554 }
Adam Cohen477828c2013-09-20 12:05:49 -0700555 }
556
Tony7308cde2017-06-27 22:38:33 -0700557 public boolean shouldTextBeVisible() {
558 // Text should be visible everywhere but the hotseat.
559 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
560 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700561 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
562 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700563 }
564
Winson Chung5f8afe62013-08-12 16:19:28 -0700565 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700566 setTextAlpha(visible ? 1 : 0);
567 }
568
569 private void setTextAlpha(float alpha) {
570 mTextAlpha = alpha;
571 super.setTextColor(getModifiedColor());
572 }
573
574 private int getModifiedColor() {
575 if (mTextAlpha == 0) {
576 // Special case to prevent text shadows in high contrast mode
577 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700578 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800579 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700580 }
581
582 /**
583 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700584 *
Tony8f402802017-06-16 17:24:54 -0700585 * @param fadeIn Whether the text should fade in or fade out.
586 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700587 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700588 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700589 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700590 }
591
Winson Chungaffd7b42010-08-20 15:11:56 -0700592 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800593 public void cancelLongPress() {
594 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800595 mLongPressHelper.cancelLongPress();
596 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500597
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000598 /** Applies the loading progress value to the progress bar.
599 *
600 * If this app is installing, the progress bar will be updated with the installation progress.
601 * If this app is installed and downloading incrementally, the progress bar will be updated
602 * with the total download progress.
603 */
604 public void applyLoadingState(boolean promiseStateChanged) {
Sunny Goyal95899162019-03-27 16:03:06 -0700605 if (getTag() instanceof WorkspaceItemInfo) {
606 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000607 int progressLevel = info.getProgressLevel();
608 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE)
609 != 0) {
610 updateProgressBarUi(progressLevel, progressLevel == 100);
611 } else if (info.hasPromiseIconUi() || (info.runtimeStatusFlags
612 & ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE) != 0) {
613 updateProgressBarUi(progressLevel, promiseStateChanged);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700614 }
615 }
616 }
617
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000618 private void updateProgressBarUi(int progressLevel, boolean maybePerformFinishedAnimation) {
619 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
620 if (preloadDrawable != null && maybePerformFinishedAnimation) {
621 preloadDrawable.maybePerformFinishedAnimation();
622 }
623 }
624
625 /** Applies the given progress level to the this icon's progress bar. */
626 @Nullable
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700627 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
628 if (getTag() instanceof ItemInfoWithIcon) {
629 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700630 if (progressLevel >= 100) {
631 setContentDescription(info.contentDescription != null
632 ? info.contentDescription : "");
633 } else if (progressLevel > 0) {
634 setContentDescription(getContext()
635 .getString(R.string.app_downloading_title, info.title,
636 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
637 } else {
638 setContentDescription(getContext()
639 .getString(R.string.app_waiting_download_title, info.title));
640 }
Winson Chungb745afb2015-03-02 11:51:23 -0800641 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700642 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800643 if (mIcon instanceof PreloadIconDrawable) {
644 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700645 preloadDrawable.setLevel(progressLevel);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000646 preloadDrawable.setIsDisabled(!info.isAppStartable());
Sunny Goyale755d462014-07-22 13:48:29 -0700647 } else {
Sunny Goyal14168432019-10-24 15:59:49 -0700648 preloadDrawable = newPendingIcon(getContext(), info);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700649 preloadDrawable.setLevel(progressLevel);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000650 preloadDrawable.setIsDisabled(!info.isAppStartable());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700651 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700652 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700653 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700654 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400655 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700656 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400657 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700658
Tony Wickhamf34bee82018-12-03 18:11:39 -0800659 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800660 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800661 boolean wasDotted = mDotInfo != null;
662 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
663 boolean isDotted = mDotInfo != null;
664 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700665 if (mDisplay == DISPLAY_ALL_APPS) {
666 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
667 } else {
668 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
669 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800670 if (wasDotted || isDotted) {
671 // Animate when a dot is first added or when it is removed.
672 if (animate && (wasDotted ^ isDotted) && isShown()) {
673 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800674 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800675 cancelDotScaleAnim();
676 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800677 invalidate();
678 }
679 }
Tony Wickham305e9202018-01-23 17:46:47 -0800680 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700681 if (itemInfo.isDisabled()) {
682 setContentDescription(getContext().getString(R.string.disabled_app_label,
683 itemInfo.contentDescription));
684 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800685 int count = mDotInfo.getNotificationCount();
Tony Wickham305e9202018-01-23 17:46:47 -0800686 setContentDescription(getContext().getResources().getQuantityString(
Tony Wickhamf34bee82018-12-03 18:11:39 -0800687 R.plurals.dotted_app_label, count, itemInfo.contentDescription, count));
Tony Wickham305e9202018-01-23 17:46:47 -0800688 } else {
689 setContentDescription(itemInfo.contentDescription);
690 }
691 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800692 }
693 }
694
Winson Chungb745afb2015-03-02 11:51:23 -0800695 /**
696 * Sets the icon for this view based on the layout direction.
697 */
Samuel Fufa4b7f38b2020-10-06 18:37:46 -0700698 protected void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700699 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700700 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700701 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700702 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800703 if (mIcon != null) {
704 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
705 }
Tony17b7f9b2017-05-23 12:19:09 -0700706 }
707
Jon Miranda9b78e192019-08-30 18:42:01 -0700708 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700709 public void setIconVisible(boolean visible) {
710 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700711 if (!mIsIconVisible) {
712 resetIconScale();
713 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700714 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700715 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700716 }
717
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400718 protected boolean iconUpdateAnimationEnabled() {
719 return mEnableIconUpdateAnimation;
720 }
721
Tony Wickham377ed3f2016-07-20 15:21:04 -0700722 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700723 // If we had already set an icon before, disable relayout as the icon size is the
724 // same as before.
725 mDisableRelayout = mIcon != null;
726
727 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700728
729 updateIcon(icon);
730
731 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -0400732 if (mIcon != null
733 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400734 && iconUpdateAnimationEnabled()) {
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700735 animateIconUpdate((PlaceHolderIconDrawable) mIcon, icon);
Winson Chungb745afb2015-03-02 11:51:23 -0800736 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700737
Sunny Goyal66dccad2018-03-19 12:00:51 -0700738 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800739 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700740
Sunny Goyal69b75642015-05-15 17:00:24 -0700741 @Override
742 public void requestLayout() {
743 if (!mDisableRelayout) {
744 super.requestLayout();
745 }
746 }
747
Sunny Goyal34b65272015-03-11 16:56:52 -0700748 /**
749 * Applies the item info if it is same as what the view is pointing to currently.
750 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800751 @Override
752 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700753 if (getTag() == info) {
754 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700755 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400756 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -0700757
Jon Miranda7f522a22017-07-28 11:56:47 -0700758 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -0700759 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -0700760
Sunny Goyal34b65272015-03-11 16:56:52 -0700761 if (info instanceof AppInfo) {
762 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700763 } else if (info instanceof WorkspaceItemInfo) {
764 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700765 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700766 } else if (info instanceof PackageItemInfo) {
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700767 applyFromItemInfoWithIcon((PackageItemInfo) info);
Samuel Fufa02be2a42020-09-23 00:45:27 -0700768 } else if (info instanceof RemoteActionItemInfo) {
769 applyFromRemoteActionInfo((RemoteActionItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700770 }
Winsonc0880492015-08-21 11:16:27 -0700771
Sunny Goyal69b75642015-05-15 17:00:24 -0700772 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400773 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700774 }
775 }
776
777 /**
778 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
779 */
780 public void verifyHighRes() {
781 if (mIconLoadRequest != null) {
782 mIconLoadRequest.cancel();
783 mIconLoadRequest = null;
784 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800785 if (getTag() instanceof ItemInfoWithIcon) {
786 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700787 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800788 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700789 .updateIconInBackground(BubbleTextView.this, info);
790 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700791 }
792 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700793
Jon Miranda47170112017-03-03 14:57:14 -0800794 public int getIconSize() {
795 return mIconSize;
796 }
Adam Cohen65086992020-02-19 08:40:49 -0800797
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700798 private void updateTranslation() {
799 super.setTranslationX(mTranslationForReorderBounce.x + mTranslationForReorderPreview.x);
800 super.setTranslationY(mTranslationForReorderBounce.y + mTranslationForReorderPreview.y);
Adam Cohend9162062020-03-24 16:35:35 -0700801 }
802
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700803 public void setReorderBounceOffset(float x, float y) {
804 mTranslationForReorderBounce.set(x, y);
805 updateTranslation();
Adam Cohend9162062020-03-24 16:35:35 -0700806 }
807
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700808 public void getReorderBounceOffset(PointF offset) {
809 offset.set(mTranslationForReorderBounce);
810 }
811
812 @Override
813 public void setReorderPreviewOffset(float x, float y) {
814 mTranslationForReorderPreview.set(x, y);
815 updateTranslation();
816 }
817
818 @Override
819 public void getReorderPreviewOffset(PointF offset) {
820 offset.set(mTranslationForReorderPreview);
821 }
822
823 public void setReorderBounceScale(float scale) {
824 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -0700825 super.setScaleX(scale);
826 super.setScaleY(scale);
827 }
828
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700829 public float getReorderBounceScale() {
830 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -0700831 }
832
833 public View getView() {
834 return this;
835 }
836
Adam Cohen65086992020-02-19 08:40:49 -0800837 @Override
838 public int getViewType() {
839 return DRAGGABLE_ICON;
840 }
841
842 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -0700843 public void getWorkspaceVisualDragBounds(Rect bounds) {
Adam Cohen65086992020-02-19 08:40:49 -0800844 DeviceProfile grid = mActivity.getDeviceProfile();
845 BubbleTextView.getIconBounds(this, bounds, grid.iconSizePx);
846 }
847
Adam Cohenc77bc452020-05-07 11:55:19 -0700848 private int getIconSizeForDisplay(int display) {
849 DeviceProfile grid = mActivity.getDeviceProfile();
850 switch (display) {
851 case DISPLAY_ALL_APPS:
852 return grid.allAppsIconSizePx;
853 case DISPLAY_WORKSPACE:
854 case DISPLAY_FOLDER:
855 default:
856 return grid.iconSizePx;
857 }
858 }
859
860 public void getSourceVisualDragBounds(Rect bounds) {
861 BubbleTextView.getIconBounds(this, bounds, getIconSizeForDisplay(mDisplay));
862 }
863
Adam Cohen65086992020-02-19 08:40:49 -0800864 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -0700865 public SafeCloseable prepareDrawDragView() {
Samuel Fufa314761a2020-10-14 10:15:07 -0700866 int highlightColor = mHighlightColor;
867 mHighlightColor = Color.TRANSPARENT;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700868 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -0800869 setForceHideDot(true);
Samuel Fufa314761a2020-10-14 10:15:07 -0700870 return () -> mHighlightColor = highlightColor;
Adam Cohen65086992020-02-19 08:40:49 -0800871 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700872
873 private void resetIconScale() {
874 if (mIcon instanceof FastBitmapDrawable) {
875 ((FastBitmapDrawable) mIcon).setScale(1f);
876 }
877 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700878
879 private void updateIcon(Drawable newIcon) {
880 if (mLayoutHorizontal) {
881 setCompoundDrawablesRelative(newIcon, null, null, null);
882 } else {
883 setCompoundDrawables(null, newIcon, null, null);
884 }
885 }
886
887 private static void animateIconUpdate(PlaceHolderIconDrawable oldIcon, Drawable newIcon) {
888 int placeholderColor = oldIcon.mPaint.getColor();
889 int originalAlpha = Color.alpha(placeholderColor);
890
891 ValueAnimator iconUpdateAnimation = ValueAnimator.ofInt(originalAlpha, 0);
892 iconUpdateAnimation.setDuration(ICON_UPDATE_ANIMATION_DURATION);
893 iconUpdateAnimation.addUpdateListener(valueAnimator -> {
894 int newAlpha = (int) valueAnimator.getAnimatedValue();
895 int newColor = ColorUtils.setAlphaComponent(placeholderColor, newAlpha);
896
897 newIcon.setColorFilter(new PorterDuffColorFilter(newColor, Mode.SRC_ATOP));
898 });
899 iconUpdateAnimation.addListener(new AnimatorListenerAdapter() {
900 @Override
901 public void onAnimationEnd(Animator animation) {
902 newIcon.setColorFilter(null);
903 }
904 });
905 iconUpdateAnimation.start();
906 }
Samuel Fufa314761a2020-10-14 10:15:07 -0700907
908
909 @Override
910 public void decorate(int color) {
911 mHighlightColor = color;
912 invalidate();
913 }
914
915 @Override
916 public void removeDecoration() {
917 mHighlightColor = Color.TRANSPARENT;
918 invalidate();
919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920}