blob: 4d177103d3bf0488e0dc48955cfc67179d299dc9 [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 Goyal066ace12018-11-05 11:08:31 -080019import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
20
Tony4043b052018-08-17 13:47:39 -070021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Tony Wickham1237df02017-02-24 08:59:36 -080023import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070025import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070026import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070028import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070029import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080030import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070031import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.drawable.Drawable;
Sunny Goyal66dccad2018-03-19 12:00:51 -070033import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080034import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080035import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070036import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070037import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080038import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070039import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040040import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080041import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080042import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070043
Sunny Goyal3dce5f32017-10-05 11:40:05 -070044import com.android.launcher3.Launcher.OnResumeCallback;
Adam Cohen29794c52019-06-21 12:50:30 -070045import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Tony Wickhamf34bee82018-12-03 18:11:39 -080046import com.android.launcher3.dot.DotInfo;
Tony7308cde2017-06-27 22:38:33 -070047import com.android.launcher3.folder.FolderIcon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080048import com.android.launcher3.graphics.DrawableFactory;
Tony Wickham1237df02017-02-24 08:59:36 -080049import com.android.launcher3.graphics.IconPalette;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070050import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080051import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080052import com.android.launcher3.icons.DotRenderer;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080053import com.android.launcher3.icons.IconCache.IconLoadRequest;
54import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070055import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080056import com.android.launcher3.views.ActivityContext;
Jon Miranda9b78e192019-08-30 18:42:01 -070057import com.android.launcher3.views.IconLabelDotView;
Sunny Goyal34b65272015-03-11 16:56:52 -070058
Sunny Goyalc469aad2015-10-01 11:24:23 -070059import java.text.NumberFormat;
60
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061/**
62 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
63 * because we want to make the bubble taller than the text and TextView's clip is
64 * too aggressive.
65 */
Jon Miranda9b78e192019-08-30 18:42:01 -070066public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback,
67 IconLabelDotView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070068
Sunny Goyaldfaccf62015-05-11 16:30:44 -070069 private static final int DISPLAY_WORKSPACE = 0;
70 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070071 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070072
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080073 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
74
Tony Wickham1237df02017-02-24 08:59:36 -080075
Tony Wickhamf34bee82018-12-03 18:11:39 -080076 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
77 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -080078 @Override
79 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -080080 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -080081 }
82
83 @Override
84 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -080085 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -080086 bubbleTextView.invalidate();
87 }
88 };
89
Sunny Goyaled7a6932018-04-13 11:41:50 -070090 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
91 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -070092 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070093 public Float get(BubbleTextView bubbleTextView) {
94 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -070095 }
96
97 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070098 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -070099 bubbleTextView.setTextAlpha(alpha);
100 }
101 };
102
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800103 private final ActivityContext mActivity;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700104 private Drawable mIcon;
105 private final boolean mCenterVertically;
106
107 private final CheckLongPressHelper mLongPressHelper;
108 private final StylusEventHelper mStylusEventHelper;
109 private final float mSlop;
110
111 private final boolean mLayoutHorizontal;
112 private final int mIconSize;
113
114 @ViewDebug.ExportedProperty(category = "launcher")
115 private boolean mIsIconVisible = true;
116 @ViewDebug.ExportedProperty(category = "launcher")
117 private int mTextColor;
118 @ViewDebug.ExportedProperty(category = "launcher")
119 private float mTextAlpha = 1;
120
Tony4043b052018-08-17 13:47:39 -0700121 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800122 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800123 private DotRenderer mDotRenderer;
124 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
125 private DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800126 private Animator mDotScaleAnim;
127 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700128
Sunny Goyal4ffec482016-02-09 11:28:52 -0800129 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800130 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800131 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700132 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800133 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700134 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500135
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700136 @ViewDebug.ExportedProperty(category = "launcher")
137 private final boolean mIgnorePaddingTouch;
138
Sunny Goyal34b65272015-03-11 16:56:52 -0700139 private IconLoadRequest mIconLoadRequest;
140
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700142 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 }
144
145 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700146 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 }
148
149 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
150 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800151 mActivity = ActivityContext.lookupContext(context);
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700152 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700153
Adam Cohen96bb7982014-07-07 11:58:56 -0700154 TypedArray a = context.obtainStyledAttributes(attrs,
155 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800156 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700157
158 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700159 final int defaultIconSize;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700160 if (display == DISPLAY_WORKSPACE) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700161 DeviceProfile grid = mActivity.getWallpaperDeviceProfile();
Winson Chung44d0aac2015-05-11 18:02:55 -0700162 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700163 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700164 defaultIconSize = grid.iconSizePx;
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700165 mIgnorePaddingTouch = true;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700166 } else if (display == DISPLAY_ALL_APPS) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700167 DeviceProfile grid = mActivity.getDeviceProfile();
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 Fufa4a1d9b12019-09-19 17:28:00 -0700171 mIgnorePaddingTouch = true;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700172 } else if (display == DISPLAY_FOLDER) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700173 DeviceProfile grid = mActivity.getDeviceProfile();
Jon Mirandabf7d8122016-11-03 15:29:29 -0700174 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700175 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700176 defaultIconSize = grid.folderChildIconSizePx;
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700177 mIgnorePaddingTouch = true;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700178 } else {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700179 // widget_selection or shortcut_popup
Sunny Goyalae6e3182019-04-30 12:04:37 -0700180 defaultIconSize = mActivity.getDeviceProfile().iconSizePx;
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700181 mIgnorePaddingTouch = false;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700182 }
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700183
Winson1f064272016-07-18 17:18:02 -0700184 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700185
186 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
187 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700188 a.recycle();
189
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800190 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700191 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800192
Tony Wickham8912b042018-11-29 15:28:53 -0800193 mDotParams = new DotRenderer.DrawParams();
194
Sunny Goyal66dccad2018-03-19 12:00:51 -0700195 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700196 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700197 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 }
199
Sunny Goyal66dccad2018-03-19 12:00:51 -0700200 @Override
201 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
202 // Disable marques when not focused to that, so that updating text does not cause relayout.
203 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
204 super.onFocusChanged(focused, direction, previouslyFocusedRect);
205 }
206
Jon Miranda52549442017-10-26 11:28:06 -0700207 /**
208 * Resets the view so it can be recycled.
209 */
210 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800211 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800212 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800213 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800214 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800215 mForceHideDot = false;
Jon Miranda52549442017-10-26 11:28:06 -0700216 }
217
Tony Wickhamf34bee82018-12-03 18:11:39 -0800218 private void cancelDotScaleAnim() {
219 if (mDotScaleAnim != null) {
220 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700221 }
222 }
223
Tony Wickhamf34bee82018-12-03 18:11:39 -0800224 private void animateDotScale(float... dotScales) {
225 cancelDotScaleAnim();
226 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
227 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700228 @Override
229 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800230 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700231 }
232 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800233 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700234 }
235
Sunny Goyal95899162019-03-27 16:03:06 -0700236 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
237 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700238 }
239
Adam Cohen29794c52019-06-21 12:50:30 -0700240 @Override
241 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
242 if (delegate instanceof LauncherAccessibilityDelegate) {
243 super.setAccessibilityDelegate(delegate);
244 } else {
245 // NO-OP
246 // Workaround for b/129745295 where RecyclerView is setting our Accessibility
247 // delegate incorrectly. There are no cases when we shouldn't be using the
248 // LauncherAccessibilityDelegate for BubbleTextView.
249 }
250 }
251
Sunny Goyal95899162019-03-27 16:03:06 -0700252 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800253 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800254 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700255 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800256 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500257 }
Tony Wickham010d2552017-01-20 08:15:28 -0800258
Tony Wickhamf34bee82018-12-03 18:11:39 -0800259 applyDotState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800260 }
261
Sunny Goyal508da152014-08-14 10:53:27 -0700262 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800263 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700264
Sunny Goyal95899162019-03-27 16:03:06 -0700265 // We don't need to check the info since it's not a WorkspaceItemInfo
Winson Chung888b3a12015-03-13 11:14:16 -0700266 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700267
268 // Verify high res immediately
269 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800270
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700271 if (info instanceof PromiseAppInfo) {
272 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
273 applyProgressLevel(promiseAppInfo.level);
274 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800275 applyDotState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700276 }
277
Sunny Goyal0e08f162015-05-12 11:32:39 -0700278 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800279 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700280 // We don't need to check the info since it's not a WorkspaceItemInfo
Sunny Goyal0e08f162015-05-12 11:32:39 -0700281 super.setTag(info);
282
283 // Verify high res immediately
284 verifyHighRes();
285 }
286
Sunny Goyal179249d2017-12-19 16:49:24 -0800287 private void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal2b787e52018-08-20 15:01:03 -0700288 FastBitmapDrawable iconDrawable = DrawableFactory.INSTANCE.get(getContext())
289 .newIcon(getContext(), info);
Tony Wickham8912b042018-11-29 15:28:53 -0800290 mDotParams.color = IconPalette.getMutedColor(info.iconColor, 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800291
Sunny Goyalf4204382016-07-13 10:46:07 -0700292 setIcon(iconDrawable);
293 setText(info.title);
294 if (info.contentDescription != null) {
295 setContentDescription(info.isDisabled()
296 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
297 : info.contentDescription);
298 }
299 }
300
Winson Chung7501adf2015-06-02 11:24:28 -0700301 /**
302 * Overrides the default long press timeout.
303 */
Tony2ca999c2018-09-24 17:24:51 -0400304 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
305 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700306 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700307
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800309 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700310 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800311 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800312 }
Sunny Goyal508da152014-08-14 10:53:27 -0700313 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800314
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800315 @Override
316 protected int[] onCreateDrawableState(int extraSpace) {
317 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
318 if (mStayPressed) {
319 mergeDrawableStates(drawableState, STATE_PRESSED);
320 }
321 return drawableState;
322 }
323
Winson Chungb745afb2015-03-02 11:51:23 -0800324 /** Returns the icon for this view. */
325 public Drawable getIcon() {
326 return mIcon;
327 }
328
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700329 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800330 public boolean onTouchEvent(MotionEvent event) {
Samuel Fufa4a1d9b12019-09-19 17:28:00 -0700331 // ignore events if they happen in padding area
332 if (event.getAction() == MotionEvent.ACTION_DOWN && mIgnorePaddingTouch
333 && (event.getY() < getPaddingTop()
334 || event.getX() < getPaddingLeft()
335 || event.getY() > getHeight() - getPaddingBottom()
336 || event.getX() > getWidth() - getPaddingRight())) {
337 return false;
338 }
339
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800340 // Call the superclass onTouchEvent first, because sometimes it changes the state to
341 // isPressed() on an ACTION_UP
342 boolean result = super.onTouchEvent(event);
343
Mady Melloref044dd2015-06-02 15:35:07 -0700344 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700345 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700346 mLongPressHelper.cancelLongPress();
347 result = true;
348 }
349
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800350 switch (event.getAction()) {
351 case MotionEvent.ACTION_DOWN:
Mady Melloref044dd2015-06-02 15:35:07 -0700352 // If we're in a stylus button press, don't check for long press.
353 if (!mStylusEventHelper.inStylusButtonPressed()) {
354 mLongPressHelper.postCheckForLongPress();
355 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800356 break;
357 case MotionEvent.ACTION_CANCEL:
358 case MotionEvent.ACTION_UP:
Winson Chung88f33452012-02-23 15:23:44 -0800359 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800360 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400361 case MotionEvent.ACTION_MOVE:
362 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
363 mLongPressHelper.cancelLongPress();
364 }
365 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800366 }
367 return result;
368 }
369
Michael Jurkaddd62e92011-02-16 17:49:14 -0800370 void setStayPressed(boolean stayPressed) {
371 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800372 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800373 }
374
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700375 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800376 public void onVisibilityAggregated(boolean isVisible) {
377 super.onVisibilityAggregated(isVisible);
378 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800379 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800380 }
381 }
382
383 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700384 public void onLauncherResume() {
385 // Reset the pressed state of icon that was locked in the press state while activity
386 // was launching
387 setStayPressed(false);
388 }
389
Sunny Goyal508da152014-08-14 10:53:27 -0700390 void clearPressedBackground() {
391 setPressed(false);
392 setStayPressed(false);
393 }
394
395 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700396 public boolean onKeyUp(int keyCode, KeyEvent event) {
397 // Unlike touch events, keypress event propagate pressed state change immediately,
398 // without waiting for onClickHandler to execute. Disable pressed state changes here
399 // to avoid flickering.
400 mIgnorePressedStateChange = true;
401 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700402 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800403 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700404 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800405 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800406
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700407 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800408 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700409 super.onDraw(canvas);
410 }
411
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700413 public void onDraw(Canvas canvas) {
414 super.onDraw(canvas);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800415 drawDotIfNecessary(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800416 }
417
418 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800419 * Draws the notification dot in the top right corner of the icon bounds.
Tony Wickham1237df02017-02-24 08:59:36 -0800420 * @param canvas The canvas to draw to.
421 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800422 protected void drawDotIfNecessary(Canvas canvas) {
423 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800424 getIconBounds(mDotParams.iconBounds);
Tony Wickhame1cb93f2019-05-03 11:27:32 -0700425 Utilities.scaleRectAboutCenter(mDotParams.iconBounds, IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800426 final int scrollX = getScrollX();
427 final int scrollY = getScrollY();
428 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800429 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800430 canvas.translate(-scrollX, -scrollY);
431 }
432 }
433
Jon Miranda9b78e192019-08-30 18:42:01 -0700434 @Override
435 public void setForceHideDot(boolean forceHideDot) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800436 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800437 return;
438 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800439 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800440
Tony Wickhamf34bee82018-12-03 18:11:39 -0800441 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800442 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800443 } else if (hasDot()) {
444 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800445 }
446 }
447
Tony Wickhamf34bee82018-12-03 18:11:39 -0800448 private boolean hasDot() {
449 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800450 }
451
452 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700453 getIconBounds(this, outBounds, mIconSize);
454 }
455
456 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
457 int top = iconView.getPaddingTop();
458 int left = (iconView.getWidth() - iconSize) / 2;
459 int right = left + iconSize;
460 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800461 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400463
464 @Override
Winson1f064272016-07-18 17:18:02 -0700465 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
466 if (mCenterVertically) {
467 Paint.FontMetrics fm = getPaint().getFontMetrics();
468 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
469 (int) Math.ceil(fm.bottom - fm.top);
470 int height = MeasureSpec.getSize(heightMeasureSpec);
471 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
472 getPaddingBottom());
473 }
474 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
475 }
476
477 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700478 public void setTextColor(int color) {
479 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700480 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700481 }
482
Adam Cohen96bb7982014-07-07 11:58:56 -0700483 @Override
484 public void setTextColor(ColorStateList colors) {
485 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700486 if (Float.compare(mTextAlpha, 1) == 0) {
487 super.setTextColor(colors);
488 } else {
489 super.setTextColor(getModifiedColor());
490 }
Adam Cohen477828c2013-09-20 12:05:49 -0700491 }
492
Tony7308cde2017-06-27 22:38:33 -0700493 public boolean shouldTextBeVisible() {
494 // Text should be visible everywhere but the hotseat.
495 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
496 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
497 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
498 }
499
Winson Chung5f8afe62013-08-12 16:19:28 -0700500 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700501 setTextAlpha(visible ? 1 : 0);
502 }
503
504 private void setTextAlpha(float alpha) {
505 mTextAlpha = alpha;
506 super.setTextColor(getModifiedColor());
507 }
508
509 private int getModifiedColor() {
510 if (mTextAlpha == 0) {
511 // Special case to prevent text shadows in high contrast mode
512 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700513 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800514 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700515 }
516
517 /**
518 * Creates an animator to fade the text in or out.
519 * @param fadeIn Whether the text should fade in or fade out.
520 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700521 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700522 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700523 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700524 }
525
Winson Chungaffd7b42010-08-20 15:11:56 -0700526 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800527 public void cancelLongPress() {
528 super.cancelLongPress();
529
530 mLongPressHelper.cancelLongPress();
531 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500532
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800533 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyal95899162019-03-27 16:03:06 -0700534 if (getTag() instanceof WorkspaceItemInfo) {
535 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700536 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700537 final int progressLevel = isPromise ?
Sunny Goyal95899162019-03-27 16:03:06 -0700538 ((info.hasStatusFlag(WorkspaceItemInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
Sunny Goyal34942622014-08-29 17:20:55 -0700539 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700540
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700541 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
542 if (preloadDrawable != null && promiseStateChanged) {
543 preloadDrawable.maybePerformFinishedAnimation();
544 }
545 }
546 }
547
548 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
549 if (getTag() instanceof ItemInfoWithIcon) {
550 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700551 if (progressLevel >= 100) {
552 setContentDescription(info.contentDescription != null
553 ? info.contentDescription : "");
554 } else if (progressLevel > 0) {
555 setContentDescription(getContext()
556 .getString(R.string.app_downloading_title, info.title,
557 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
558 } else {
559 setContentDescription(getContext()
560 .getString(R.string.app_waiting_download_title, info.title));
561 }
Winson Chungb745afb2015-03-02 11:51:23 -0800562 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700563 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800564 if (mIcon instanceof PreloadIconDrawable) {
565 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700566 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700567 } else {
Sunny Goyal2b787e52018-08-20 15:01:03 -0700568 preloadDrawable = DrawableFactory.INSTANCE.get(getContext())
569 .newPendingIcon(getContext(), info);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700570 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700571 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700572 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700573 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700574 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400575 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700576 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400577 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700578
Tony Wickhamf34bee82018-12-03 18:11:39 -0800579 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800580 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800581 boolean wasDotted = mDotInfo != null;
582 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
583 boolean isDotted = mDotInfo != null;
584 float newDotScale = isDotted ? 1f : 0;
Tony Wickham8912b042018-11-29 15:28:53 -0800585 mDotRenderer = mActivity.getDeviceProfile().mDotRenderer;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800586 if (wasDotted || isDotted) {
587 // Animate when a dot is first added or when it is removed.
588 if (animate && (wasDotted ^ isDotted) && isShown()) {
589 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800590 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800591 cancelDotScaleAnim();
592 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800593 invalidate();
594 }
595 }
Tony Wickham305e9202018-01-23 17:46:47 -0800596 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700597 if (itemInfo.isDisabled()) {
598 setContentDescription(getContext().getString(R.string.disabled_app_label,
599 itemInfo.contentDescription));
600 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800601 int count = mDotInfo.getNotificationCount();
Tony Wickham305e9202018-01-23 17:46:47 -0800602 setContentDescription(getContext().getResources().getQuantityString(
Tony Wickhamf34bee82018-12-03 18:11:39 -0800603 R.plurals.dotted_app_label, count, itemInfo.contentDescription, count));
Tony Wickham305e9202018-01-23 17:46:47 -0800604 } else {
605 setContentDescription(itemInfo.contentDescription);
606 }
607 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800608 }
609 }
610
Winson Chungb745afb2015-03-02 11:51:23 -0800611 /**
612 * Sets the icon for this view based on the layout direction.
613 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700614 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700615 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700616 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700617 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700618 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800619 if (mIcon != null) {
620 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
621 }
Tony17b7f9b2017-05-23 12:19:09 -0700622 }
623
Jon Miranda9b78e192019-08-30 18:42:01 -0700624 @Override
Tony17b7f9b2017-05-23 12:19:09 -0700625 public void setIconVisible(boolean visible) {
626 mIsIconVisible = visible;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700627 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700628 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700629 }
630
631 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700632 // If we had already set an icon before, disable relayout as the icon size is the
633 // same as before.
634 mDisableRelayout = mIcon != null;
635
636 icon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800637 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800638 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800639 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700640 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800641 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700642 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800643 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700644
Sunny Goyal69b75642015-05-15 17:00:24 -0700645 @Override
646 public void requestLayout() {
647 if (!mDisableRelayout) {
648 super.requestLayout();
649 }
650 }
651
Sunny Goyal34b65272015-03-11 16:56:52 -0700652 /**
653 * Applies the item info if it is same as what the view is pointing to currently.
654 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800655 @Override
656 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700657 if (getTag() == info) {
658 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700659 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700660
Jon Miranda7f522a22017-07-28 11:56:47 -0700661 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
662 info.iconBitmap.prepareToDraw();
663
Sunny Goyal34b65272015-03-11 16:56:52 -0700664 if (info instanceof AppInfo) {
665 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700666 } else if (info instanceof WorkspaceItemInfo) {
667 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700668 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700669 } else if (info instanceof PackageItemInfo) {
670 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700671 }
Winsonc0880492015-08-21 11:16:27 -0700672
Sunny Goyal69b75642015-05-15 17:00:24 -0700673 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700674 }
675 }
676
677 /**
678 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
679 */
680 public void verifyHighRes() {
681 if (mIconLoadRequest != null) {
682 mIconLoadRequest.cancel();
683 mIconLoadRequest = null;
684 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800685 if (getTag() instanceof ItemInfoWithIcon) {
686 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700687 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800688 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700689 .updateIconInBackground(BubbleTextView.this, info);
690 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700691 }
692 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700693
Jon Miranda47170112017-03-03 14:57:14 -0800694 public int getIconSize() {
695 return mIconSize;
696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697}