blob: 93247abe6c455dd5f1d866679f68f397db648667 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070027import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070028import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070030import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070031import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080032import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070033import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.graphics.drawable.Drawable;
Sunny Goyal66dccad2018-03-19 12:00:51 -070035import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080036import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080037import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070038import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070039import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080040import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070041import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080042import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080043import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070044
Sunny Goyal3dce5f32017-10-05 11:40:05 -070045import com.android.launcher3.Launcher.OnResumeCallback;
Adam Cohen29794c52019-06-21 12:50:30 -070046import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Tony Wickhamf34bee82018-12-03 18:11:39 -080047import com.android.launcher3.dot.DotInfo;
Adam Cohen65086992020-02-19 08:40:49 -080048import com.android.launcher3.dragndrop.DraggableView;
Tony7308cde2017-06-27 22:38:33 -070049import com.android.launcher3.folder.FolderIcon;
Tony Wickham1237df02017-02-24 08:59:36 -080050import com.android.launcher3.graphics.IconPalette;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070051import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080052import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080053import com.android.launcher3.icons.DotRenderer;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080054import com.android.launcher3.icons.IconCache.IconLoadRequest;
55import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Sunny Goyale396abf2020-04-06 15:11:17 -070056import com.android.launcher3.model.data.AppInfo;
57import com.android.launcher3.model.data.ItemInfo;
58import com.android.launcher3.model.data.ItemInfoWithIcon;
59import com.android.launcher3.model.data.PackageItemInfo;
60import com.android.launcher3.model.data.PromiseAppInfo;
61import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080062import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070063import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070064
Sunny Goyalc469aad2015-10-01 11:24:23 -070065import java.text.NumberFormat;
66
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067/**
68 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
69 * because we want to make the bubble taller than the text and TextView's clip is
70 * too aggressive.
71 */
Jon Miranda9b78e192019-08-30 18:42:01 -070072public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback,
Adam Cohen65086992020-02-19 08:40:49 -080073 IconLabelDotView, DraggableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070074
Sunny Goyaldfaccf62015-05-11 16:30:44 -070075 private static final int DISPLAY_WORKSPACE = 0;
76 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070077 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070078
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080079 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
80
Tony Wickham1237df02017-02-24 08:59:36 -080081
Tony Wickhamf34bee82018-12-03 18:11:39 -080082 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
83 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -080084 @Override
85 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -080086 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -080087 }
88
89 @Override
90 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -080091 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -080092 bubbleTextView.invalidate();
93 }
94 };
95
Sunny Goyaled7a6932018-04-13 11:41:50 -070096 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
97 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -070098 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070099 public Float get(BubbleTextView bubbleTextView) {
100 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -0700101 }
102
103 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -0700104 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700105 bubbleTextView.setTextAlpha(alpha);
106 }
107 };
108
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800109 private final ActivityContext mActivity;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700110 private Drawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800111 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700112
Samuel Fufac96fa242019-09-26 23:06:32 -0700113 private final int mDisplay;
114
Sunny Goyaled7a6932018-04-13 11:41:50 -0700115 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700116
117 private final boolean mLayoutHorizontal;
118 private final int mIconSize;
119
120 @ViewDebug.ExportedProperty(category = "launcher")
121 private boolean mIsIconVisible = true;
122 @ViewDebug.ExportedProperty(category = "launcher")
123 private int mTextColor;
124 @ViewDebug.ExportedProperty(category = "launcher")
125 private float mTextAlpha = 1;
126
Tony4043b052018-08-17 13:47:39 -0700127 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800128 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800129 private DotRenderer mDotRenderer;
130 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
131 private DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800132 private Animator mDotScaleAnim;
133 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700134
Sunny Goyal4ffec482016-02-09 11:28:52 -0800135 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800136 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800137 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700138 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800139 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700140 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500141
Sunny Goyal34b65272015-03-11 16:56:52 -0700142 private IconLoadRequest mIconLoadRequest;
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700145 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 }
147
148 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700149 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 }
151
152 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
153 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800154 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700155
Adam Cohen96bb7982014-07-07 11:58:56 -0700156 TypedArray a = context.obtainStyledAttributes(attrs,
157 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800158 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyalc4d32012020-04-03 17:10:11 -0700159 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700160
Samuel Fufac96fa242019-09-26 23:06:32 -0700161 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700162 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700163 if (mDisplay == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700164 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700165 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700166 defaultIconSize = grid.iconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700167 } else if (mDisplay == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700168 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
169 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700170 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700171 } else if (mDisplay == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700172 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700173 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700174 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700175 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700176 // widget_selection or shortcut_popup
Sunny Goyalc4d32012020-04-03 17:10:11 -0700177 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700178 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700179
Winson1f064272016-07-18 17:18:02 -0700180 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700181
182 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
183 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700184 a.recycle();
185
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800186 mLongPressHelper = new CheckLongPressHelper(this);
187
Tony Wickham8912b042018-11-29 15:28:53 -0800188 mDotParams = new DotRenderer.DrawParams();
189
Sunny Goyal66dccad2018-03-19 12:00:51 -0700190 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700191 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700192 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
194
Sunny Goyal66dccad2018-03-19 12:00:51 -0700195 @Override
196 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
197 // Disable marques when not focused to that, so that updating text does not cause relayout.
198 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
199 super.onFocusChanged(focused, direction, previouslyFocusedRect);
200 }
201
Jon Miranda52549442017-10-26 11:28:06 -0700202 /**
203 * Resets the view so it can be recycled.
204 */
205 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800206 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800207 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800208 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800209 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800210 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800211 setBackground(null);
Jon Miranda52549442017-10-26 11:28:06 -0700212 }
213
Tony Wickhamf34bee82018-12-03 18:11:39 -0800214 private void cancelDotScaleAnim() {
215 if (mDotScaleAnim != null) {
216 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700217 }
218 }
219
Tony Wickhamf34bee82018-12-03 18:11:39 -0800220 private void animateDotScale(float... dotScales) {
221 cancelDotScaleAnim();
222 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
223 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700224 @Override
225 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800226 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700227 }
228 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800229 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700230 }
231
Sunny Goyal95899162019-03-27 16:03:06 -0700232 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
233 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700234 }
235
Adam Cohen29794c52019-06-21 12:50:30 -0700236 @Override
237 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
238 if (delegate instanceof LauncherAccessibilityDelegate) {
239 super.setAccessibilityDelegate(delegate);
240 } else {
241 // NO-OP
242 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
243 // delegate incorrectly. There are no cases when we shouldn't be using the
244 // LauncherAccessibilityDelegate for BubbleTextView.
245 }
246 }
247
Sunny Goyal95899162019-03-27 16:03:06 -0700248 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800249 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800250 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700251 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800252 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500253 }
Tony Wickham010d2552017-01-20 08:15:28 -0800254
Tony Wickhamf34bee82018-12-03 18:11:39 -0800255 applyDotState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800256 }
257
Sunny Goyal508da152014-08-14 10:53:27 -0700258 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800259 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700260
Sunny Goyal95899162019-03-27 16:03:06 -0700261 // We don't need to check the info since it's not a WorkspaceItemInfo
Winson Chung888b3a12015-03-13 11:14:16 -0700262 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700263
264 // Verify high res immediately
265 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800266
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700267 if (info instanceof PromiseAppInfo) {
268 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
269 applyProgressLevel(promiseAppInfo.level);
270 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800271 applyDotState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700272 }
273
Sunny Goyal0e08f162015-05-12 11:32:39 -0700274 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800275 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700276 // We don't need to check the info since it's not a WorkspaceItemInfo
Sunny Goyal0e08f162015-05-12 11:32:39 -0700277 super.setTag(info);
278
279 // Verify high res immediately
280 verifyHighRes();
281 }
282
Sunny Goyal179249d2017-12-19 16:49:24 -0800283 private void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal14168432019-10-24 15:59:49 -0700284 FastBitmapDrawable iconDrawable = newIcon(getContext(), info);
Sunny Goyal3808a692019-10-25 13:41:28 -0700285 mDotParams.color = IconPalette.getMutedColor(info.bitmap.color, 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800286
Sunny Goyalf4204382016-07-13 10:46:07 -0700287 setIcon(iconDrawable);
288 setText(info.title);
289 if (info.contentDescription != null) {
290 setContentDescription(info.isDisabled()
291 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
292 : info.contentDescription);
293 }
294 }
295
Winson Chung7501adf2015-06-02 11:24:28 -0700296 /**
297 * Overrides the default long press timeout.
298 */
Tony2ca999c2018-09-24 17:24:51 -0400299 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
300 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700301 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800304 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700305 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800306 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800307 }
Sunny Goyal508da152014-08-14 10:53:27 -0700308 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800309
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800310 @Override
311 protected int[] onCreateDrawableState(int extraSpace) {
312 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
313 if (mStayPressed) {
314 mergeDrawableStates(drawableState, STATE_PRESSED);
315 }
316 return drawableState;
317 }
318
Winson Chungb745afb2015-03-02 11:51:23 -0800319 /** Returns the icon for this view. */
320 public Drawable getIcon() {
321 return mIcon;
322 }
323
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700324 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800325 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700326 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700327 if (event.getAction() == MotionEvent.ACTION_DOWN
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700328 && (event.getY() < getPaddingTop()
329 || event.getX() < getPaddingLeft()
330 || event.getY() > getHeight() - getPaddingBottom()
331 || event.getX() > getWidth() - getPaddingRight())) {
332 return false;
333 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700334 if (isLongClickable()) {
335 super.onTouchEvent(event);
336 mLongPressHelper.onTouchEvent(event);
337 // Keep receiving the rest of the events
338 return true;
339 } else {
340 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700341 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800342 }
343
Michael Jurkaddd62e92011-02-16 17:49:14 -0800344 void setStayPressed(boolean stayPressed) {
345 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800346 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800347 }
348
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700349 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800350 public void onVisibilityAggregated(boolean isVisible) {
351 super.onVisibilityAggregated(isVisible);
352 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800353 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800354 }
355 }
356
357 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700358 public void onLauncherResume() {
359 // Reset the pressed state of icon that was locked in the press state while activity
360 // was launching
361 setStayPressed(false);
362 }
363
Sunny Goyal508da152014-08-14 10:53:27 -0700364 void clearPressedBackground() {
365 setPressed(false);
366 setStayPressed(false);
367 }
368
369 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700370 public boolean onKeyUp(int keyCode, KeyEvent event) {
371 // Unlike touch events, keypress event propagate pressed state change immediately,
372 // without waiting for onClickHandler to execute. Disable pressed state changes here
373 // to avoid flickering.
374 mIgnorePressedStateChange = true;
375 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700376 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800377 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700378 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800379 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800380
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700381 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800382 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700383 super.onDraw(canvas);
384 }
385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700387 public void onDraw(Canvas canvas) {
388 super.onDraw(canvas);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800389 drawDotIfNecessary(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800390 }
391
392 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800393 * Draws the notification dot in the top right corner of the icon bounds.
Tony Wickham1237df02017-02-24 08:59:36 -0800394 * @param canvas The canvas to draw to.
395 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800396 protected void drawDotIfNecessary(Canvas canvas) {
397 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800398 getIconBounds(mDotParams.iconBounds);
Tony Wickhame1cb93f2019-05-03 11:27:32 -0700399 Utilities.scaleRectAboutCenter(mDotParams.iconBounds, IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800400 final int scrollX = getScrollX();
401 final int scrollY = getScrollY();
402 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800403 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800404 canvas.translate(-scrollX, -scrollY);
405 }
406 }
407
Jon Miranda9b78e192019-08-30 18:42:01 -0700408 @Override
409 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800410 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800411 return;
412 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800413 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800414
Tony Wickhamf34bee82018-12-03 18:11:39 -0800415 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800416 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800417 } else if (hasDot()) {
418 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800419 }
420 }
421
Tony Wickhamf34bee82018-12-03 18:11:39 -0800422 private boolean hasDot() {
423 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800424 }
425
426 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700427 getIconBounds(this, outBounds, mIconSize);
428 }
429
430 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
431 int top = iconView.getPaddingTop();
432 int left = (iconView.getWidth() - iconSize) / 2;
433 int right = left + iconSize;
434 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800435 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400437
438 @Override
Winson1f064272016-07-18 17:18:02 -0700439 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
440 if (mCenterVertically) {
441 Paint.FontMetrics fm = getPaint().getFontMetrics();
442 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
443 (int) Math.ceil(fm.bottom - fm.top);
444 int height = MeasureSpec.getSize(heightMeasureSpec);
445 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
446 getPaddingBottom());
447 }
448 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
449 }
450
451 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700452 public void setTextColor(int color) {
453 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700454 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700455 }
456
Adam Cohen96bb7982014-07-07 11:58:56 -0700457 @Override
458 public void setTextColor(ColorStateList colors) {
459 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700460 if (Float.compare(mTextAlpha, 1) == 0) {
461 super.setTextColor(colors);
462 } else {
463 super.setTextColor(getModifiedColor());
464 }
Adam Cohen477828c2013-09-20 12:05:49 -0700465 }
466
Tony7308cde2017-06-27 22:38:33 -0700467 public boolean shouldTextBeVisible() {
468 // Text should be visible everywhere but the hotseat.
469 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
470 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700471 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
472 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700473 }
474
Winson Chung5f8afe62013-08-12 16:19:28 -0700475 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700476 setTextAlpha(visible ? 1 : 0);
477 }
478
479 private void setTextAlpha(float alpha) {
480 mTextAlpha = alpha;
481 super.setTextColor(getModifiedColor());
482 }
483
484 private int getModifiedColor() {
485 if (mTextAlpha == 0) {
486 // Special case to prevent text shadows in high contrast mode
487 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700488 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800489 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700490 }
491
492 /**
493 * Creates an animator to fade the text in or out.
494 * @param fadeIn Whether the text should fade in or fade out.
495 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700496 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700497 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700498 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700499 }
500
Winson Chungaffd7b42010-08-20 15:11:56 -0700501 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800502 public void cancelLongPress() {
503 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800504 mLongPressHelper.cancelLongPress();
505 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500506
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800507 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyal95899162019-03-27 16:03:06 -0700508 if (getTag() instanceof WorkspaceItemInfo) {
509 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700510 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700511 final int progressLevel = isPromise ?
Sunny Goyal95899162019-03-27 16:03:06 -0700512 ((info.hasStatusFlag(WorkspaceItemInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
Sunny Goyal34942622014-08-29 17:20:55 -0700513 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700514
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700515 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
516 if (preloadDrawable != null && promiseStateChanged) {
517 preloadDrawable.maybePerformFinishedAnimation();
518 }
519 }
520 }
521
522 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
523 if (getTag() instanceof ItemInfoWithIcon) {
524 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700525 if (progressLevel >= 100) {
526 setContentDescription(info.contentDescription != null
527 ? info.contentDescription : "");
528 } else if (progressLevel > 0) {
529 setContentDescription(getContext()
530 .getString(R.string.app_downloading_title, info.title,
531 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
532 } else {
533 setContentDescription(getContext()
534 .getString(R.string.app_waiting_download_title, info.title));
535 }
Winson Chungb745afb2015-03-02 11:51:23 -0800536 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700537 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800538 if (mIcon instanceof PreloadIconDrawable) {
539 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700540 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700541 } else {
Sunny Goyal14168432019-10-24 15:59:49 -0700542 preloadDrawable = newPendingIcon(getContext(), info);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700543 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700544 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700545 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700546 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700547 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400548 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700549 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400550 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700551
Tony Wickhamf34bee82018-12-03 18:11:39 -0800552 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800553 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800554 boolean wasDotted = mDotInfo != null;
555 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
556 boolean isDotted = mDotInfo != null;
557 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700558 if (mDisplay == DISPLAY_ALL_APPS) {
559 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
560 } else {
561 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
562 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800563 if (wasDotted || isDotted) {
564 // Animate when a dot is first added or when it is removed.
565 if (animate && (wasDotted ^ isDotted) && isShown()) {
566 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800567 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800568 cancelDotScaleAnim();
569 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800570 invalidate();
571 }
572 }
Tony Wickham305e9202018-01-23 17:46:47 -0800573 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700574 if (itemInfo.isDisabled()) {
575 setContentDescription(getContext().getString(R.string.disabled_app_label,
576 itemInfo.contentDescription));
577 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800578 int count = mDotInfo.getNotificationCount();
Tony Wickham305e9202018-01-23 17:46:47 -0800579 setContentDescription(getContext().getResources().getQuantityString(
Tony Wickhamf34bee82018-12-03 18:11:39 -0800580 R.plurals.dotted_app_label, count, itemInfo.contentDescription, count));
Tony Wickham305e9202018-01-23 17:46:47 -0800581 } else {
582 setContentDescription(itemInfo.contentDescription);
583 }
584 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800585 }
586 }
587
Winson Chungb745afb2015-03-02 11:51:23 -0800588 /**
589 * Sets the icon for this view based on the layout direction.
590 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700591 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700592 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700593 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700594 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700595 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800596 if (mIcon != null) {
597 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
598 }
Tony17b7f9b2017-05-23 12:19:09 -0700599 }
600
Jon Miranda9b78e192019-08-30 18:42:01 -0700601 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700602 public void setIconVisible(boolean visible) {
603 mIsIconVisible = visible;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700604 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700605 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700606 }
607
608 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700609 // If we had already set an icon before, disable relayout as the icon size is the
610 // same as before.
611 mDisableRelayout = mIcon != null;
612
613 icon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800614 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800615 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800616 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700617 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800618 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700619 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800620 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700621
Sunny Goyal69b75642015-05-15 17:00:24 -0700622 @Override
623 public void requestLayout() {
624 if (!mDisableRelayout) {
625 super.requestLayout();
626 }
627 }
628
Sunny Goyal34b65272015-03-11 16:56:52 -0700629 /**
630 * Applies the item info if it is same as what the view is pointing to currently.
631 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800632 @Override
633 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700634 if (getTag() == info) {
635 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700636 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700637
Jon Miranda7f522a22017-07-28 11:56:47 -0700638 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -0700639 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -0700640
Sunny Goyal34b65272015-03-11 16:56:52 -0700641 if (info instanceof AppInfo) {
642 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700643 } else if (info instanceof WorkspaceItemInfo) {
644 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700645 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700646 } else if (info instanceof PackageItemInfo) {
647 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700648 }
Winsonc0880492015-08-21 11:16:27 -0700649
Sunny Goyal69b75642015-05-15 17:00:24 -0700650 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700651 }
652 }
653
654 /**
655 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
656 */
657 public void verifyHighRes() {
658 if (mIconLoadRequest != null) {
659 mIconLoadRequest.cancel();
660 mIconLoadRequest = null;
661 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800662 if (getTag() instanceof ItemInfoWithIcon) {
663 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700664 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800665 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700666 .updateIconInBackground(BubbleTextView.this, info);
667 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700668 }
669 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700670
Jon Miranda47170112017-03-03 14:57:14 -0800671 public int getIconSize() {
672 return mIconSize;
673 }
Adam Cohen65086992020-02-19 08:40:49 -0800674
675 @Override
676 public int getViewType() {
677 return DRAGGABLE_ICON;
678 }
679
680 @Override
681 public void getVisualDragBounds(Rect bounds) {
682 DeviceProfile grid = mActivity.getDeviceProfile();
683 BubbleTextView.getIconBounds(this, bounds, grid.iconSizePx);
684 }
685
686 @Override
687 public void prepareDrawDragView() {
688 if (getIcon() instanceof FastBitmapDrawable) {
689 FastBitmapDrawable icon = (FastBitmapDrawable) getIcon();
690 icon.setScale(1f);
691 }
692 setForceHideDot(true);
693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694}