blob: ff5f33d52667a3e98902e4bd3b303cb7c70bd298 [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;
Jason Monk02dd7ae2014-04-15 15:23:31 -040042import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080043import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080044import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070045
Sunny Goyal3dce5f32017-10-05 11:40:05 -070046import com.android.launcher3.Launcher.OnResumeCallback;
Adam Cohen29794c52019-06-21 12:50:30 -070047import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Tony Wickhamf34bee82018-12-03 18:11:39 -080048import com.android.launcher3.dot.DotInfo;
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,
68 IconLabelDotView {
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;
106 private final boolean mCenterVertically;
107
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;
111 private final StylusEventHelper mStylusEventHelper;
112 private final float mSlop;
113
114 private final boolean mLayoutHorizontal;
115 private final int mIconSize;
116
117 @ViewDebug.ExportedProperty(category = "launcher")
118 private boolean mIsIconVisible = true;
119 @ViewDebug.ExportedProperty(category = "launcher")
120 private int mTextColor;
121 @ViewDebug.ExportedProperty(category = "launcher")
122 private float mTextAlpha = 1;
123
Tony4043b052018-08-17 13:47:39 -0700124 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800125 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800126 private DotRenderer mDotRenderer;
127 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
128 private DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800129 private Animator mDotScaleAnim;
130 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700131
Sunny Goyal4ffec482016-02-09 11:28:52 -0800132 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800133 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800134 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700135 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800136 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700137 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500138
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700139 @ViewDebug.ExportedProperty(category = "launcher")
140 private final boolean mIgnorePaddingTouch;
141
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);
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700155 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700156
Adam Cohen96bb7982014-07-07 11:58:56 -0700157 TypedArray a = context.obtainStyledAttributes(attrs,
158 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800159 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
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) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700164 DeviceProfile grid = mActivity.getWallpaperDeviceProfile();
Winson Chung44d0aac2015-05-11 18:02:55 -0700165 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700166 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700167 defaultIconSize = grid.iconSizePx;
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700168 mIgnorePaddingTouch = true;
Samuel Fufac96fa242019-09-26 23:06:32 -0700169 } else if (mDisplay == DISPLAY_ALL_APPS) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700170 DeviceProfile grid = mActivity.getDeviceProfile();
Winson1f064272016-07-18 17:18:02 -0700171 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
172 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700173 defaultIconSize = grid.allAppsIconSizePx;
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700174 mIgnorePaddingTouch = true;
Samuel Fufac96fa242019-09-26 23:06:32 -0700175 } else if (mDisplay == DISPLAY_FOLDER) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700176 DeviceProfile grid = mActivity.getDeviceProfile();
Jon Mirandabf7d8122016-11-03 15:29:29 -0700177 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700178 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700179 defaultIconSize = grid.folderChildIconSizePx;
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700180 mIgnorePaddingTouch = true;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700181 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700182 // widget_selection or shortcut_popup
Sunny Goyalae6e3182019-04-30 12:04:37 -0700183 defaultIconSize = mActivity.getDeviceProfile().iconSizePx;
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700184 mIgnorePaddingTouch = false;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700185 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700186
Winson1f064272016-07-18 17:18:02 -0700187 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700188
189 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
190 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700191 a.recycle();
192
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800193 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700194 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800195
Tony Wickham8912b042018-11-29 15:28:53 -0800196 mDotParams = new DotRenderer.DrawParams();
197
Sunny Goyal66dccad2018-03-19 12:00:51 -0700198 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700199 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700200 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 }
202
Sunny Goyal66dccad2018-03-19 12:00:51 -0700203 @Override
204 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
205 // Disable marques when not focused to that, so that updating text does not cause relayout.
206 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
207 super.onFocusChanged(focused, direction, previouslyFocusedRect);
208 }
209
Jon Miranda52549442017-10-26 11:28:06 -0700210 /**
211 * Resets the view so it can be recycled.
212 */
213 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800214 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800215 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800216 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800217 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800218 mForceHideDot = false;
Jon Miranda52549442017-10-26 11:28:06 -0700219 }
220
Tony Wickhamf34bee82018-12-03 18:11:39 -0800221 private void cancelDotScaleAnim() {
222 if (mDotScaleAnim != null) {
223 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700224 }
225 }
226
Tony Wickhamf34bee82018-12-03 18:11:39 -0800227 private void animateDotScale(float... dotScales) {
228 cancelDotScaleAnim();
229 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
230 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700231 @Override
232 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800233 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700234 }
235 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800236 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700237 }
238
Sunny Goyal95899162019-03-27 16:03:06 -0700239 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
240 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700241 }
242
Adam Cohen29794c52019-06-21 12:50:30 -0700243 @Override
244 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
245 if (delegate instanceof LauncherAccessibilityDelegate) {
246 super.setAccessibilityDelegate(delegate);
247 } else {
248 // NO-OP
249 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
250 // delegate incorrectly. There are no cases when we shouldn't be using the
251 // LauncherAccessibilityDelegate for BubbleTextView.
252 }
253 }
254
Sunny Goyal95899162019-03-27 16:03:06 -0700255 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800256 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800257 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700258 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800259 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500260 }
Tony Wickham010d2552017-01-20 08:15:28 -0800261
Tony Wickhamf34bee82018-12-03 18:11:39 -0800262 applyDotState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800263 }
264
Sunny Goyal508da152014-08-14 10:53:27 -0700265 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800266 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700267
Sunny Goyal95899162019-03-27 16:03:06 -0700268 // We don't need to check the info since it's not a WorkspaceItemInfo
Winson Chung888b3a12015-03-13 11:14:16 -0700269 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700270
271 // Verify high res immediately
272 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800273
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700274 if (info instanceof PromiseAppInfo) {
275 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
276 applyProgressLevel(promiseAppInfo.level);
277 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800278 applyDotState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700279 }
280
Sunny Goyal0e08f162015-05-12 11:32:39 -0700281 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800282 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700283 // We don't need to check the info since it's not a WorkspaceItemInfo
Sunny Goyal0e08f162015-05-12 11:32:39 -0700284 super.setTag(info);
285
286 // Verify high res immediately
287 verifyHighRes();
288 }
289
Sunny Goyal179249d2017-12-19 16:49:24 -0800290 private void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal14168432019-10-24 15:59:49 -0700291 FastBitmapDrawable iconDrawable = newIcon(getContext(), info);
Sunny Goyal3808a692019-10-25 13:41:28 -0700292 mDotParams.color = IconPalette.getMutedColor(info.bitmap.color, 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800293
Sunny Goyalf4204382016-07-13 10:46:07 -0700294 setIcon(iconDrawable);
295 setText(info.title);
296 if (info.contentDescription != null) {
297 setContentDescription(info.isDisabled()
298 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
299 : info.contentDescription);
300 }
301 }
302
Winson Chung7501adf2015-06-02 11:24:28 -0700303 /**
304 * Overrides the default long press timeout.
305 */
Tony2ca999c2018-09-24 17:24:51 -0400306 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
307 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700308 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700309
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800311 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700312 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800313 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800314 }
Sunny Goyal508da152014-08-14 10:53:27 -0700315 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800316
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800317 @Override
318 protected int[] onCreateDrawableState(int extraSpace) {
319 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
320 if (mStayPressed) {
321 mergeDrawableStates(drawableState, STATE_PRESSED);
322 }
323 return drawableState;
324 }
325
Winson Chungb745afb2015-03-02 11:51:23 -0800326 /** Returns the icon for this view. */
327 public Drawable getIcon() {
328 return mIcon;
329 }
330
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700331 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800332 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700333 // ignore events if they happen in padding area
334 if (event.getAction() == MotionEvent.ACTION_DOWN && mIgnorePaddingTouch
335 && (event.getY() < getPaddingTop()
336 || event.getX() < getPaddingLeft()
337 || event.getY() > getHeight() - getPaddingBottom()
338 || event.getX() > getWidth() - getPaddingRight())) {
339 return false;
340 }
341
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800342 // Call the superclass onTouchEvent first, because sometimes it changes the state to
343 // isPressed() on an ACTION_UP
344 boolean result = super.onTouchEvent(event);
345
Mady Melloref044dd2015-06-02 15:35:07 -0700346 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700347 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700348 mLongPressHelper.cancelLongPress();
349 result = true;
350 }
351
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800352 switch (event.getAction()) {
353 case MotionEvent.ACTION_DOWN:
Mady Melloref044dd2015-06-02 15:35:07 -0700354 // If we're in a stylus button press, don't check for long press.
355 if (!mStylusEventHelper.inStylusButtonPressed()) {
356 mLongPressHelper.postCheckForLongPress();
357 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800358 break;
359 case MotionEvent.ACTION_CANCEL:
360 case MotionEvent.ACTION_UP:
Winson Chung88f33452012-02-23 15:23:44 -0800361 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800362 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400363 case MotionEvent.ACTION_MOVE:
364 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
365 mLongPressHelper.cancelLongPress();
366 }
367 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800368 }
369 return result;
370 }
371
Michael Jurkaddd62e92011-02-16 17:49:14 -0800372 void setStayPressed(boolean stayPressed) {
373 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800374 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800375 }
376
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700377 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800378 public void onVisibilityAggregated(boolean isVisible) {
379 super.onVisibilityAggregated(isVisible);
380 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800381 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800382 }
383 }
384
385 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700386 public void onLauncherResume() {
387 // Reset the pressed state of icon that was locked in the press state while activity
388 // was launching
389 setStayPressed(false);
390 }
391
Sunny Goyal508da152014-08-14 10:53:27 -0700392 void clearPressedBackground() {
393 setPressed(false);
394 setStayPressed(false);
395 }
396
397 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700398 public boolean onKeyUp(int keyCode, KeyEvent event) {
399 // Unlike touch events, keypress event propagate pressed state change immediately,
400 // without waiting for onClickHandler to execute. Disable pressed state changes here
401 // to avoid flickering.
402 mIgnorePressedStateChange = true;
403 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700404 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800405 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700406 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800407 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800408
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700409 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800410 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700411 super.onDraw(canvas);
412 }
413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700415 public void onDraw(Canvas canvas) {
416 super.onDraw(canvas);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800417 drawDotIfNecessary(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800418 }
419
420 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800421 * Draws the notification dot in the top right corner of the icon bounds.
Tony Wickham1237df02017-02-24 08:59:36 -0800422 * @param canvas The canvas to draw to.
423 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800424 protected void drawDotIfNecessary(Canvas canvas) {
425 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800426 getIconBounds(mDotParams.iconBounds);
Tony Wickhame1cb93f2019-05-03 11:27:32 -0700427 Utilities.scaleRectAboutCenter(mDotParams.iconBounds, IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800428 final int scrollX = getScrollX();
429 final int scrollY = getScrollY();
430 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800431 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800432 canvas.translate(-scrollX, -scrollY);
433 }
434 }
435
Jon Miranda9b78e192019-08-30 18:42:01 -0700436 @Override
437 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800438 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800439 return;
440 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800441 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800442
Tony Wickhamf34bee82018-12-03 18:11:39 -0800443 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800444 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800445 } else if (hasDot()) {
446 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800447 }
448 }
449
Tony Wickhamf34bee82018-12-03 18:11:39 -0800450 private boolean hasDot() {
451 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800452 }
453
454 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700455 getIconBounds(this, outBounds, mIconSize);
456 }
457
458 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
459 int top = iconView.getPaddingTop();
460 int left = (iconView.getWidth() - iconSize) / 2;
461 int right = left + iconSize;
462 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800463 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400465
466 @Override
Winson1f064272016-07-18 17:18:02 -0700467 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
468 if (mCenterVertically) {
469 Paint.FontMetrics fm = getPaint().getFontMetrics();
470 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
471 (int) Math.ceil(fm.bottom - fm.top);
472 int height = MeasureSpec.getSize(heightMeasureSpec);
473 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
474 getPaddingBottom());
475 }
476 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
477 }
478
479 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700480 public void setTextColor(int color) {
481 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700482 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700483 }
484
Adam Cohen96bb7982014-07-07 11:58:56 -0700485 @Override
486 public void setTextColor(ColorStateList colors) {
487 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700488 if (Float.compare(mTextAlpha, 1) == 0) {
489 super.setTextColor(colors);
490 } else {
491 super.setTextColor(getModifiedColor());
492 }
Adam Cohen477828c2013-09-20 12:05:49 -0700493 }
494
Tony7308cde2017-06-27 22:38:33 -0700495 public boolean shouldTextBeVisible() {
496 // Text should be visible everywhere but the hotseat.
497 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
498 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
499 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
500 }
501
Winson Chung5f8afe62013-08-12 16:19:28 -0700502 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700503 setTextAlpha(visible ? 1 : 0);
504 }
505
506 private void setTextAlpha(float alpha) {
507 mTextAlpha = alpha;
508 super.setTextColor(getModifiedColor());
509 }
510
511 private int getModifiedColor() {
512 if (mTextAlpha == 0) {
513 // Special case to prevent text shadows in high contrast mode
514 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700515 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800516 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700517 }
518
519 /**
520 * Creates an animator to fade the text in or out.
521 * @param fadeIn Whether the text should fade in or fade out.
522 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700523 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700524 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700525 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700526 }
527
Winson Chungaffd7b42010-08-20 15:11:56 -0700528 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800529 public void cancelLongPress() {
530 super.cancelLongPress();
531
532 mLongPressHelper.cancelLongPress();
533 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500534
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800535 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyal95899162019-03-27 16:03:06 -0700536 if (getTag() instanceof WorkspaceItemInfo) {
537 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700538 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700539 final int progressLevel = isPromise ?
Sunny Goyal95899162019-03-27 16:03:06 -0700540 ((info.hasStatusFlag(WorkspaceItemInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
Sunny Goyal34942622014-08-29 17:20:55 -0700541 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700542
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700543 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
544 if (preloadDrawable != null && promiseStateChanged) {
545 preloadDrawable.maybePerformFinishedAnimation();
546 }
547 }
548 }
549
550 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
551 if (getTag() instanceof ItemInfoWithIcon) {
552 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700553 if (progressLevel >= 100) {
554 setContentDescription(info.contentDescription != null
555 ? info.contentDescription : "");
556 } else if (progressLevel > 0) {
557 setContentDescription(getContext()
558 .getString(R.string.app_downloading_title, info.title,
559 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
560 } else {
561 setContentDescription(getContext()
562 .getString(R.string.app_waiting_download_title, info.title));
563 }
Winson Chungb745afb2015-03-02 11:51:23 -0800564 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700565 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800566 if (mIcon instanceof PreloadIconDrawable) {
567 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700568 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700569 } else {
Sunny Goyal14168432019-10-24 15:59:49 -0700570 preloadDrawable = newPendingIcon(getContext(), info);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700571 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700572 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700573 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700574 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700575 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400576 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700577 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400578 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700579
Tony Wickhamf34bee82018-12-03 18:11:39 -0800580 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800581 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800582 boolean wasDotted = mDotInfo != null;
583 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
584 boolean isDotted = mDotInfo != null;
585 float newDotScale = isDotted ? 1f : 0;
Samuel Fufac96fa242019-09-26 23:06:32 -0700586 if (mDisplay == DISPLAY_ALL_APPS) {
587 mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps;
588 } else {
589 mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace;
590 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800591 if (wasDotted || isDotted) {
592 // Animate when a dot is first added or when it is removed.
593 if (animate && (wasDotted ^ isDotted) && isShown()) {
594 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800595 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800596 cancelDotScaleAnim();
597 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800598 invalidate();
599 }
600 }
Tony Wickham305e9202018-01-23 17:46:47 -0800601 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700602 if (itemInfo.isDisabled()) {
603 setContentDescription(getContext().getString(R.string.disabled_app_label,
604 itemInfo.contentDescription));
605 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800606 int count = mDotInfo.getNotificationCount();
Tony Wickham305e9202018-01-23 17:46:47 -0800607 setContentDescription(getContext().getResources().getQuantityString(
Tony Wickhamf34bee82018-12-03 18:11:39 -0800608 R.plurals.dotted_app_label, count, itemInfo.contentDescription, count));
Tony Wickham305e9202018-01-23 17:46:47 -0800609 } else {
610 setContentDescription(itemInfo.contentDescription);
611 }
612 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800613 }
614 }
615
Winson Chungb745afb2015-03-02 11:51:23 -0800616 /**
617 * Sets the icon for this view based on the layout direction.
618 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700619 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700620 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700621 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700622 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700623 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800624 if (mIcon != null) {
625 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
626 }
Tony17b7f9b2017-05-23 12:19:09 -0700627 }
628
Jon Miranda9b78e192019-08-30 18:42:01 -0700629 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700630 public void setIconVisible(boolean visible) {
631 mIsIconVisible = visible;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700632 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700633 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700634 }
635
636 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700637 // If we had already set an icon before, disable relayout as the icon size is the
638 // same as before.
639 mDisableRelayout = mIcon != null;
640
641 icon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800642 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800643 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800644 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700645 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800646 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700647 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800648 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700649
Sunny Goyal69b75642015-05-15 17:00:24 -0700650 @Override
651 public void requestLayout() {
652 if (!mDisableRelayout) {
653 super.requestLayout();
654 }
655 }
656
Sunny Goyal34b65272015-03-11 16:56:52 -0700657 /**
658 * Applies the item info if it is same as what the view is pointing to currently.
659 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800660 @Override
661 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700662 if (getTag() == info) {
663 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700664 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700665
Jon Miranda7f522a22017-07-28 11:56:47 -0700666 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
Sunny Goyal3808a692019-10-25 13:41:28 -0700667 info.bitmap.icon.prepareToDraw();
Jon Miranda7f522a22017-07-28 11:56:47 -0700668
Sunny Goyal34b65272015-03-11 16:56:52 -0700669 if (info instanceof AppInfo) {
670 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700671 } else if (info instanceof WorkspaceItemInfo) {
672 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700673 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700674 } else if (info instanceof PackageItemInfo) {
675 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700676 }
Winsonc0880492015-08-21 11:16:27 -0700677
Sunny Goyal69b75642015-05-15 17:00:24 -0700678 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700679 }
680 }
681
682 /**
683 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
684 */
685 public void verifyHighRes() {
686 if (mIconLoadRequest != null) {
687 mIconLoadRequest.cancel();
688 mIconLoadRequest = null;
689 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800690 if (getTag() instanceof ItemInfoWithIcon) {
691 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700692 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800693 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700694 .updateIconInBackground(BubbleTextView.this, info);
695 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700696 }
697 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700698
Jon Miranda47170112017-03-03 14:57:14 -0800699 public int getIconSize() {
700 return mIconSize;
701 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702}