blob: 1e5a9e44d50a694d552099cf06ad73e1bd5e1dc6 [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;
Samuel Fufa02be2a42020-09-23 00:45:27 -070068import com.android.launcher3.model.data.RemoteActionItemInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070069import com.android.launcher3.model.data.WorkspaceItemInfo;
Samuel Fufa167210a2020-05-12 18:40:11 -070070import com.android.launcher3.util.SafeCloseable;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080071import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070072import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070073
Sunny Goyalc469aad2015-10-01 11:24:23 -070074import java.text.NumberFormat;
75
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076/**
77 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
78 * because we want to make the bubble taller than the text and TextView's clip is
79 * too aggressive.
80 */
Jon Miranda9b78e192019-08-30 18:42:01 -070081public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback,
Adam Cohend9162062020-03-24 16:35:35 -070082 IconLabelDotView, DraggableView, Reorderable {
Sunny Goyal95abbb32014-08-04 10:53:22 -070083
Sunny Goyaldfaccf62015-05-11 16:30:44 -070084 private static final int DISPLAY_WORKSPACE = 0;
85 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070086 private static final int DISPLAY_FOLDER = 2;
Samuel Fufa4a53c702020-08-04 14:06:55 -070087 private static final int DISPLAY_HERO_APP = 5;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070088
Samuel Fufad6bacdc2020-08-23 21:56:07 -070089 private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed};
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080090
Adam Cohen1d13c0b2020-04-21 16:29:12 -070091 private final PointF mTranslationForReorderBounce = new PointF(0, 0);
92 private final PointF mTranslationForReorderPreview = new PointF(0, 0);
93
Schneider Victor-tulias350b4622020-09-18 11:16:30 -070094 private static final int ICON_UPDATE_ANIMATION_DURATION = 375;
95
Adam Cohen1d13c0b2020-04-21 16:29:12 -070096 private float mScaleForReorderBounce = 1f;
Tony Wickham1237df02017-02-24 08:59:36 -080097
Tony Wickhamf34bee82018-12-03 18:11:39 -080098 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
99 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -0800100 @Override
101 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -0800102 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -0800103 }
104
105 @Override
106 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -0800107 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -0800108 bubbleTextView.invalidate();
109 }
110 };
111
Sunny Goyaled7a6932018-04-13 11:41:50 -0700112 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
113 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -0700114 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700115 public Float get(BubbleTextView bubbleTextView) {
116 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700117 }
118
119 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700120 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700121 bubbleTextView.setTextAlpha(alpha);
122 }
123 };
124
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800125 private final ActivityContext mActivity;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700126 private Drawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800127 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700128
Samuel Fufac96fa242019-09-26 23:06:32 -0700129 private final int mDisplay;
130
Sunny Goyaled7a6932018-04-13 11:41:50 -0700131 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700132
133 private final boolean mLayoutHorizontal;
134 private final int mIconSize;
135
136 @ViewDebug.ExportedProperty(category = "launcher")
137 private boolean mIsIconVisible = true;
138 @ViewDebug.ExportedProperty(category = "launcher")
139 private int mTextColor;
140 @ViewDebug.ExportedProperty(category = "launcher")
141 private float mTextAlpha = 1;
142
Tony4043b052018-08-17 13:47:39 -0700143 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800144 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800145 private DotRenderer mDotRenderer;
146 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
147 private DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800148 private Animator mDotScaleAnim;
149 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700150
Sunny Goyal4ffec482016-02-09 11:28:52 -0800151 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800152 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800153 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700154 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800155 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700156 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500157
Sunny Goyal34b65272015-03-11 16:56:52 -0700158 private IconLoadRequest mIconLoadRequest;
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700161 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 }
163
164 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700165 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 }
167
168 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
169 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800170 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700171
Adam Cohen96bb7982014-07-07 11:58:56 -0700172 TypedArray a = context.obtainStyledAttributes(attrs,
173 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800174 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700175 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700176
Samuel Fufac96fa242019-09-26 23:06:32 -0700177 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700178 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700179 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700180 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700181 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700182 defaultIconSize = grid.iconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700183 } else if (mDisplay == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700184 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
185 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700186 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700187 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700188 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700189 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700190 defaultIconSize = grid.folderChildIconSizePx;
Samuel Fufa4a53c702020-08-04 14:06:55 -0700191 } else if (mDisplay == DISPLAY_HERO_APP) {
192 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700193 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700194 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700195 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700196 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700197
Winson1f064272016-07-18 17:18:02 -0700198 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700199
200 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
201 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700202 a.recycle();
203
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800204 mLongPressHelper = new CheckLongPressHelper(this);
205
Tony Wickham8912b042018-11-29 15:28:53 -0800206 mDotParams = new DotRenderer.DrawParams();
207
Sunny Goyal66dccad2018-03-19 12:00:51 -0700208 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700209 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700210 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 }
212
Sunny Goyal66dccad2018-03-19 12:00:51 -0700213 @Override
214 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
215 // Disable marques when not focused to that, so that updating text does not cause relayout.
216 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
217 super.onFocusChanged(focused, direction, previouslyFocusedRect);
218 }
219
Jon Miranda52549442017-10-26 11:28:06 -0700220 /**
221 * Resets the view so it can be recycled.
222 */
223 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800224 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800225 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800226 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800227 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800228 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800229 setBackground(null);
Jon Miranda52549442017-10-26 11:28:06 -0700230 }
231
Tony Wickhamf34bee82018-12-03 18:11:39 -0800232 private void cancelDotScaleAnim() {
233 if (mDotScaleAnim != null) {
234 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700235 }
236 }
237
Tony Wickhamf34bee82018-12-03 18:11:39 -0800238 private void animateDotScale(float... dotScales) {
239 cancelDotScaleAnim();
240 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
241 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700242 @Override
243 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800244 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700245 }
246 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800247 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700248 }
249
Sunny Goyal95899162019-03-27 16:03:06 -0700250 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
251 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700252 }
253
Adam Cohen29794c52019-06-21 12:50:30 -0700254 @Override
255 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
256 if (delegate instanceof LauncherAccessibilityDelegate) {
257 super.setAccessibilityDelegate(delegate);
258 } else {
259 // NO-OP
260 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
261 // delegate incorrectly. There are no cases when we shouldn't be using the
262 // LauncherAccessibilityDelegate for BubbleTextView.
263 }
264 }
265
Sunny Goyal95899162019-03-27 16:03:06 -0700266 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800267 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800268 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700269 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800270 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500271 }
Tony Wickham010d2552017-01-20 08:15:28 -0800272
Tony Wickhamf34bee82018-12-03 18:11:39 -0800273 applyDotState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800274 }
275
Sunny Goyal508da152014-08-14 10:53:27 -0700276 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800277 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700278
Sunny Goyal95899162019-03-27 16:03:06 -0700279 // We don't need to check the info since it's not a WorkspaceItemInfo
Winson Chung888b3a12015-03-13 11:14:16 -0700280 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700281
282 // Verify high res immediately
283 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800284
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700285 if (info instanceof PromiseAppInfo) {
286 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
287 applyProgressLevel(promiseAppInfo.level);
288 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800289 applyDotState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700290 }
291
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700292 /**
293 * Apply label and tag using a generic {@link ItemInfoWithIcon}
294 */
295 public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800296 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700297 // We don't need to check the info since it's not a WorkspaceItemInfo
Sunny Goyal0e08f162015-05-12 11:32:39 -0700298 super.setTag(info);
299
300 // Verify high res immediately
301 verifyHighRes();
302 }
303
Samuel Fufa02be2a42020-09-23 00:45:27 -0700304 /**
305 * Apply label and tag using a {@link RemoteActionItemInfo}
306 */
307 public void applyFromRemoteActionInfo(RemoteActionItemInfo remoteActionItemInfo) {
308 applyIconAndLabel(remoteActionItemInfo);
309 setTag(remoteActionItemInfo);
310 }
311
Sunny Goyal179249d2017-12-19 16:49:24 -0800312 private void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal14168432019-10-24 15:59:49 -0700313 FastBitmapDrawable iconDrawable = newIcon(getContext(), info);
Sunny Goyal3808a692019-10-25 13:41:28 -0700314 mDotParams.color = IconPalette.getMutedColor(info.bitmap.color, 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800315
Sunny Goyalf4204382016-07-13 10:46:07 -0700316 setIcon(iconDrawable);
317 setText(info.title);
318 if (info.contentDescription != null) {
319 setContentDescription(info.isDisabled()
320 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
321 : info.contentDescription);
322 }
323 }
324
Winson Chung7501adf2015-06-02 11:24:28 -0700325 /**
326 * Overrides the default long press timeout.
327 */
Tony2ca999c2018-09-24 17:24:51 -0400328 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
329 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700330 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800333 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700334 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800335 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800336 }
Sunny Goyal508da152014-08-14 10:53:27 -0700337 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800338
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800339 @Override
340 protected int[] onCreateDrawableState(int extraSpace) {
341 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
342 if (mStayPressed) {
343 mergeDrawableStates(drawableState, STATE_PRESSED);
344 }
345 return drawableState;
346 }
347
Winson Chungb745afb2015-03-02 11:51:23 -0800348 /** Returns the icon for this view. */
349 public Drawable getIcon() {
350 return mIcon;
351 }
352
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700353 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800354 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700355 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700356 if (event.getAction() == MotionEvent.ACTION_DOWN
Sunny Goyal44a3b202020-07-09 08:27:06 -0700357 && shouldIgnoreTouchDown(event.getX(), event.getY())) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700358 return false;
359 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700360 if (isLongClickable()) {
361 super.onTouchEvent(event);
362 mLongPressHelper.onTouchEvent(event);
363 // Keep receiving the rest of the events
364 return true;
365 } else {
366 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700367 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800368 }
369
Sunny Goyal44a3b202020-07-09 08:27:06 -0700370 /**
371 * Returns true if the touch down at the provided position be ignored
372 */
373 protected boolean shouldIgnoreTouchDown(float x, float y) {
374 return y < getPaddingTop()
375 || x < getPaddingLeft()
376 || y > getHeight() - getPaddingBottom()
377 || x > getWidth() - getPaddingRight();
378 }
379
Michael Jurkaddd62e92011-02-16 17:49:14 -0800380 void setStayPressed(boolean stayPressed) {
381 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800382 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800383 }
384
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700385 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800386 public void onVisibilityAggregated(boolean isVisible) {
387 super.onVisibilityAggregated(isVisible);
388 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800389 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800390 }
391 }
392
393 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700394 public void onLauncherResume() {
395 // Reset the pressed state of icon that was locked in the press state while activity
396 // was launching
397 setStayPressed(false);
398 }
399
Sunny Goyal508da152014-08-14 10:53:27 -0700400 void clearPressedBackground() {
401 setPressed(false);
402 setStayPressed(false);
403 }
404
405 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700406 public boolean onKeyUp(int keyCode, KeyEvent event) {
407 // Unlike touch events, keypress event propagate pressed state change immediately,
408 // without waiting for onClickHandler to execute. Disable pressed state changes here
409 // to avoid flickering.
410 mIgnorePressedStateChange = true;
411 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700412 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800413 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700414 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800415 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800416
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700417 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800418 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700419 super.onDraw(canvas);
420 }
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700423 public void onDraw(Canvas canvas) {
424 super.onDraw(canvas);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800425 drawDotIfNecessary(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800426 }
427
428 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800429 * Draws the notification dot in the top right corner of the icon bounds.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700430 *
Tony Wickham1237df02017-02-24 08:59:36 -0800431 * @param canvas The canvas to draw to.
432 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800433 protected void drawDotIfNecessary(Canvas canvas) {
434 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800435 getIconBounds(mDotParams.iconBounds);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700436 Utilities.scaleRectAboutCenter(mDotParams.iconBounds,
437 IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800438 final int scrollX = getScrollX();
439 final int scrollY = getScrollY();
440 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800441 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800442 canvas.translate(-scrollX, -scrollY);
443 }
444 }
445
Jon Miranda9b78e192019-08-30 18:42:01 -0700446 @Override
447 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800448 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800449 return;
450 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800451 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800452
Tony Wickhamf34bee82018-12-03 18:11:39 -0800453 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800454 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800455 } else if (hasDot()) {
456 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800457 }
458 }
459
Tony Wickhamf34bee82018-12-03 18:11:39 -0800460 private boolean hasDot() {
461 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800462 }
463
464 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700465 getIconBounds(this, outBounds, mIconSize);
466 }
467
468 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
469 int top = iconView.getPaddingTop();
470 int left = (iconView.getWidth() - iconSize) / 2;
471 int right = left + iconSize;
472 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800473 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400475
476 @Override
Winson1f064272016-07-18 17:18:02 -0700477 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
478 if (mCenterVertically) {
479 Paint.FontMetrics fm = getPaint().getFontMetrics();
480 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
481 (int) Math.ceil(fm.bottom - fm.top);
482 int height = MeasureSpec.getSize(heightMeasureSpec);
483 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
484 getPaddingBottom());
485 }
486 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
487 }
488
489 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700490 public void setTextColor(int color) {
491 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700492 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700493 }
494
Adam Cohen96bb7982014-07-07 11:58:56 -0700495 @Override
496 public void setTextColor(ColorStateList colors) {
497 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700498 if (Float.compare(mTextAlpha, 1) == 0) {
499 super.setTextColor(colors);
500 } else {
501 super.setTextColor(getModifiedColor());
502 }
Adam Cohen477828c2013-09-20 12:05:49 -0700503 }
504
Tony7308cde2017-06-27 22:38:33 -0700505 public boolean shouldTextBeVisible() {
506 // Text should be visible everywhere but the hotseat.
507 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
508 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700509 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
510 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700511 }
512
Winson Chung5f8afe62013-08-12 16:19:28 -0700513 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700514 setTextAlpha(visible ? 1 : 0);
515 }
516
517 private void setTextAlpha(float alpha) {
518 mTextAlpha = alpha;
519 super.setTextColor(getModifiedColor());
520 }
521
522 private int getModifiedColor() {
523 if (mTextAlpha == 0) {
524 // Special case to prevent text shadows in high contrast mode
525 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700526 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800527 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700528 }
529
530 /**
531 * Creates an animator to fade the text in or out.
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700532 *
Tony8f402802017-06-16 17:24:54 -0700533 * @param fadeIn Whether the text should fade in or fade out.
534 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700535 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700536 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700537 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700538 }
539
Winson Chungaffd7b42010-08-20 15:11:56 -0700540 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800541 public void cancelLongPress() {
542 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800543 mLongPressHelper.cancelLongPress();
544 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500545
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800546 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyal95899162019-03-27 16:03:06 -0700547 if (getTag() instanceof WorkspaceItemInfo) {
548 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700549 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700550 final int progressLevel = isPromise ?
Sunny Goyal95899162019-03-27 16:03:06 -0700551 ((info.hasStatusFlag(WorkspaceItemInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
Sunny Goyal34942622014-08-29 17:20:55 -0700552 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700553
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700554 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
555 if (preloadDrawable != null && promiseStateChanged) {
556 preloadDrawable.maybePerformFinishedAnimation();
557 }
558 }
559 }
560
561 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
562 if (getTag() instanceof ItemInfoWithIcon) {
563 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700564 if (progressLevel >= 100) {
565 setContentDescription(info.contentDescription != null
566 ? info.contentDescription : "");
567 } else if (progressLevel > 0) {
568 setContentDescription(getContext()
569 .getString(R.string.app_downloading_title, info.title,
570 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
571 } else {
572 setContentDescription(getContext()
573 .getString(R.string.app_waiting_download_title, info.title));
574 }
Winson Chungb745afb2015-03-02 11:51:23 -0800575 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700576 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800577 if (mIcon instanceof PreloadIconDrawable) {
578 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700579 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700580 } else {
Sunny Goyal14168432019-10-24 15:59:49 -0700581 preloadDrawable = newPendingIcon(getContext(), info);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700582 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700583 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700584 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700585 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700586 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400587 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700588 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400589 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700590
Tony Wickhamf34bee82018-12-03 18:11:39 -0800591 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800592 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800593 boolean wasDotted = mDotInfo != null;
594 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
595 boolean isDotted = mDotInfo != null;
596 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700597 if (mDisplay == DISPLAY_ALL_APPS) {
598 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
599 } else {
600 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
601 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800602 if (wasDotted || isDotted) {
603 // Animate when a dot is first added or when it is removed.
604 if (animate && (wasDotted ^ isDotted) && isShown()) {
605 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800606 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800607 cancelDotScaleAnim();
608 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800609 invalidate();
610 }
611 }
Tony Wickham305e9202018-01-23 17:46:47 -0800612 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700613 if (itemInfo.isDisabled()) {
614 setContentDescription(getContext().getString(R.string.disabled_app_label,
615 itemInfo.contentDescription));
616 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800617 int count = mDotInfo.getNotificationCount();
Tony Wickham305e9202018-01-23 17:46:47 -0800618 setContentDescription(getContext().getResources().getQuantityString(
Tony Wickhamf34bee82018-12-03 18:11:39 -0800619 R.plurals.dotted_app_label, count, itemInfo.contentDescription, count));
Tony Wickham305e9202018-01-23 17:46:47 -0800620 } else {
621 setContentDescription(itemInfo.contentDescription);
622 }
623 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800624 }
625 }
626
Winson Chungb745afb2015-03-02 11:51:23 -0800627 /**
628 * Sets the icon for this view based on the layout direction.
629 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700630 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700631 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700632 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700633 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700634 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800635 if (mIcon != null) {
636 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
637 }
Tony17b7f9b2017-05-23 12:19:09 -0700638 }
639
Jon Miranda9b78e192019-08-30 18:42:01 -0700640 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700641 public void setIconVisible(boolean visible) {
642 mIsIconVisible = visible;
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700643 if (!mIsIconVisible) {
644 resetIconScale();
645 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700646 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700647 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700648 }
649
650 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700651 // If we had already set an icon before, disable relayout as the icon size is the
652 // same as before.
653 mDisableRelayout = mIcon != null;
654
655 icon.setBounds(0, 0, mIconSize, mIconSize);
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700656
657 updateIcon(icon);
658
659 // If the current icon is a placeholder color, animate its update.
660 if (mIcon != null && mIcon instanceof PlaceHolderIconDrawable) {
661 animateIconUpdate((PlaceHolderIconDrawable) mIcon, icon);
Winson Chungb745afb2015-03-02 11:51:23 -0800662 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700663
Sunny Goyal66dccad2018-03-19 12:00:51 -0700664 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800665 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700666
Sunny Goyal69b75642015-05-15 17:00:24 -0700667 @Override
668 public void requestLayout() {
669 if (!mDisableRelayout) {
670 super.requestLayout();
671 }
672 }
673
Sunny Goyal34b65272015-03-11 16:56:52 -0700674 /**
675 * Applies the item info if it is same as what the view is pointing to currently.
676 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800677 @Override
678 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700679 if (getTag() == info) {
680 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700681 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700682
Jon Miranda7f522a22017-07-28 11:56:47 -0700683 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -0700684 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -0700685
Sunny Goyal34b65272015-03-11 16:56:52 -0700686 if (info instanceof AppInfo) {
687 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700688 } else if (info instanceof WorkspaceItemInfo) {
689 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700690 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700691 } else if (info instanceof PackageItemInfo) {
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700692 applyFromItemInfoWithIcon((PackageItemInfo) info);
Samuel Fufa02be2a42020-09-23 00:45:27 -0700693 } else if (info instanceof RemoteActionItemInfo) {
694 applyFromRemoteActionInfo((RemoteActionItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700695 }
Winsonc0880492015-08-21 11:16:27 -0700696
Sunny Goyal69b75642015-05-15 17:00:24 -0700697 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700698 }
699 }
700
701 /**
702 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
703 */
704 public void verifyHighRes() {
705 if (mIconLoadRequest != null) {
706 mIconLoadRequest.cancel();
707 mIconLoadRequest = null;
708 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800709 if (getTag() instanceof ItemInfoWithIcon) {
710 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700711 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800712 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700713 .updateIconInBackground(BubbleTextView.this, info);
714 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700715 }
716 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700717
Jon Miranda47170112017-03-03 14:57:14 -0800718 public int getIconSize() {
719 return mIconSize;
720 }
Adam Cohen65086992020-02-19 08:40:49 -0800721
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700722 private void updateTranslation() {
723 super.setTranslationX(mTranslationForReorderBounce.x + mTranslationForReorderPreview.x);
724 super.setTranslationY(mTranslationForReorderBounce.y + mTranslationForReorderPreview.y);
Adam Cohend9162062020-03-24 16:35:35 -0700725 }
726
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700727 public void setReorderBounceOffset(float x, float y) {
728 mTranslationForReorderBounce.set(x, y);
729 updateTranslation();
Adam Cohend9162062020-03-24 16:35:35 -0700730 }
731
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700732 public void getReorderBounceOffset(PointF offset) {
733 offset.set(mTranslationForReorderBounce);
734 }
735
736 @Override
737 public void setReorderPreviewOffset(float x, float y) {
738 mTranslationForReorderPreview.set(x, y);
739 updateTranslation();
740 }
741
742 @Override
743 public void getReorderPreviewOffset(PointF offset) {
744 offset.set(mTranslationForReorderPreview);
745 }
746
747 public void setReorderBounceScale(float scale) {
748 mScaleForReorderBounce = scale;
Adam Cohend9162062020-03-24 16:35:35 -0700749 super.setScaleX(scale);
750 super.setScaleY(scale);
751 }
752
Adam Cohen1d13c0b2020-04-21 16:29:12 -0700753 public float getReorderBounceScale() {
754 return mScaleForReorderBounce;
Adam Cohend9162062020-03-24 16:35:35 -0700755 }
756
757 public View getView() {
758 return this;
759 }
760
Adam Cohen65086992020-02-19 08:40:49 -0800761 @Override
762 public int getViewType() {
763 return DRAGGABLE_ICON;
764 }
765
766 @Override
Adam Cohenc77bc452020-05-07 11:55:19 -0700767 public void getWorkspaceVisualDragBounds(Rect bounds) {
Adam Cohen65086992020-02-19 08:40:49 -0800768 DeviceProfile grid = mActivity.getDeviceProfile();
769 BubbleTextView.getIconBounds(this, bounds, grid.iconSizePx);
770 }
771
Adam Cohenc77bc452020-05-07 11:55:19 -0700772 private int getIconSizeForDisplay(int display) {
773 DeviceProfile grid = mActivity.getDeviceProfile();
774 switch (display) {
775 case DISPLAY_ALL_APPS:
776 return grid.allAppsIconSizePx;
777 case DISPLAY_WORKSPACE:
778 case DISPLAY_FOLDER:
779 default:
780 return grid.iconSizePx;
781 }
782 }
783
784 public void getSourceVisualDragBounds(Rect bounds) {
785 BubbleTextView.getIconBounds(this, bounds, getIconSizeForDisplay(mDisplay));
786 }
787
Adam Cohen65086992020-02-19 08:40:49 -0800788 @Override
Samuel Fufa167210a2020-05-12 18:40:11 -0700789 public SafeCloseable prepareDrawDragView() {
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700790 resetIconScale();
Adam Cohen65086992020-02-19 08:40:49 -0800791 setForceHideDot(true);
Samuel Fufad6bacdc2020-08-23 21:56:07 -0700792 return () -> {
793 };
Adam Cohen65086992020-02-19 08:40:49 -0800794 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700795
796 private void resetIconScale() {
797 if (mIcon instanceof FastBitmapDrawable) {
798 ((FastBitmapDrawable) mIcon).setScale(1f);
799 }
800 }
Schneider Victor-tulias350b4622020-09-18 11:16:30 -0700801
802 private void updateIcon(Drawable newIcon) {
803 if (mLayoutHorizontal) {
804 setCompoundDrawablesRelative(newIcon, null, null, null);
805 } else {
806 setCompoundDrawables(null, newIcon, null, null);
807 }
808 }
809
810 private static void animateIconUpdate(PlaceHolderIconDrawable oldIcon, Drawable newIcon) {
811 int placeholderColor = oldIcon.mPaint.getColor();
812 int originalAlpha = Color.alpha(placeholderColor);
813
814 ValueAnimator iconUpdateAnimation = ValueAnimator.ofInt(originalAlpha, 0);
815 iconUpdateAnimation.setDuration(ICON_UPDATE_ANIMATION_DURATION);
816 iconUpdateAnimation.addUpdateListener(valueAnimator -> {
817 int newAlpha = (int) valueAnimator.getAnimatedValue();
818 int newColor = ColorUtils.setAlphaComponent(placeholderColor, newAlpha);
819
820 newIcon.setColorFilter(new PorterDuffColorFilter(newColor, Mode.SRC_ATOP));
821 });
822 iconUpdateAnimation.addListener(new AnimatorListenerAdapter() {
823 @Override
824 public void onAnimationEnd(Animator animation) {
825 newIcon.setColorFilter(null);
826 }
827 });
828 iconUpdateAnimation.start();
829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830}