blob: c8e73ba8d4a44a611463ba64e1a3b8cb7e8df71f [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;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070056import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080057import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070058import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070059
Sunny Goyalc469aad2015-10-01 11:24:23 -070060import java.text.NumberFormat;
61
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062/**
63 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
64 * because we want to make the bubble taller than the text and TextView's clip is
65 * too aggressive.
66 */
Jon Miranda9b78e192019-08-30 18:42:01 -070067public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback,
Adam Cohen65086992020-02-19 08:40:49 -080068 IconLabelDotView, DraggableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070069
Sunny Goyaldfaccf62015-05-11 16:30:44 -070070 private static final int DISPLAY_WORKSPACE = 0;
71 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070072 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070073
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080074 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
75
Tony Wickham1237df02017-02-24 08:59:36 -080076
Tony Wickhamf34bee82018-12-03 18:11:39 -080077 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
78 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -080079 @Override
80 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -080081 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -080082 }
83
84 @Override
85 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -080086 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -080087 bubbleTextView.invalidate();
88 }
89 };
90
Sunny Goyaled7a6932018-04-13 11:41:50 -070091 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
92 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -070093 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070094 public Float get(BubbleTextView bubbleTextView) {
95 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -070096 }
97
98 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070099 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -0700100 bubbleTextView.setTextAlpha(alpha);
101 }
102 };
103
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800104 private final ActivityContext mActivity;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700105 private Drawable mIcon;
Adam Cohen65086992020-02-19 08:40:49 -0800106 private boolean mCenterVertically;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700107
Samuel Fufac96fa242019-09-26 23:06:32 -0700108 private final int mDisplay;
109
Sunny Goyaled7a6932018-04-13 11:41:50 -0700110 private final CheckLongPressHelper mLongPressHelper;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700111
112 private final boolean mLayoutHorizontal;
113 private final int mIconSize;
114
115 @ViewDebug.ExportedProperty(category = "launcher")
116 private boolean mIsIconVisible = true;
117 @ViewDebug.ExportedProperty(category = "launcher")
118 private int mTextColor;
119 @ViewDebug.ExportedProperty(category = "launcher")
120 private float mTextAlpha = 1;
121
Tony4043b052018-08-17 13:47:39 -0700122 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800123 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800124 private DotRenderer mDotRenderer;
125 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
126 private DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800127 private Animator mDotScaleAnim;
128 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700129
Sunny Goyal4ffec482016-02-09 11:28:52 -0800130 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800131 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800132 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700133 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800134 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700135 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500136
Sunny Goyal34b65272015-03-11 16:56:52 -0700137 private IconLoadRequest mIconLoadRequest;
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700140 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 }
142
143 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700144 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 }
146
147 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
148 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800149 mActivity = ActivityContext.lookupContext(context);
Adam Cohen96bb7982014-07-07 11:58:56 -0700150
Adam Cohen96bb7982014-07-07 11:58:56 -0700151 TypedArray a = context.obtainStyledAttributes(attrs,
152 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800153 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700154
Samuel Fufac96fa242019-09-26 23:06:32 -0700155 mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700156 final int defaultIconSize;
Samuel Fufac96fa242019-09-26 23:06:32 -0700157 if (mDisplay == DISPLAY_WORKSPACE) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700158 DeviceProfile grid = mActivity.getWallpaperDeviceProfile();
Winson Chung44d0aac2015-05-11 18:02:55 -0700159 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700160 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700161 defaultIconSize = grid.iconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700162 } else if (mDisplay == DISPLAY_ALL_APPS) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700163 DeviceProfile grid = mActivity.getDeviceProfile();
Winson1f064272016-07-18 17:18:02 -0700164 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
165 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700166 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufac96fa242019-09-26 23:06:32 -0700167 } else if (mDisplay == DISPLAY_FOLDER) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700168 DeviceProfile grid = mActivity.getDeviceProfile();
Jon Mirandabf7d8122016-11-03 15:29:29 -0700169 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700170 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700171 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700172 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700173 // widget_selection or shortcut_popup
Sunny Goyalae6e3182019-04-30 12:04:37 -0700174 defaultIconSize = mActivity.getDeviceProfile().iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700175 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700176
Winson1f064272016-07-18 17:18:02 -0700177 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700178
179 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
180 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700181 a.recycle();
182
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800183 mLongPressHelper = new CheckLongPressHelper(this);
184
Tony Wickham8912b042018-11-29 15:28:53 -0800185 mDotParams = new DotRenderer.DrawParams();
186
Sunny Goyal66dccad2018-03-19 12:00:51 -0700187 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700188 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700189 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191
Sunny Goyal66dccad2018-03-19 12:00:51 -0700192 @Override
193 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
194 // Disable marques when not focused to that, so that updating text does not cause relayout.
195 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
196 super.onFocusChanged(focused, direction, previouslyFocusedRect);
197 }
198
Jon Miranda52549442017-10-26 11:28:06 -0700199 /**
200 * Resets the view so it can be recycled.
201 */
202 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800203 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800204 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800205 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800206 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800207 mForceHideDot = false;
Samuel Fufaba6b47f2019-11-07 10:27:41 -0800208 setBackground(null);
Jon Miranda52549442017-10-26 11:28:06 -0700209 }
210
Tony Wickhamf34bee82018-12-03 18:11:39 -0800211 private void cancelDotScaleAnim() {
212 if (mDotScaleAnim != null) {
213 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700214 }
215 }
216
Tony Wickhamf34bee82018-12-03 18:11:39 -0800217 private void animateDotScale(float... dotScales) {
218 cancelDotScaleAnim();
219 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
220 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700221 @Override
222 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800223 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700224 }
225 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800226 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700227 }
228
Sunny Goyal95899162019-03-27 16:03:06 -0700229 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
230 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700231 }
232
Adam Cohen29794c52019-06-21 12:50:30 -0700233 @Override
234 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
235 if (delegate instanceof LauncherAccessibilityDelegate) {
236 super.setAccessibilityDelegate(delegate);
237 } else {
238 // NO-OP
239 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
240 // delegate incorrectly. There are no cases when we shouldn't be using the
241 // LauncherAccessibilityDelegate for BubbleTextView.
242 }
243 }
244
Sunny Goyal95899162019-03-27 16:03:06 -0700245 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800246 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800247 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700248 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800249 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500250 }
Tony Wickham010d2552017-01-20 08:15:28 -0800251
Tony Wickhamf34bee82018-12-03 18:11:39 -0800252 applyDotState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800253 }
254
Sunny Goyal508da152014-08-14 10:53:27 -0700255 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800256 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700257
Sunny Goyal95899162019-03-27 16:03:06 -0700258 // We don't need to check the info since it's not a WorkspaceItemInfo
Winson Chung888b3a12015-03-13 11:14:16 -0700259 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700260
261 // Verify high res immediately
262 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800263
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700264 if (info instanceof PromiseAppInfo) {
265 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
266 applyProgressLevel(promiseAppInfo.level);
267 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800268 applyDotState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700269 }
270
Sunny Goyal0e08f162015-05-12 11:32:39 -0700271 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800272 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700273 // We don't need to check the info since it's not a WorkspaceItemInfo
Sunny Goyal0e08f162015-05-12 11:32:39 -0700274 super.setTag(info);
275
276 // Verify high res immediately
277 verifyHighRes();
278 }
279
Sunny Goyal179249d2017-12-19 16:49:24 -0800280 private void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal14168432019-10-24 15:59:49 -0700281 FastBitmapDrawable iconDrawable = newIcon(getContext(), info);
Sunny Goyal3808a692019-10-25 13:41:28 -0700282 mDotParams.color = IconPalette.getMutedColor(info.bitmap.color, 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800283
Sunny Goyalf4204382016-07-13 10:46:07 -0700284 setIcon(iconDrawable);
285 setText(info.title);
286 if (info.contentDescription != null) {
287 setContentDescription(info.isDisabled()
288 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
289 : info.contentDescription);
290 }
291 }
292
Winson Chung7501adf2015-06-02 11:24:28 -0700293 /**
294 * Overrides the default long press timeout.
295 */
Tony2ca999c2018-09-24 17:24:51 -0400296 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
297 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700298 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800301 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700302 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800303 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800304 }
Sunny Goyal508da152014-08-14 10:53:27 -0700305 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800306
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800307 @Override
308 protected int[] onCreateDrawableState(int extraSpace) {
309 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
310 if (mStayPressed) {
311 mergeDrawableStates(drawableState, STATE_PRESSED);
312 }
313 return drawableState;
314 }
315
Winson Chungb745afb2015-03-02 11:51:23 -0800316 /** Returns the icon for this view. */
317 public Drawable getIcon() {
318 return mIcon;
319 }
320
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700321 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800322 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700323 // ignore events if they happen in padding area
Sunny Goyal17feee82020-03-24 13:55:15 -0700324 if (event.getAction() == MotionEvent.ACTION_DOWN
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700325 && (event.getY() < getPaddingTop()
326 || event.getX() < getPaddingLeft()
327 || event.getY() > getHeight() - getPaddingBottom()
328 || event.getX() > getWidth() - getPaddingRight())) {
329 return false;
330 }
Sunny Goyal17feee82020-03-24 13:55:15 -0700331 if (isLongClickable()) {
332 super.onTouchEvent(event);
333 mLongPressHelper.onTouchEvent(event);
334 // Keep receiving the rest of the events
335 return true;
336 } else {
337 return super.onTouchEvent(event);
Mady Melloref044dd2015-06-02 15:35:07 -0700338 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800339 }
340
Michael Jurkaddd62e92011-02-16 17:49:14 -0800341 void setStayPressed(boolean stayPressed) {
342 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800343 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800344 }
345
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700346 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800347 public void onVisibilityAggregated(boolean isVisible) {
348 super.onVisibilityAggregated(isVisible);
349 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800350 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800351 }
352 }
353
354 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700355 public void onLauncherResume() {
356 // Reset the pressed state of icon that was locked in the press state while activity
357 // was launching
358 setStayPressed(false);
359 }
360
Sunny Goyal508da152014-08-14 10:53:27 -0700361 void clearPressedBackground() {
362 setPressed(false);
363 setStayPressed(false);
364 }
365
366 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700367 public boolean onKeyUp(int keyCode, KeyEvent event) {
368 // Unlike touch events, keypress event propagate pressed state change immediately,
369 // without waiting for onClickHandler to execute. Disable pressed state changes here
370 // to avoid flickering.
371 mIgnorePressedStateChange = true;
372 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700373 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800374 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700375 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800376 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800377
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700378 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800379 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700380 super.onDraw(canvas);
381 }
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700384 public void onDraw(Canvas canvas) {
385 super.onDraw(canvas);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800386 drawDotIfNecessary(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800387 }
388
389 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800390 * Draws the notification dot in the top right corner of the icon bounds.
Tony Wickham1237df02017-02-24 08:59:36 -0800391 * @param canvas The canvas to draw to.
392 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800393 protected void drawDotIfNecessary(Canvas canvas) {
394 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800395 getIconBounds(mDotParams.iconBounds);
Tony Wickhame1cb93f2019-05-03 11:27:32 -0700396 Utilities.scaleRectAboutCenter(mDotParams.iconBounds, IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800397 final int scrollX = getScrollX();
398 final int scrollY = getScrollY();
399 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800400 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800401 canvas.translate(-scrollX, -scrollY);
402 }
403 }
404
Jon Miranda9b78e192019-08-30 18:42:01 -0700405 @Override
406 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800407 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800408 return;
409 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800410 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800411
Tony Wickhamf34bee82018-12-03 18:11:39 -0800412 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800413 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800414 } else if (hasDot()) {
415 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800416 }
417 }
418
Tony Wickhamf34bee82018-12-03 18:11:39 -0800419 private boolean hasDot() {
420 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800421 }
422
423 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700424 getIconBounds(this, outBounds, mIconSize);
425 }
426
427 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
428 int top = iconView.getPaddingTop();
429 int left = (iconView.getWidth() - iconSize) / 2;
430 int right = left + iconSize;
431 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800432 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400434
435 @Override
Winson1f064272016-07-18 17:18:02 -0700436 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
437 if (mCenterVertically) {
438 Paint.FontMetrics fm = getPaint().getFontMetrics();
439 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
440 (int) Math.ceil(fm.bottom - fm.top);
441 int height = MeasureSpec.getSize(heightMeasureSpec);
442 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
443 getPaddingBottom());
444 }
445 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
446 }
447
448 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700449 public void setTextColor(int color) {
450 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700451 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700452 }
453
Adam Cohen96bb7982014-07-07 11:58:56 -0700454 @Override
455 public void setTextColor(ColorStateList colors) {
456 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700457 if (Float.compare(mTextAlpha, 1) == 0) {
458 super.setTextColor(colors);
459 } else {
460 super.setTextColor(getModifiedColor());
461 }
Adam Cohen477828c2013-09-20 12:05:49 -0700462 }
463
Tony7308cde2017-06-27 22:38:33 -0700464 public boolean shouldTextBeVisible() {
465 // Text should be visible everywhere but the hotseat.
466 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
467 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
Samuel Fufaefb665d2019-10-30 13:24:14 -0700468 return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT
469 && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION);
Tony7308cde2017-06-27 22:38:33 -0700470 }
471
Winson Chung5f8afe62013-08-12 16:19:28 -0700472 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700473 setTextAlpha(visible ? 1 : 0);
474 }
475
476 private void setTextAlpha(float alpha) {
477 mTextAlpha = alpha;
478 super.setTextColor(getModifiedColor());
479 }
480
481 private int getModifiedColor() {
482 if (mTextAlpha == 0) {
483 // Special case to prevent text shadows in high contrast mode
484 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700485 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800486 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700487 }
488
489 /**
490 * Creates an animator to fade the text in or out.
491 * @param fadeIn Whether the text should fade in or fade out.
492 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700493 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700494 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700495 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700496 }
497
Winson Chungaffd7b42010-08-20 15:11:56 -0700498 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800499 public void cancelLongPress() {
500 super.cancelLongPress();
Winson Chung88f33452012-02-23 15:23:44 -0800501 mLongPressHelper.cancelLongPress();
502 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500503
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800504 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyal95899162019-03-27 16:03:06 -0700505 if (getTag() instanceof WorkspaceItemInfo) {
506 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700507 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700508 final int progressLevel = isPromise ?
Sunny Goyal95899162019-03-27 16:03:06 -0700509 ((info.hasStatusFlag(WorkspaceItemInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
Sunny Goyal34942622014-08-29 17:20:55 -0700510 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700511
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700512 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
513 if (preloadDrawable != null && promiseStateChanged) {
514 preloadDrawable.maybePerformFinishedAnimation();
515 }
516 }
517 }
518
519 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
520 if (getTag() instanceof ItemInfoWithIcon) {
521 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700522 if (progressLevel >= 100) {
523 setContentDescription(info.contentDescription != null
524 ? info.contentDescription : "");
525 } else if (progressLevel > 0) {
526 setContentDescription(getContext()
527 .getString(R.string.app_downloading_title, info.title,
528 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
529 } else {
530 setContentDescription(getContext()
531 .getString(R.string.app_waiting_download_title, info.title));
532 }
Winson Chungb745afb2015-03-02 11:51:23 -0800533 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700534 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800535 if (mIcon instanceof PreloadIconDrawable) {
536 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700537 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700538 } else {
Sunny Goyal14168432019-10-24 15:59:49 -0700539 preloadDrawable = newPendingIcon(getContext(), info);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700540 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700541 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700542 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700543 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700544 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400545 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700546 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400547 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700548
Tony Wickhamf34bee82018-12-03 18:11:39 -0800549 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800550 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800551 boolean wasDotted = mDotInfo != null;
552 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
553 boolean isDotted = mDotInfo != null;
554 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700555 if (mDisplay == DISPLAY_ALL_APPS) {
556 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
557 } else {
558 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
559 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800560 if (wasDotted || isDotted) {
561 // Animate when a dot is first added or when it is removed.
562 if (animate && (wasDotted ^ isDotted) && isShown()) {
563 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800564 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800565 cancelDotScaleAnim();
566 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800567 invalidate();
568 }
569 }
Tony Wickham305e9202018-01-23 17:46:47 -0800570 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700571 if (itemInfo.isDisabled()) {
572 setContentDescription(getContext().getString(R.string.disabled_app_label,
573 itemInfo.contentDescription));
574 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800575 int count = mDotInfo.getNotificationCount();
Tony Wickham305e9202018-01-23 17:46:47 -0800576 setContentDescription(getContext().getResources().getQuantityString(
Tony Wickhamf34bee82018-12-03 18:11:39 -0800577 R.plurals.dotted_app_label, count, itemInfo.contentDescription, count));
Tony Wickham305e9202018-01-23 17:46:47 -0800578 } else {
579 setContentDescription(itemInfo.contentDescription);
580 }
581 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800582 }
583 }
584
Winson Chungb745afb2015-03-02 11:51:23 -0800585 /**
586 * Sets the icon for this view based on the layout direction.
587 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700588 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700589 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700590 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700591 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700592 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800593 if (mIcon != null) {
594 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
595 }
Tony17b7f9b2017-05-23 12:19:09 -0700596 }
597
Jon Miranda9b78e192019-08-30 18:42:01 -0700598 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700599 public void setIconVisible(boolean visible) {
600 mIsIconVisible = visible;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700601 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700602 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700603 }
604
605 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700606 // If we had already set an icon before, disable relayout as the icon size is the
607 // same as before.
608 mDisableRelayout = mIcon != null;
609
610 icon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800611 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800612 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800613 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700614 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800615 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700616 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800617 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700618
Sunny Goyal69b75642015-05-15 17:00:24 -0700619 @Override
620 public void requestLayout() {
621 if (!mDisableRelayout) {
622 super.requestLayout();
623 }
624 }
625
Sunny Goyal34b65272015-03-11 16:56:52 -0700626 /**
627 * Applies the item info if it is same as what the view is pointing to currently.
628 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800629 @Override
630 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700631 if (getTag() == info) {
632 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700633 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700634
Jon Miranda7f522a22017-07-28 11:56:47 -0700635 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -0700636 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -0700637
Sunny Goyal34b65272015-03-11 16:56:52 -0700638 if (info instanceof AppInfo) {
639 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700640 } else if (info instanceof WorkspaceItemInfo) {
641 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700642 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700643 } else if (info instanceof PackageItemInfo) {
644 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700645 }
Winsonc0880492015-08-21 11:16:27 -0700646
Sunny Goyal69b75642015-05-15 17:00:24 -0700647 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700648 }
649 }
650
651 /**
652 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
653 */
654 public void verifyHighRes() {
655 if (mIconLoadRequest != null) {
656 mIconLoadRequest.cancel();
657 mIconLoadRequest = null;
658 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800659 if (getTag() instanceof ItemInfoWithIcon) {
660 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700661 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800662 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700663 .updateIconInBackground(BubbleTextView.this, info);
664 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700665 }
666 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700667
Jon Miranda47170112017-03-03 14:57:14 -0800668 public int getIconSize() {
669 return mIconSize;
670 }
Adam Cohen65086992020-02-19 08:40:49 -0800671
672 @Override
673 public int getViewType() {
674 return DRAGGABLE_ICON;
675 }
676
677 @Override
678 public void getVisualDragBounds(Rect bounds) {
679 DeviceProfile grid = mActivity.getDeviceProfile();
680 BubbleTextView.getIconBounds(this, bounds, grid.iconSizePx);
681 }
682
683 @Override
684 public void prepareDrawDragView() {
685 if (getIcon() instanceof FastBitmapDrawable) {
686 FastBitmapDrawable icon = (FastBitmapDrawable) getIcon();
687 icon.setScale(1f);
688 }
689 setForceHideDot(true);
690 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691}