blob: fd0408119857d182431af23308c8b3a8abc833f5 [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;
20import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon;
Sunny Goyal066ace12018-11-05 11:08:31 -080021import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
22
Tony4043b052018-08-17 13:47:39 -070023import android.animation.Animator;
24import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080025import android.animation.ObjectAnimator;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070026import android.animation.ValueAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070028import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070029import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070031import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070032import android.graphics.Paint;
Adam Cohend9162062020-03-24 16:35:35 -070033import android.graphics.PointF;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070034import android.graphics.PorterDuff.Mode;
35import android.graphics.PorterDuffColorFilter;
Tony Wickham1237df02017-02-24 08:59:36 -080036import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070037import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.graphics.drawable.Drawable;
Sunny Goyal66dccad2018-03-19 12:00:51 -070039import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080040import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080041import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070042import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070043import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080044import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070045import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080046import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080047import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070048
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070049import androidx.core.graphics.ColorUtils;
50
Sunny Goyal3dce5f32017-10-05 11:40:05 -070051import com.android.launcher3.Launcher.OnResumeCallback;
Adam Cohen29794c52019-06-21 12:50:30 -070052import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Tony Wickhamf34bee82018-12-03 18:11:39 -080053import com.android.launcher3.dot.DotInfo;
Adam Cohen65086992020-02-19 08:40:49 -080054import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070055import com.android.launcher3.folder.FolderIcon;
Tony Wickham1237df02017-02-24 08:59:36 -080056import com.android.launcher3.graphics.IconPalette;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070057import com.android.launcher3.graphics.IconShape;
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070058import com.android.launcher3.graphics.PlaceHolderIconDrawable;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080059import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080060import com.android.launcher3.icons.DotRenderer;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080061import com.android.launcher3.icons.IconCache.IconLoadRequest;
62import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyale396abf2020-04-06 15:11:17 -070063import com.android.launcher3.model.data.AppInfo;
64import com.android.launcher3.model.data.ItemInfo;
65import com.android.launcher3.model.data.ItemInfoWithIcon;
66import com.android.launcher3.model.data.PackageItemInfo;
67import com.android.launcher3.model.data.PromiseAppInfo;
68import com.android.launcher3.model.data.WorkspaceItemInfo;
Samuel Fufa167210a2020-05-12 18:40:11 -070069import com.android.launcher3.util.SafeCloseable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080070import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070071import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070072
Sunny Goyalc469aad2015-10-01 11:24:23 -070073import java.text.NumberFormat;
74
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075/**
76 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
77 * because we want to make the bubble taller than the text and TextView's clip is
78 * too aggressive.
79 */
Jon Miranda9b78e192019-08-30 18:42:01 -070080public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback,
Adam Cohend9162062020-03-24 16:35:35 -070081 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070082
Sunny Goyaldfaccf62015-05-11 16:30:44 -070083 private static final int DISPLAY_WORKSPACE = 0;
84 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070085 private static final int DISPLAY_FOLDER = 2;
Samuel Fufa4a53c702020-08-04 14:06:55 -070086 private static final int DISPLAY_HERO_APP = 5;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070087
Samuel Fufad6bacdc2020-08-23 21:56:07 -070088 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080089
Adam Cohen1d13c0b2020-04-21 16:29:12 -070090 private final PointF mTranslationForReorderBounce = new PointF(0, 0);
91 private final PointF mTranslationForReorderPreview = new PointF(0, 0);
92
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070093 private static final int ICON_UPDATE_ANIMATION_DURATION = 375;
94
Adam Cohen1d13c0b2020-04-21 16:29:12 -070095 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -080096
Tony Wickhamf34bee82018-12-03 18:11:39 -080097 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
98 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -080099 @Override
100 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800101 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800102 }
103
104 @Override
105 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800106 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800107 bubbleTextView.invalidate();
108 }
109 };
110
Sunny Goyaled7a6932018-04-13 11:41:50 -0700111 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
112 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700113 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700114 public Float get(BubbleTextView bubbleTextView) {
115 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700116 }
117
118 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700119 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700120 bubbleTextView.setTextAlpha(alpha);
121 }
122 };
123
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800124 private final ActivityContext mActivity;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700125 private Drawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800126 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700127
Samuel Fufac96fa242019-09-26 23:06:32 -0700128 private final int mDisplay;
129
Sunny Goyaled7a6932018-04-13 11:41:50 -0700130 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700131
132 private final boolean mLayoutHorizontal;
133 private final int mIconSize;
134
135 @ViewDebug.ExportedProperty(category = "launcher")
136 private boolean mIsIconVisible = true;
137 @ViewDebug.ExportedProperty(category = "launcher")
138 private int mTextColor;
139 @ViewDebug.ExportedProperty(category = "launcher")
140 private float mTextAlpha = 1;
141
Tony4043b052018-08-17 13:47:39 -0700142 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800143 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800144 private DotRenderer mDotRenderer;
145 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
146 private DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800147 private Animator mDotScaleAnim;
148 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700149
Sunny Goyal4ffec482016-02-09 11:28:52 -0800150 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800151 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800152 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700153 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800154 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700155 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500156
Sunny Goyal34b65272015-03-11 16:56:52 -0700157 private IconLoadRequest mIconLoadRequest;
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700160 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 }
162
163 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700164 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 }
166
167 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
168 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800169 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700170
Adam Cohen96bb7982014-07-07 11:58:56 -0700171 TypedArray a = context.obtainStyledAttributes(attrs,
172 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800173 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700174 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700175
Samuel Fufac96fa242019-09-26 23:06:32 -0700176 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700177 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700178 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700179 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700180 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700181 defaultIconSize = grid.iconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700182 } else if (mDisplay == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700183 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
184 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700185 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700186 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700187 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700188 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700189 defaultIconSize = grid.folderChildIconSizePx;
Samuel Fufa4a53c702020-08-04 14:06:55 -0700190 } else if (mDisplay == DISPLAY_HERO_APP) {
191 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700192 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700193 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700194 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700195 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700196
Winson1f064272016-07-18 17:18:02 -0700197 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700198
199 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
200 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700201 a.recycle();
202
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800203 mLongPressHelper = new CheckLongPressHelper(this);
204
Tony Wickham8912b042018-11-29 15:28:53 -0800205 mDotParams = new DotRenderer.DrawParams();
206
Sunny Goyal66dccad2018-03-19 12:00:51 -0700207 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700208 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700209 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 }
211
Sunny Goyal66dccad2018-03-19 12:00:51 -0700212 @Override
213 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
214 // Disable marques when not focused to that, so that updating text does not cause relayout.
215 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
216 super.onFocusChanged(focused, direction, previouslyFocusedRect);
217 }
218
Jon Miranda52549442017-10-26 11:28:06 -0700219 /**
220 * Resets the view so it can be recycled.
221 */
222 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800223 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800224 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800225 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800226 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800227 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800228 setBackground(null);
Jon Miranda52549442017-10-26 11:28:06 -0700229 }
230
Tony Wickhamf34bee82018-12-03 18:11:39 -0800231 private void cancelDotScaleAnim() {
232 if (mDotScaleAnim != null) {
233 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700234 }
235 }
236
Tony Wickhamf34bee82018-12-03 18:11:39 -0800237 private void animateDotScale(float... dotScales) {
238 cancelDotScaleAnim();
239 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
240 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700241 @Override
242 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800243 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700244 }
245 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800246 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700247 }
248
Sunny Goyal95899162019-03-27 16:03:06 -0700249 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
250 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700251 }
252
Adam Cohen29794c52019-06-21 12:50:30 -0700253 @Override
254 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
255 if (delegate instanceof LauncherAccessibilityDelegate) {
256 super.setAccessibilityDelegate(delegate);
257 } else {
258 // NO-OP
259 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
260 // delegate incorrectly. There are no cases when we shouldn't be using the
261 // LauncherAccessibilityDelegate for BubbleTextView.
262 }
263 }
264
Sunny Goyal95899162019-03-27 16:03:06 -0700265 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800266 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800267 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700268 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800269 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500270 }
Tony Wickham010d2552017-01-20 08:15:28 -0800271
Tony Wickhamf34bee82018-12-03 18:11:39 -0800272 applyDotState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800273 }
274
Sunny Goyal508da152014-08-14 10:53:27 -0700275 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800276 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700277
Sunny Goyal95899162019-03-27 16:03:06 -0700278 // We don't need to check the info since it's not a WorkspaceItemInfo
Winson Chung888b3a12015-03-13 11:14:16 -0700279 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700280
281 // Verify high res immediately
282 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800283
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700284 if (info instanceof PromiseAppInfo) {
285 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
286 applyProgressLevel(promiseAppInfo.level);
287 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800288 applyDotState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700289 }
290
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700291 /**
292 * Apply label and tag using a generic {@link ItemInfoWithIcon}
293 */
294 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800295 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700296 // We don't need to check the info since it's not a WorkspaceItemInfo
Sunny Goyal0e08f162015-05-12 11:32:39 -0700297 super.setTag(info);
298
299 // Verify high res immediately
300 verifyHighRes();
301 }
302
Sunny Goyal179249d2017-12-19 16:49:24 -0800303 private void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal14168432019-10-24 15:59:49 -0700304 FastBitmapDrawable iconDrawable = newIcon(getContext(), info);
Sunny Goyal3808a692019-10-25 13:41:28 -0700305 mDotParams.color = IconPalette.getMutedColor(info.bitmap.color, 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800306
Sunny Goyalf4204382016-07-13 10:46:07 -0700307 setIcon(iconDrawable);
308 setText(info.title);
309 if (info.contentDescription != null) {
310 setContentDescription(info.isDisabled()
311 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
312 : info.contentDescription);
313 }
314 }
315
Winson Chung7501adf2015-06-02 11:24:28 -0700316 /**
317 * Overrides the default long press timeout.
318 */
Tony2ca999c2018-09-24 17:24:51 -0400319 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
320 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700321 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700322
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800324 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700325 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800326 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800327 }
Sunny Goyal508da152014-08-14 10:53:27 -0700328 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800329
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800330 @Override
331 protected int[] onCreateDrawableState(int extraSpace) {
332 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
333 if (mStayPressed) {
334 mergeDrawableStates(drawableState, STATE_PRESSED);
335 }
336 return drawableState;
337 }
338
Winson Chungb745afb2015-03-02 11:51:23 -0800339 /** Returns the icon for this view. */
340 public Drawable getIcon() {
341 return mIcon;
342 }
343
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700344 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800345 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700346 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700347 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700348 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700349 return false;
350 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700351 if (isLongClickable()) {
352 super.onTouchEvent(event);
353 mLongPressHelper.onTouchEvent(event);
354 // Keep receiving the rest of the events
355 return true;
356 } else {
357 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700358 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800359 }
360
Sunny Goyal44a3b202020-07-09 08:27:06 -0700361 /**
362 * Returns true if the touch down at the provided position be ignored
363 */
364 protected boolean shouldIgnoreTouchDown(float x, float y) {
365 return y < getPaddingTop()
366 || x < getPaddingLeft()
367 || y > getHeight() - getPaddingBottom()
368 || x > getWidth() - getPaddingRight();
369 }
370
Michael Jurkaddd62e92011-02-16 17:49:14 -0800371 void setStayPressed(boolean stayPressed) {
372 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800373 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800374 }
375
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700376 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800377 public void onVisibilityAggregated(boolean isVisible) {
378 super.onVisibilityAggregated(isVisible);
379 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800380 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800381 }
382 }
383
384 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700385 public void onLauncherResume() {
386 // Reset the pressed state of icon that was locked in the press state while activity
387 // was launching
388 setStayPressed(false);
389 }
390
Sunny Goyal508da152014-08-14 10:53:27 -0700391 void clearPressedBackground() {
392 setPressed(false);
393 setStayPressed(false);
394 }
395
396 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700397 public boolean onKeyUp(int keyCode, KeyEvent event) {
398 // Unlike touch events, keypress event propagate pressed state change immediately,
399 // without waiting for onClickHandler to execute. Disable pressed state changes here
400 // to avoid flickering.
401 mIgnorePressedStateChange = true;
402 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700403 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800404 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700405 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800406 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800407
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700408 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800409 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700410 super.onDraw(canvas);
411 }
412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700414 public void onDraw(Canvas canvas) {
415 super.onDraw(canvas);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800416 drawDotIfNecessary(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800417 }
418
419 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800420 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700421 *
Tony Wickham1237df02017-02-24 08:59:36 -0800422 * @param canvas The canvas to draw to.
423 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800424 protected void drawDotIfNecessary(Canvas canvas) {
425 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800426 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700427 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
428 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800429 final int scrollX = getScrollX();
430 final int scrollY = getScrollY();
431 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800432 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800433 canvas.translate(-scrollX, -scrollY);
434 }
435 }
436
Jon Miranda9b78e192019-08-30 18:42:01 -0700437 @Override
438 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800439 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800440 return;
441 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800442 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800443
Tony Wickhamf34bee82018-12-03 18:11:39 -0800444 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800445 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800446 } else if (hasDot()) {
447 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800448 }
449 }
450
Tony Wickhamf34bee82018-12-03 18:11:39 -0800451 private boolean hasDot() {
452 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800453 }
454
455 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700456 getIconBounds(this, outBounds, mIconSize);
457 }
458
459 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
460 int top = iconView.getPaddingTop();
461 int left = (iconView.getWidth() - iconSize) / 2;
462 int right = left + iconSize;
463 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800464 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400466
467 @Override
Winson1f064272016-07-18 17:18:02 -0700468 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
469 if (mCenterVertically) {
470 Paint.FontMetrics fm = getPaint().getFontMetrics();
471 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
472 (int) Math.ceil(fm.bottom - fm.top);
473 int height = MeasureSpec.getSize(heightMeasureSpec);
474 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
475 getPaddingBottom());
476 }
477 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
478 }
479
480 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700481 public void setTextColor(int color) {
482 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700483 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700484 }
485
Adam Cohen96bb7982014-07-07 11:58:56 -0700486 @Override
487 public void setTextColor(ColorStateList colors) {
488 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700489 if (Float.compare(mTextAlpha, 1) == 0) {
490 super.setTextColor(colors);
491 } else {
492 super.setTextColor(getModifiedColor());
493 }
Adam Cohen477828c2013-09-20 12:05:49 -0700494 }
495
Tony7308cde2017-06-27 22:38:33 -0700496 public boolean shouldTextBeVisible() {
497 // Text should be visible everywhere but the hotseat.
498 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
499 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700500 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
501 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700502 }
503
Winson Chung5f8afe62013-08-12 16:19:28 -0700504 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700505 setTextAlpha(visible ? 1 : 0);
506 }
507
508 private void setTextAlpha(float alpha) {
509 mTextAlpha = alpha;
510 super.setTextColor(getModifiedColor());
511 }
512
513 private int getModifiedColor() {
514 if (mTextAlpha == 0) {
515 // Special case to prevent text shadows in high contrast mode
516 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700517 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800518 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700519 }
520
521 /**
522 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700523 *
Tony8f402802017-06-16 17:24:54 -0700524 * @param fadeIn Whether the text should fade in or fade out.
525 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700526 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700527 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700528 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700529 }
530
Winson Chungaffd7b42010-08-20 15:11:56 -0700531 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800532 public void cancelLongPress() {
533 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800534 mLongPressHelper.cancelLongPress();
535 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500536
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800537 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyal95899162019-03-27 16:03:06 -0700538 if (getTag() instanceof WorkspaceItemInfo) {
539 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700540 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700541 final int progressLevel = isPromise ?
Sunny Goyal95899162019-03-27 16:03:06 -0700542 ((info.hasStatusFlag(WorkspaceItemInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
Sunny Goyal34942622014-08-29 17:20:55 -0700543 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700544
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700545 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
546 if (preloadDrawable != null && promiseStateChanged) {
547 preloadDrawable.maybePerformFinishedAnimation();
548 }
549 }
550 }
551
552 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
553 if (getTag() instanceof ItemInfoWithIcon) {
554 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700555 if (progressLevel >= 100) {
556 setContentDescription(info.contentDescription != null
557 ? info.contentDescription : "");
558 } else if (progressLevel > 0) {
559 setContentDescription(getContext()
560 .getString(R.string.app_downloading_title, info.title,
561 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
562 } else {
563 setContentDescription(getContext()
564 .getString(R.string.app_waiting_download_title, info.title));
565 }
Winson Chungb745afb2015-03-02 11:51:23 -0800566 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700567 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800568 if (mIcon instanceof PreloadIconDrawable) {
569 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700570 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700571 } else {
Sunny Goyal14168432019-10-24 15:59:49 -0700572 preloadDrawable = newPendingIcon(getContext(), info);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700573 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700574 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700575 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700576 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700577 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400578 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700579 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400580 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700581
Tony Wickhamf34bee82018-12-03 18:11:39 -0800582 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800583 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800584 boolean wasDotted = mDotInfo != null;
585 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
586 boolean isDotted = mDotInfo != null;
587 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700588 if (mDisplay == DISPLAY_ALL_APPS) {
589 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
590 } else {
591 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
592 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800593 if (wasDotted || isDotted) {
594 // Animate when a dot is first added or when it is removed.
595 if (animate && (wasDotted ^ isDotted) && isShown()) {
596 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800597 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800598 cancelDotScaleAnim();
599 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800600 invalidate();
601 }
602 }
Tony Wickham305e9202018-01-23 17:46:47 -0800603 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700604 if (itemInfo.isDisabled()) {
605 setContentDescription(getContext().getString(R.string.disabled_app_label,
606 itemInfo.contentDescription));
607 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800608 int count = mDotInfo.getNotificationCount();
Tony Wickham305e9202018-01-23 17:46:47 -0800609 setContentDescription(getContext().getResources().getQuantityString(
Tony Wickhamf34bee82018-12-03 18:11:39 -0800610 R.plurals.dotted_app_label, count, itemInfo.contentDescription, count));
Tony Wickham305e9202018-01-23 17:46:47 -0800611 } else {
612 setContentDescription(itemInfo.contentDescription);
613 }
614 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800615 }
616 }
617
Winson Chungb745afb2015-03-02 11:51:23 -0800618 /**
619 * Sets the icon for this view based on the layout direction.
620 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700621 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700622 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700623 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700624 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700625 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800626 if (mIcon != null) {
627 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
628 }
Tony17b7f9b2017-05-23 12:19:09 -0700629 }
630
Jon Miranda9b78e192019-08-30 18:42:01 -0700631 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700632 public void setIconVisible(boolean visible) {
633 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700634 if (!mIsIconVisible) {
635 resetIconScale();
636 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700637 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700638 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700639 }
640
641 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700642 // If we had already set an icon before, disable relayout as the icon size is the
643 // same as before.
644 mDisableRelayout = mIcon != null;
645
646 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700647
648 updateIcon(icon);
649
650 // If the current icon is a placeholder color, animate its update.
651 if (mIcon != null && mIcon instanceof PlaceHolderIconDrawable) {
652 animateIconUpdate((PlaceHolderIconDrawable) mIcon, icon);
Winson Chungb745afb2015-03-02 11:51:23 -0800653 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700654
Sunny Goyal66dccad2018-03-19 12:00:51 -0700655 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800656 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700657
Sunny Goyal69b75642015-05-15 17:00:24 -0700658 @Override
659 public void requestLayout() {
660 if (!mDisableRelayout) {
661 super.requestLayout();
662 }
663 }
664
Sunny Goyal34b65272015-03-11 16:56:52 -0700665 /**
666 * Applies the item info if it is same as what the view is pointing to currently.
667 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800668 @Override
669 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700670 if (getTag() == info) {
671 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700672 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700673
Jon Miranda7f522a22017-07-28 11:56:47 -0700674 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -0700675 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -0700676
Sunny Goyal34b65272015-03-11 16:56:52 -0700677 if (info instanceof AppInfo) {
678 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700679 } else if (info instanceof WorkspaceItemInfo) {
680 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700681 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700682 } else if (info instanceof PackageItemInfo) {
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700683 applyFromItemInfoWithIcon((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700684 }
Winsonc0880492015-08-21 11:16:27 -0700685
Sunny Goyal69b75642015-05-15 17:00:24 -0700686 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700687 }
688 }
689
690 /**
691 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
692 */
693 public void verifyHighRes() {
694 if (mIconLoadRequest != null) {
695 mIconLoadRequest.cancel();
696 mIconLoadRequest = null;
697 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800698 if (getTag() instanceof ItemInfoWithIcon) {
699 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700700 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800701 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700702 .updateIconInBackground(BubbleTextView.this, info);
703 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700704 }
705 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700706
Jon Miranda47170112017-03-03 14:57:14 -0800707 public int getIconSize() {
708 return mIconSize;
709 }
Adam Cohen65086992020-02-19 08:40:49 -0800710
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700711 private void updateTranslation() {
712 super.setTranslationX(mTranslationForReorderBounce.x + mTranslationForReorderPreview.x);
713 super.setTranslationY(mTranslationForReorderBounce.y + mTranslationForReorderPreview.y);
Adam Cohend9162062020-03-24 16:35:35 -0700714 }
715
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700716 public void setReorderBounceOffset(float x, float y) {
717 mTranslationForReorderBounce.set(x, y);
718 updateTranslation();
Adam Cohend9162062020-03-24 16:35:35 -0700719 }
720
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700721 public void getReorderBounceOffset(PointF offset) {
722 offset.set(mTranslationForReorderBounce);
723 }
724
725 @Override
726 public void setReorderPreviewOffset(float x, float y) {
727 mTranslationForReorderPreview.set(x, y);
728 updateTranslation();
729 }
730
731 @Override
732 public void getReorderPreviewOffset(PointF offset) {
733 offset.set(mTranslationForReorderPreview);
734 }
735
736 public void setReorderBounceScale(float scale) {
737 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -0700738 super.setScaleX(scale);
739 super.setScaleY(scale);
740 }
741
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700742 public float getReorderBounceScale() {
743 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -0700744 }
745
746 public View getView() {
747 return this;
748 }
749
Adam Cohen65086992020-02-19 08:40:49 -0800750 @Override
751 public int getViewType() {
752 return DRAGGABLE_ICON;
753 }
754
755 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -0700756 public void getWorkspaceVisualDragBounds(Rect bounds) {
Adam Cohen65086992020-02-19 08:40:49 -0800757 DeviceProfile grid = mActivity.getDeviceProfile();
758 BubbleTextView.getIconBounds(this, bounds, grid.iconSizePx);
759 }
760
Adam Cohenc77bc452020-05-07 11:55:19 -0700761 private int getIconSizeForDisplay(int display) {
762 DeviceProfile grid = mActivity.getDeviceProfile();
763 switch (display) {
764 case DISPLAY_ALL_APPS:
765 return grid.allAppsIconSizePx;
766 case DISPLAY_WORKSPACE:
767 case DISPLAY_FOLDER:
768 default:
769 return grid.iconSizePx;
770 }
771 }
772
773 public void getSourceVisualDragBounds(Rect bounds) {
774 BubbleTextView.getIconBounds(this, bounds, getIconSizeForDisplay(mDisplay));
775 }
776
Adam Cohen65086992020-02-19 08:40:49 -0800777 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -0700778 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700779 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -0800780 setForceHideDot(true);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700781 return () -> {
782 };
Adam Cohen65086992020-02-19 08:40:49 -0800783 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700784
785 private void resetIconScale() {
786 if (mIcon instanceof FastBitmapDrawable) {
787 ((FastBitmapDrawable) mIcon).setScale(1f);
788 }
789 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700790
791 private void updateIcon(Drawable newIcon) {
792 if (mLayoutHorizontal) {
793 setCompoundDrawablesRelative(newIcon, null, null, null);
794 } else {
795 setCompoundDrawables(null, newIcon, null, null);
796 }
797 }
798
799 private static void animateIconUpdate(PlaceHolderIconDrawable oldIcon, Drawable newIcon) {
800 int placeholderColor = oldIcon.mPaint.getColor();
801 int originalAlpha = Color.alpha(placeholderColor);
802
803 ValueAnimator iconUpdateAnimation = ValueAnimator.ofInt(originalAlpha, 0);
804 iconUpdateAnimation.setDuration(ICON_UPDATE_ANIMATION_DURATION);
805 iconUpdateAnimation.addUpdateListener(valueAnimator -> {
806 int newAlpha = (int) valueAnimator.getAnimatedValue();
807 int newColor = ColorUtils.setAlphaComponent(placeholderColor, newAlpha);
808
809 newIcon.setColorFilter(new PorterDuffColorFilter(newColor, Mode.SRC_ATOP));
810 });
811 iconUpdateAnimation.addListener(new AnimatorListenerAdapter() {
812 @Override
813 public void onAnimationEnd(Animator animation) {
814 newIcon.setColorFilter(null);
815 }
816 });
817 iconUpdateAnimation.start();
818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819}