blob: b05a13a5b0e352162de2b9c578944dd7b862f342 [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;
Jon Miranda228877d2021-02-09 11:05:00 -050020import static com.android.launcher3.config.FeatureFlags.ENABLE_FOUR_COLUMNS;
Samuel Fufa314761a2020-10-14 10:15:07 -070021import static com.android.launcher3.graphics.IconShape.getShape;
Sunny Goyal14168432019-10-24 15:59:49 -070022import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon;
Sunny Goyal066ace12018-11-05 11:08:31 -080023import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
24
Tony4043b052018-08-17 13:47:39 -070025import android.animation.Animator;
26import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080027import android.animation.ObjectAnimator;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070028import android.animation.ValueAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070030import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070031import android.content.res.TypedArray;
Samuel Fufa314761a2020-10-14 10:15:07 -070032import android.graphics.BlurMaskFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070034import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070035import android.graphics.Paint;
Samuel Fufa314761a2020-10-14 10:15:07 -070036import android.graphics.Path;
Adam Cohend9162062020-03-24 16:35:35 -070037import android.graphics.PointF;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070038import android.graphics.PorterDuff.Mode;
39import android.graphics.PorterDuffColorFilter;
Tony Wickham1237df02017-02-24 08:59:36 -080040import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070041import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.drawable.Drawable;
Samuel Fufa314761a2020-10-14 10:15:07 -070043import android.os.Process;
Sunny Goyal66dccad2018-03-19 12:00:51 -070044import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080045import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080046import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070047import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070048import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080049import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070050import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080051import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080052import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070053
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +000054import androidx.annotation.Nullable;
Hyunyoung Song5809a402021-02-02 01:19:18 -080055import androidx.annotation.UiThread;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070056import androidx.core.graphics.ColorUtils;
57
Sunny Goyal3dce5f32017-10-05 11:40:05 -070058import com.android.launcher3.Launcher.OnResumeCallback;
Adam Cohen29794c52019-06-21 12:50:30 -070059import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Samuel Fufa314761a2020-10-14 10:15:07 -070060import com.android.launcher3.allapps.AllAppsSectionDecorator;
61import com.android.launcher3.config.FeatureFlags;
Tony Wickhamf34bee82018-12-03 18:11:39 -080062import com.android.launcher3.dot.DotInfo;
Adam Cohen65086992020-02-19 08:40:49 -080063import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070064import com.android.launcher3.folder.FolderIcon;
Tony Wickham1237df02017-02-24 08:59:36 -080065import com.android.launcher3.graphics.IconPalette;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070066import com.android.launcher3.graphics.IconShape;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070067import com.android.launcher3.graphics.PlaceHolderIconDrawable;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080068import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080069import com.android.launcher3.icons.DotRenderer;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080070import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Samuel Fufa314761a2020-10-14 10:15:07 -070071import com.android.launcher3.icons.LauncherIcons;
Sunny Goyal79e52fc2021-02-03 10:22:28 -080072import com.android.launcher3.icons.cache.HandlerRunnable;
Sunny Goyale396abf2020-04-06 15:11:17 -070073import com.android.launcher3.model.data.AppInfo;
74import com.android.launcher3.model.data.ItemInfo;
75import com.android.launcher3.model.data.ItemInfoWithIcon;
76import com.android.launcher3.model.data.PackageItemInfo;
Samuel Fufac2820182021-01-22 11:47:25 -060077import com.android.launcher3.model.data.SearchActionItemInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070078import com.android.launcher3.model.data.WorkspaceItemInfo;
Samuel Fufa167210a2020-05-12 18:40:11 -070079import com.android.launcher3.util.SafeCloseable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080080import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070081import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070082
Sunny Goyalc469aad2015-10-01 11:24:23 -070083import java.text.NumberFormat;
84
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085/**
86 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
87 * because we want to make the bubble taller than the text and TextView's clip is
88 * too aggressive.
89 */
Jon Miranda9b78e192019-08-30 18:42:01 -070090public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback,
Samuel Fufa314761a2020-10-14 10:15:07 -070091 IconLabelDotView, DraggableView, Reorderable, AllAppsSectionDecorator.SelfDecoratingView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070092
Sunny Goyaldfaccf62015-05-11 16:30:44 -070093 private static final int DISPLAY_WORKSPACE = 0;
94 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070095 private static final int DISPLAY_FOLDER = 2;
Samuel Fufa4a53c702020-08-04 14:06:55 -070096 private static final int DISPLAY_HERO_APP = 5;
Tony Wickham794fe4f2021-01-11 14:54:23 -060097 protected static final int DISPLAY_TASKBAR = 6;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070098
Samuel Fufad6bacdc2020-08-23 21:56:07 -070099 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Samuel Fufa314761a2020-10-14 10:15:07 -0700100 private static final float HIGHLIGHT_SCALE = 1.16f;
101
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800102
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700103 private final PointF mTranslationForReorderBounce = new PointF(0, 0);
104 private final PointF mTranslationForReorderPreview = new PointF(0, 0);
105
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700106 private static final int ICON_UPDATE_ANIMATION_DURATION = 375;
107
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700108 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -0800109
Samuel Fufa314761a2020-10-14 10:15:07 -0700110 protected final Paint mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
111 private final Path mHighlightPath = new Path();
112 protected int mHighlightColor = Color.TRANSPARENT;
113 private final BlurMaskFilter mHighlightShadowFilter;
114
Tony Wickhamf34bee82018-12-03 18:11:39 -0800115 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
116 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800117 @Override
118 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800119 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800120 }
121
122 @Override
123 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800124 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800125 bubbleTextView.invalidate();
126 }
127 };
128
Sunny Goyaled7a6932018-04-13 11:41:50 -0700129 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
130 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700131 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700132 public Float get(BubbleTextView bubbleTextView) {
133 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700134 }
135
136 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700137 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700138 bubbleTextView.setTextAlpha(alpha);
139 }
140 };
141
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800142 private final ActivityContext mActivity;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700143 private Drawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800144 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700145
Tony Wickham794fe4f2021-01-11 14:54:23 -0600146 protected final int mDisplay;
Samuel Fufac96fa242019-09-26 23:06:32 -0700147
Sunny Goyaled7a6932018-04-13 11:41:50 -0700148 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700149
150 private final boolean mLayoutHorizontal;
151 private final int mIconSize;
152
153 @ViewDebug.ExportedProperty(category = "launcher")
154 private boolean mIsIconVisible = true;
155 @ViewDebug.ExportedProperty(category = "launcher")
156 private int mTextColor;
157 @ViewDebug.ExportedProperty(category = "launcher")
158 private float mTextAlpha = 1;
159
Tony4043b052018-08-17 13:47:39 -0700160 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800161 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800162 private DotRenderer mDotRenderer;
163 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
164 private DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800165 private Animator mDotScaleAnim;
166 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700167
Sunny Goyal4ffec482016-02-09 11:28:52 -0800168 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800169 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800170 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700171 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800172 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700173 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500174
Sunny Goyal79e52fc2021-02-03 10:22:28 -0800175 private HandlerRunnable mIconLoadRequest;
Sunny Goyal34b65272015-03-11 16:56:52 -0700176
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400177 private boolean mEnableIconUpdateAnimation = false;
178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700180 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 }
182
183 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700184 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 }
186
187 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
188 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800189 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700190
Adam Cohen96bb7982014-07-07 11:58:56 -0700191 TypedArray a = context.obtainStyledAttributes(attrs,
192 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800193 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700194 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700195
Samuel Fufac96fa242019-09-26 23:06:32 -0700196 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700197 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700198 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700199 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700200 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700201 defaultIconSize = grid.iconSizePx;
Jon Miranda228877d2021-02-09 11:05:00 -0500202 setCenterVertically(ENABLE_FOUR_COLUMNS.get());
Samuel Fufac96fa242019-09-26 23:06:32 -0700203 } else if (mDisplay == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700204 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
205 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700206 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700207 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700208 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700209 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700210 defaultIconSize = grid.folderChildIconSizePx;
Samuel Fufa4a53c702020-08-04 14:06:55 -0700211 } else if (mDisplay == DISPLAY_HERO_APP) {
212 defaultIconSize = grid.allAppsIconSizePx;
Tony Wickham794fe4f2021-01-11 14:54:23 -0600213 } else if (mDisplay == DISPLAY_TASKBAR) {
214 defaultIconSize = grid.iconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700215 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700216 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700217 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700218 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700219
Winson1f064272016-07-18 17:18:02 -0700220 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700221
222 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
223 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700224 a.recycle();
225
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800226 mLongPressHelper = new CheckLongPressHelper(this);
227
Tony Wickham8912b042018-11-29 15:28:53 -0800228 mDotParams = new DotRenderer.DrawParams();
229
Sunny Goyal66dccad2018-03-19 12:00:51 -0700230 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700231 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700232 setTextAlpha(1f);
Samuel Fufa314761a2020-10-14 10:15:07 -0700233
234 int shadowSize = context.getResources().getDimensionPixelSize(
235 R.dimen.blur_size_click_shadow);
236 mHighlightShadowFilter = new BlurMaskFilter(shadowSize, BlurMaskFilter.Blur.INNER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 }
238
Sunny Goyal66dccad2018-03-19 12:00:51 -0700239 @Override
240 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
241 // Disable marques when not focused to that, so that updating text does not cause relayout.
242 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
243 super.onFocusChanged(focused, direction, previouslyFocusedRect);
244 }
245
Jon Miranda52549442017-10-26 11:28:06 -0700246 /**
247 * Resets the view so it can be recycled.
248 */
249 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800250 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800251 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800252 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800253 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800254 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800255 setBackground(null);
Jon Miranda52549442017-10-26 11:28:06 -0700256 }
257
Tony Wickhamf34bee82018-12-03 18:11:39 -0800258 private void cancelDotScaleAnim() {
259 if (mDotScaleAnim != null) {
260 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700261 }
262 }
263
Tony Wickhamf34bee82018-12-03 18:11:39 -0800264 private void animateDotScale(float... dotScales) {
265 cancelDotScaleAnim();
266 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
267 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700268 @Override
269 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800270 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700271 }
272 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800273 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700274 }
275
Hyunyoung Song5809a402021-02-02 01:19:18 -0800276 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700277 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
278 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700279 }
280
Adam Cohen29794c52019-06-21 12:50:30 -0700281 @Override
282 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
283 if (delegate instanceof LauncherAccessibilityDelegate) {
284 super.setAccessibilityDelegate(delegate);
285 } else {
286 // NO-OP
287 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
288 // delegate incorrectly. There are no cases when we shouldn't be using the
289 // LauncherAccessibilityDelegate for BubbleTextView.
290 }
291 }
292
Hyunyoung Song5809a402021-02-02 01:19:18 -0800293 @UiThread
Sunny Goyal95899162019-03-27 16:03:06 -0700294 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800295 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800296 setTag(info);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000297 applyLoadingState(promiseStateChanged);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800298 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800299 setDownloadStateContentDescription(info, info.getProgressLevel());
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800300 }
301
Hyunyoung Song5809a402021-02-02 01:19:18 -0800302 @UiThread
Sunny Goyal508da152014-08-14 10:53:27 -0700303 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800304 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700305
Sunny Goyal95899162019-03-27 16:03:06 -0700306 // We don't need to check the info since it's not a WorkspaceItemInfo
Winson Chung888b3a12015-03-13 11:14:16 -0700307 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700308
309 // Verify high res immediately
310 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800311
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000312 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800313 applyProgressLevel();
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700314 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800315 applyDotState(info, false /* animate */);
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800316 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal508da152014-08-14 10:53:27 -0700317 }
318
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700319 /**
320 * Apply label and tag using a generic {@link ItemInfoWithIcon}
321 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800322 @UiThread
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700323 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800324 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700325 // We don't need to check the info since it's not a WorkspaceItemInfo
Sunny Goyal0e08f162015-05-12 11:32:39 -0700326 super.setTag(info);
327
328 // Verify high res immediately
329 verifyHighRes();
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800330
331 setDownloadStateContentDescription(info, info.getProgressLevel());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700332 }
333
Samuel Fufa02be2a42020-09-23 00:45:27 -0700334 /**
Samuel Fufac2820182021-01-22 11:47:25 -0600335 * Apply label and tag using a {@link SearchActionItemInfo}
Samuel Fufa02be2a42020-09-23 00:45:27 -0700336 */
Hyunyoung Song5809a402021-02-02 01:19:18 -0800337 @UiThread
Samuel Fufac2820182021-01-22 11:47:25 -0600338 public void applyFromSearchActionItemInfo(SearchActionItemInfo searchActionItemInfo) {
339 applyIconAndLabel(searchActionItemInfo);
340 setTag(searchActionItemInfo);
Samuel Fufa02be2a42020-09-23 00:45:27 -0700341 }
342
Hyunyoung Song5809a402021-02-02 01:19:18 -0800343 @UiThread
Samuel Fufac2820182021-01-22 11:47:25 -0600344 protected void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal14168432019-10-24 15:59:49 -0700345 FastBitmapDrawable iconDrawable = newIcon(getContext(), info);
Sunny Goyal3808a692019-10-25 13:41:28 -0700346 mDotParams.color = IconPalette.getMutedColor(info.bitmap.color, 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800347
Sunny Goyalf4204382016-07-13 10:46:07 -0700348 setIcon(iconDrawable);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000349 applyLabel(info);
350 }
351
Hyunyoung Song5809a402021-02-02 01:19:18 -0800352 @UiThread
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000353 private void applyLabel(ItemInfoWithIcon info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700354 setText(info.title);
355 if (info.contentDescription != null) {
356 setContentDescription(info.isDisabled()
357 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
358 : info.contentDescription);
359 }
360 }
361
Winson Chung7501adf2015-06-02 11:24:28 -0700362 /**
Tony Wickhamaaa42bd2021-01-27 16:27:15 -0800363 * Directly set the icon and label.
364 */
365 @UiThread
366 public void applyIconAndLabel(Drawable icon, CharSequence label) {
367 setIcon(icon);
368 setText(label);
369 setContentDescription(label);
370 }
371
372 /**
Winson Chung7501adf2015-06-02 11:24:28 -0700373 * Overrides the default long press timeout.
374 */
Tony2ca999c2018-09-24 17:24:51 -0400375 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
376 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700377 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800380 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700381 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800382 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800383 }
Sunny Goyal508da152014-08-14 10:53:27 -0700384 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800385
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800386 @Override
387 protected int[] onCreateDrawableState(int extraSpace) {
388 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
389 if (mStayPressed) {
390 mergeDrawableStates(drawableState, STATE_PRESSED);
391 }
392 return drawableState;
393 }
394
Winson Chungb745afb2015-03-02 11:51:23 -0800395 /** Returns the icon for this view. */
396 public Drawable getIcon() {
397 return mIcon;
398 }
399
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700400 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800401 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700402 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700403 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700404 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700405 return false;
406 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700407 if (isLongClickable()) {
408 super.onTouchEvent(event);
409 mLongPressHelper.onTouchEvent(event);
410 // Keep receiving the rest of the events
411 return true;
412 } else {
413 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700414 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800415 }
416
Sunny Goyal44a3b202020-07-09 08:27:06 -0700417 /**
418 * Returns true if the touch down at the provided position be ignored
419 */
420 protected boolean shouldIgnoreTouchDown(float x, float y) {
421 return y < getPaddingTop()
422 || x < getPaddingLeft()
423 || y > getHeight() - getPaddingBottom()
424 || x > getWidth() - getPaddingRight();
425 }
426
Michael Jurkaddd62e92011-02-16 17:49:14 -0800427 void setStayPressed(boolean stayPressed) {
428 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800429 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800430 }
431
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700432 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800433 public void onVisibilityAggregated(boolean isVisible) {
434 super.onVisibilityAggregated(isVisible);
435 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800436 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800437 }
438 }
439
440 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700441 public void onLauncherResume() {
442 // Reset the pressed state of icon that was locked in the press state while activity
443 // was launching
444 setStayPressed(false);
445 }
446
Sunny Goyal508da152014-08-14 10:53:27 -0700447 void clearPressedBackground() {
448 setPressed(false);
449 setStayPressed(false);
450 }
451
452 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700453 public boolean onKeyUp(int keyCode, KeyEvent event) {
454 // Unlike touch events, keypress event propagate pressed state change immediately,
455 // without waiting for onClickHandler to execute. Disable pressed state changes here
456 // to avoid flickering.
457 mIgnorePressedStateChange = true;
458 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700459 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800460 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700461 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800462 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800463
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700464 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800465 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700466 super.onDraw(canvas);
467 }
468
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700470 public void onDraw(Canvas canvas) {
Samuel Fufa314761a2020-10-14 10:15:07 -0700471 if (FeatureFlags.ENABLE_DEVICE_SEARCH.get() && mHighlightColor != Color.TRANSPARENT) {
472 int count = canvas.save();
473 drawFocusHighlight(canvas);
474 canvas.restoreToCount(count);
475 }
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700476 super.onDraw(canvas);
Tony Wickham22a5a682020-11-02 17:04:58 -0800477 drawDotIfNecessary(canvas);
Samuel Fufa314761a2020-10-14 10:15:07 -0700478 }
479
480 protected void drawFocusHighlight(Canvas canvas) {
481 boolean isBadged = getTag() instanceof ItemInfo && !Process.myUserHandle().equals(
482 ((ItemInfo) getTag()).user);
483 float insetScale = (HIGHLIGHT_SCALE - 1) / 2;
484 canvas.translate(-getIconSize() * insetScale, -insetScale * getIconSize());
485 float outlineSize = getIconSize() * HIGHLIGHT_SCALE;
486 mHighlightPath.reset();
487 mHighlightPaint.reset();
488 getIconBounds(mDotParams.iconBounds);
489 getShape().addToPath(mHighlightPath, mDotParams.iconBounds.left, mDotParams.iconBounds.top,
490 outlineSize / 2);
491 if (isBadged) {
492 float borderSize = outlineSize - getIconSize();
493 float badgeSize = LauncherIcons.getBadgeSizeForIconSize(getIconSize()) + borderSize;
494 float badgeInset = outlineSize - badgeSize;
495 getShape().addToPath(mHighlightPath, mDotParams.iconBounds.left + badgeInset,
496 mDotParams.iconBounds.top + badgeInset, badgeSize / 2);
497 }
498 mHighlightPaint.setMaskFilter(mHighlightShadowFilter);
499 mHighlightPaint.setColor(mDotParams.color);
500 canvas.drawPath(mHighlightPath, mHighlightPaint);
501 mHighlightPaint.setMaskFilter(null);
502 mHighlightPaint.setColor(mHighlightColor);
503 canvas.drawPath(mHighlightPath, mHighlightPaint);
Tony Wickham1237df02017-02-24 08:59:36 -0800504 }
505
506 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800507 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700508 *
Tony Wickham1237df02017-02-24 08:59:36 -0800509 * @param canvas The canvas to draw to.
510 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800511 protected void drawDotIfNecessary(Canvas canvas) {
Tony Wickham794fe4f2021-01-11 14:54:23 -0600512 if (mDisplay == DISPLAY_TASKBAR) {
513 // TODO: support notification dots in Taskbar
514 return;
515 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800516 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800517 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700518 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
519 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800520 final int scrollX = getScrollX();
521 final int scrollY = getScrollY();
522 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800523 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800524 canvas.translate(-scrollX, -scrollY);
525 }
526 }
527
Jon Miranda9b78e192019-08-30 18:42:01 -0700528 @Override
529 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800530 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800531 return;
532 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800533 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800534
Tony Wickhamf34bee82018-12-03 18:11:39 -0800535 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800536 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800537 } else if (hasDot()) {
538 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800539 }
540 }
541
Tony Wickhamf34bee82018-12-03 18:11:39 -0800542 private boolean hasDot() {
543 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800544 }
545
546 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700547 getIconBounds(this, outBounds, mIconSize);
548 }
549
550 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
551 int top = iconView.getPaddingTop();
552 int left = (iconView.getWidth() - iconSize) / 2;
553 int right = left + iconSize;
554 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800555 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400557
Jon Miranda228877d2021-02-09 11:05:00 -0500558
559 /**
560 * Sets whether to vertically center the content.
561 */
562 public void setCenterVertically(boolean centerVertically) {
563 mCenterVertically = centerVertically;
564 }
565
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 @Override
Winson1f064272016-07-18 17:18:02 -0700567 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
568 if (mCenterVertically) {
569 Paint.FontMetrics fm = getPaint().getFontMetrics();
570 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
571 (int) Math.ceil(fm.bottom - fm.top);
572 int height = MeasureSpec.getSize(heightMeasureSpec);
573 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
574 getPaddingBottom());
575 }
576 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
577 }
578
579 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700580 public void setTextColor(int color) {
581 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700582 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700583 }
584
Adam Cohen96bb7982014-07-07 11:58:56 -0700585 @Override
586 public void setTextColor(ColorStateList colors) {
587 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700588 if (Float.compare(mTextAlpha, 1) == 0) {
589 super.setTextColor(colors);
590 } else {
591 super.setTextColor(getModifiedColor());
592 }
Adam Cohen477828c2013-09-20 12:05:49 -0700593 }
594
Tony7308cde2017-06-27 22:38:33 -0700595 public boolean shouldTextBeVisible() {
596 // Text should be visible everywhere but the hotseat.
597 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
598 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700599 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
600 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700601 }
602
Winson Chung5f8afe62013-08-12 16:19:28 -0700603 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700604 setTextAlpha(visible ? 1 : 0);
605 }
606
607 private void setTextAlpha(float alpha) {
608 mTextAlpha = alpha;
609 super.setTextColor(getModifiedColor());
610 }
611
612 private int getModifiedColor() {
613 if (mTextAlpha == 0) {
614 // Special case to prevent text shadows in high contrast mode
615 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700616 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800617 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700618 }
619
620 /**
621 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700622 *
Tony8f402802017-06-16 17:24:54 -0700623 * @param fadeIn Whether the text should fade in or fade out.
624 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700625 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700626 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700627 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700628 }
629
Winson Chungaffd7b42010-08-20 15:11:56 -0700630 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800631 public void cancelLongPress() {
632 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800633 mLongPressHelper.cancelLongPress();
634 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500635
Samuel Fufac2820182021-01-22 11:47:25 -0600636 /**
637 * Applies the loading progress value to the progress bar.
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000638 *
639 * If this app is installing, the progress bar will be updated with the installation progress.
640 * If this app is installed and downloading incrementally, the progress bar will be updated
641 * with the total download progress.
642 */
643 public void applyLoadingState(boolean promiseStateChanged) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800644 if (getTag() instanceof ItemInfoWithIcon) {
Sunny Goyal95899162019-03-27 16:03:06 -0700645 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000646 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE)
647 != 0) {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800648 updateProgressBarUi(info.getProgressLevel() == 100);
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000649 } else if (info.hasPromiseIconUi() || (info.runtimeStatusFlags
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800650 & ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE) != 0) {
651 updateProgressBarUi(promiseStateChanged);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700652 }
653 }
654 }
655
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800656 private void updateProgressBarUi(boolean maybePerformFinishedAnimation) {
657 PreloadIconDrawable preloadDrawable = applyProgressLevel();
Schneider Victor-tulias6a5f4312021-01-12 19:30:02 +0000658 if (preloadDrawable != null && maybePerformFinishedAnimation) {
659 preloadDrawable.maybePerformFinishedAnimation();
660 }
661 }
662
663 /** Applies the given progress level to the this icon's progress bar. */
664 @Nullable
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800665 public PreloadIconDrawable applyProgressLevel() {
666 if (!(getTag() instanceof ItemInfoWithIcon)) {
667 return null;
668 }
669
670 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
671 int progressLevel = info.getProgressLevel();
672 if (progressLevel >= 100) {
673 setContentDescription(info.contentDescription != null
674 ? info.contentDescription : "");
675 } else if (progressLevel > 0) {
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800676 setDownloadStateContentDescription(info, progressLevel);
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800677 } else {
678 setContentDescription(getContext()
679 .getString(R.string.app_waiting_download_title, info.title));
680 }
681 if (mIcon != null) {
682 PreloadIconDrawable preloadIconDrawable;
683 if (mIcon instanceof PreloadIconDrawable) {
684 preloadIconDrawable = (PreloadIconDrawable) mIcon;
685 preloadIconDrawable.setLevel(progressLevel);
686 preloadIconDrawable.setIsDisabled(!info.isAppStartable());
Mario Bertschler230612f2017-09-27 17:05:21 -0700687 } else {
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800688 preloadIconDrawable = makePreloadIcon();
689 setIcon(preloadIconDrawable);
Mario Bertschler230612f2017-09-27 17:05:21 -0700690 }
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800691 return preloadIconDrawable;
Chris Wren40c5ed32014-06-24 18:24:23 -0400692 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700693 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400694 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700695
Schneider Victor-tuliasb40b98d2021-01-21 14:41:07 -0800696 /**
697 * Creates a PreloadIconDrawable with the appropriate progress level without mutating this
698 * object.
699 */
700 @Nullable
701 public PreloadIconDrawable makePreloadIcon() {
702 if (!(getTag() instanceof ItemInfoWithIcon)) {
703 return null;
704 }
705
706 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
707 int progressLevel = info.getProgressLevel();
708 final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info);
709
710 preloadDrawable.setLevel(progressLevel);
711 preloadDrawable.setIsDisabled(!info.isAppStartable());
712
713 return preloadDrawable;
714 }
715
Tony Wickhamf34bee82018-12-03 18:11:39 -0800716 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800717 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800718 boolean wasDotted = mDotInfo != null;
719 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
720 boolean isDotted = mDotInfo != null;
721 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700722 if (mDisplay == DISPLAY_ALL_APPS) {
723 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
724 } else {
725 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
726 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800727 if (wasDotted || isDotted) {
728 // Animate when a dot is first added or when it is removed.
729 if (animate && (wasDotted ^ isDotted) && isShown()) {
730 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800731 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800732 cancelDotScaleAnim();
733 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800734 invalidate();
735 }
736 }
Tony Wickham305e9202018-01-23 17:46:47 -0800737 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700738 if (itemInfo.isDisabled()) {
739 setContentDescription(getContext().getString(R.string.disabled_app_label,
740 itemInfo.contentDescription));
741 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800742 int count = mDotInfo.getNotificationCount();
Tony Wickham305e9202018-01-23 17:46:47 -0800743 setContentDescription(getContext().getResources().getQuantityString(
Tony Wickhamf34bee82018-12-03 18:11:39 -0800744 R.plurals.dotted_app_label, count, itemInfo.contentDescription, count));
Tony Wickham305e9202018-01-23 17:46:47 -0800745 } else {
746 setContentDescription(itemInfo.contentDescription);
747 }
748 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800749 }
750 }
751
Schneider Victor-tulias5f7f4cb2021-01-28 13:51:23 -0800752 private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) {
753 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK)
754 != 0) {
755 String percentageString = NumberFormat.getPercentInstance()
756 .format(progressLevel * 0.01);
757 if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE) != 0) {
758 setContentDescription(getContext()
759 .getString(
760 R.string.app_installing_title, info.title, percentageString));
761 } else if ((info.runtimeStatusFlags
762 & ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) {
763 setContentDescription(getContext()
764 .getString(
765 R.string.app_downloading_title, info.title, percentageString));
766 }
767 }
768 }
769
Winson Chungb745afb2015-03-02 11:51:23 -0800770 /**
771 * Sets the icon for this view based on the layout direction.
772 */
Samuel Fufa4b7f38b2020-10-06 18:37:46 -0700773 protected void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700774 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700775 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700776 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700777 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800778 if (mIcon != null) {
779 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
780 }
Tony17b7f9b2017-05-23 12:19:09 -0700781 }
782
Jon Miranda9b78e192019-08-30 18:42:01 -0700783 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700784 public void setIconVisible(boolean visible) {
785 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700786 if (!mIsIconVisible) {
787 resetIconScale();
788 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700789 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700790 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700791 }
792
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400793 protected boolean iconUpdateAnimationEnabled() {
794 return mEnableIconUpdateAnimation;
795 }
796
Tony Wickham377ed3f2016-07-20 15:21:04 -0700797 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700798 // If we had already set an icon before, disable relayout as the icon size is the
799 // same as before.
800 mDisableRelayout = mIcon != null;
801
802 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700803
804 updateIcon(icon);
805
806 // If the current icon is a placeholder color, animate its update.
Schneider Victor-tulias3ee70182020-10-21 14:01:53 -0400807 if (mIcon != null
808 && mIcon instanceof PlaceHolderIconDrawable
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400809 && iconUpdateAnimationEnabled()) {
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700810 animateIconUpdate((PlaceHolderIconDrawable) mIcon, icon);
Winson Chungb745afb2015-03-02 11:51:23 -0800811 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700812
Sunny Goyal66dccad2018-03-19 12:00:51 -0700813 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800814 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700815
Sunny Goyal69b75642015-05-15 17:00:24 -0700816 @Override
817 public void requestLayout() {
818 if (!mDisableRelayout) {
819 super.requestLayout();
820 }
821 }
822
Sunny Goyal34b65272015-03-11 16:56:52 -0700823 /**
824 * Applies the item info if it is same as what the view is pointing to currently.
825 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800826 @Override
827 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700828 if (getTag() == info) {
829 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700830 mDisableRelayout = true;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400831 mEnableIconUpdateAnimation = true;
Winsonc0880492015-08-21 11:16:27 -0700832
Jon Miranda7f522a22017-07-28 11:56:47 -0700833 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -0700834 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -0700835
Sunny Goyal34b65272015-03-11 16:56:52 -0700836 if (info instanceof AppInfo) {
837 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700838 } else if (info instanceof WorkspaceItemInfo) {
839 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700840 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700841 } else if (info instanceof PackageItemInfo) {
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700842 applyFromItemInfoWithIcon((PackageItemInfo) info);
Samuel Fufac2820182021-01-22 11:47:25 -0600843 } else if (info instanceof SearchActionItemInfo) {
844 applyFromSearchActionItemInfo((SearchActionItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700845 }
Winsonc0880492015-08-21 11:16:27 -0700846
Sunny Goyal69b75642015-05-15 17:00:24 -0700847 mDisableRelayout = false;
Schneider Victor-tulias7bbec502020-10-29 11:39:24 -0400848 mEnableIconUpdateAnimation = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700849 }
850 }
851
852 /**
853 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
854 */
855 public void verifyHighRes() {
856 if (mIconLoadRequest != null) {
857 mIconLoadRequest.cancel();
858 mIconLoadRequest = null;
859 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800860 if (getTag() instanceof ItemInfoWithIcon) {
861 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700862 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800863 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700864 .updateIconInBackground(BubbleTextView.this, info);
865 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700866 }
867 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700868
Jon Miranda47170112017-03-03 14:57:14 -0800869 public int getIconSize() {
870 return mIconSize;
871 }
Adam Cohen65086992020-02-19 08:40:49 -0800872
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700873 private void updateTranslation() {
874 super.setTranslationX(mTranslationForReorderBounce.x + mTranslationForReorderPreview.x);
875 super.setTranslationY(mTranslationForReorderBounce.y + mTranslationForReorderPreview.y);
Adam Cohend9162062020-03-24 16:35:35 -0700876 }
877
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700878 public void setReorderBounceOffset(float x, float y) {
879 mTranslationForReorderBounce.set(x, y);
880 updateTranslation();
Adam Cohend9162062020-03-24 16:35:35 -0700881 }
882
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700883 public void getReorderBounceOffset(PointF offset) {
884 offset.set(mTranslationForReorderBounce);
885 }
886
887 @Override
888 public void setReorderPreviewOffset(float x, float y) {
889 mTranslationForReorderPreview.set(x, y);
890 updateTranslation();
891 }
892
893 @Override
894 public void getReorderPreviewOffset(PointF offset) {
895 offset.set(mTranslationForReorderPreview);
896 }
897
898 public void setReorderBounceScale(float scale) {
899 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -0700900 super.setScaleX(scale);
901 super.setScaleY(scale);
902 }
903
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700904 public float getReorderBounceScale() {
905 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -0700906 }
907
908 public View getView() {
909 return this;
910 }
911
Adam Cohen65086992020-02-19 08:40:49 -0800912 @Override
913 public int getViewType() {
914 return DRAGGABLE_ICON;
915 }
916
917 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -0700918 public void getWorkspaceVisualDragBounds(Rect bounds) {
Adam Cohen65086992020-02-19 08:40:49 -0800919 DeviceProfile grid = mActivity.getDeviceProfile();
920 BubbleTextView.getIconBounds(this, bounds, grid.iconSizePx);
921 }
922
Adam Cohenc77bc452020-05-07 11:55:19 -0700923 private int getIconSizeForDisplay(int display) {
924 DeviceProfile grid = mActivity.getDeviceProfile();
925 switch (display) {
926 case DISPLAY_ALL_APPS:
927 return grid.allAppsIconSizePx;
928 case DISPLAY_WORKSPACE:
929 case DISPLAY_FOLDER:
930 default:
931 return grid.iconSizePx;
932 }
933 }
934
935 public void getSourceVisualDragBounds(Rect bounds) {
936 BubbleTextView.getIconBounds(this, bounds, getIconSizeForDisplay(mDisplay));
937 }
938
Adam Cohen65086992020-02-19 08:40:49 -0800939 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -0700940 public SafeCloseable prepareDrawDragView() {
Samuel Fufa314761a2020-10-14 10:15:07 -0700941 int highlightColor = mHighlightColor;
942 mHighlightColor = Color.TRANSPARENT;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700943 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -0800944 setForceHideDot(true);
Samuel Fufa314761a2020-10-14 10:15:07 -0700945 return () -> mHighlightColor = highlightColor;
Adam Cohen65086992020-02-19 08:40:49 -0800946 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700947
948 private void resetIconScale() {
949 if (mIcon instanceof FastBitmapDrawable) {
950 ((FastBitmapDrawable) mIcon).setScale(1f);
951 }
952 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700953
954 private void updateIcon(Drawable newIcon) {
955 if (mLayoutHorizontal) {
956 setCompoundDrawablesRelative(newIcon, null, null, null);
957 } else {
958 setCompoundDrawables(null, newIcon, null, null);
959 }
960 }
961
962 private static void animateIconUpdate(PlaceHolderIconDrawable oldIcon, Drawable newIcon) {
963 int placeholderColor = oldIcon.mPaint.getColor();
964 int originalAlpha = Color.alpha(placeholderColor);
965
966 ValueAnimator iconUpdateAnimation = ValueAnimator.ofInt(originalAlpha, 0);
967 iconUpdateAnimation.setDuration(ICON_UPDATE_ANIMATION_DURATION);
968 iconUpdateAnimation.addUpdateListener(valueAnimator -> {
969 int newAlpha = (int) valueAnimator.getAnimatedValue();
970 int newColor = ColorUtils.setAlphaComponent(placeholderColor, newAlpha);
971
972 newIcon.setColorFilter(new PorterDuffColorFilter(newColor, Mode.SRC_ATOP));
973 });
974 iconUpdateAnimation.addListener(new AnimatorListenerAdapter() {
975 @Override
976 public void onAnimationEnd(Animator animation) {
977 newIcon.setColorFilter(null);
978 }
979 });
980 iconUpdateAnimation.start();
981 }
Samuel Fufa314761a2020-10-14 10:15:07 -0700982
983
984 @Override
985 public void decorate(int color) {
986 mHighlightColor = color;
987 invalidate();
988 }
989
990 @Override
991 public void removeDecoration() {
992 mHighlightColor = Color.TRANSPARENT;
993 invalidate();
994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995}