blob: 7085c60521eaf7bbf6a5e5707574177ce28fb5d9 [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;
vadimtf02bef72019-06-13 14:04:31 -070035import android.util.Log;
Tony Wickham1237df02017-02-24 08:59:36 -080036import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070037import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070038import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080039import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070040import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040041import android.view.ViewConfiguration;
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;
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;
vadimtf02bef72019-06-13 14:04:31 -070056import com.android.launcher3.testing.TestProtocol;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080057import com.android.launcher3.views.ActivityContext;
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 */
Sunny Goyal3dce5f32017-10-05 11:40:05 -070066public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback {
Sunny Goyal95abbb32014-08-04 10:53:22 -070067
Sunny Goyaldfaccf62015-05-11 16:30:44 -070068 private static final int DISPLAY_WORKSPACE = 0;
69 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070070 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070071
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080072 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
73
Tony Wickham1237df02017-02-24 08:59:36 -080074
Tony Wickhamf34bee82018-12-03 18:11:39 -080075 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
76 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -080077 @Override
78 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -080079 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -080080 }
81
82 @Override
83 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -080084 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -080085 bubbleTextView.invalidate();
86 }
87 };
88
Sunny Goyaled7a6932018-04-13 11:41:50 -070089 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
90 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -070091 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070092 public Float get(BubbleTextView bubbleTextView) {
93 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -070094 }
95
96 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070097 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -070098 bubbleTextView.setTextAlpha(alpha);
99 }
100 };
101
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800102 private final ActivityContext mActivity;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700103 private Drawable mIcon;
104 private final boolean mCenterVertically;
105
106 private final CheckLongPressHelper mLongPressHelper;
107 private final StylusEventHelper mStylusEventHelper;
108 private final float mSlop;
109
110 private final boolean mLayoutHorizontal;
111 private final int mIconSize;
112
113 @ViewDebug.ExportedProperty(category = "launcher")
114 private boolean mIsIconVisible = true;
115 @ViewDebug.ExportedProperty(category = "launcher")
116 private int mTextColor;
117 @ViewDebug.ExportedProperty(category = "launcher")
118 private float mTextAlpha = 1;
119
Tony4043b052018-08-17 13:47:39 -0700120 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800121 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800122 private DotRenderer mDotRenderer;
123 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
124 private DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800125 private Animator mDotScaleAnim;
126 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700127
Sunny Goyal4ffec482016-02-09 11:28:52 -0800128 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800129 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800130 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700131 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800132 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700133 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500134
Sunny Goyal34b65272015-03-11 16:56:52 -0700135 private IconLoadRequest mIconLoadRequest;
136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700138 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 }
140
141 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700142 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 }
144
145 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
146 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800147 mActivity = ActivityContext.lookupContext(context);
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700148 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700149
Adam Cohen96bb7982014-07-07 11:58:56 -0700150 TypedArray a = context.obtainStyledAttributes(attrs,
151 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800152 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700153
154 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700155 final int defaultIconSize;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700156 if (display == DISPLAY_WORKSPACE) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700157 DeviceProfile grid = mActivity.getWallpaperDeviceProfile();
Winson Chung44d0aac2015-05-11 18:02:55 -0700158 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700159 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700160 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700161 } else if (display == DISPLAY_ALL_APPS) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700162 DeviceProfile grid = mActivity.getDeviceProfile();
Winson1f064272016-07-18 17:18:02 -0700163 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
164 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700165 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700166 } else if (display == DISPLAY_FOLDER) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700167 DeviceProfile grid = mActivity.getDeviceProfile();
Jon Mirandabf7d8122016-11-03 15:29:29 -0700168 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700169 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700170 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700171 } else {
172 defaultIconSize = mActivity.getDeviceProfile().iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700173 }
Winson1f064272016-07-18 17:18:02 -0700174 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700175
176 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
177 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700178 a.recycle();
179
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800180 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700181 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800182
Tony Wickham8912b042018-11-29 15:28:53 -0800183 mDotParams = new DotRenderer.DrawParams();
184
Sunny Goyal66dccad2018-03-19 12:00:51 -0700185 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700186 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700187 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 }
189
Sunny Goyal66dccad2018-03-19 12:00:51 -0700190 @Override
191 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
192 // Disable marques when not focused to that, so that updating text does not cause relayout.
193 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
194 super.onFocusChanged(focused, direction, previouslyFocusedRect);
195 }
196
Jon Miranda52549442017-10-26 11:28:06 -0700197 /**
198 * Resets the view so it can be recycled.
199 */
200 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800201 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800202 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800203 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800204 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800205 mForceHideDot = false;
Jon Miranda52549442017-10-26 11:28:06 -0700206 }
207
Tony Wickhamf34bee82018-12-03 18:11:39 -0800208 private void cancelDotScaleAnim() {
209 if (mDotScaleAnim != null) {
210 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700211 }
212 }
213
Tony Wickhamf34bee82018-12-03 18:11:39 -0800214 private void animateDotScale(float... dotScales) {
215 cancelDotScaleAnim();
216 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
217 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700218 @Override
219 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800220 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700221 }
222 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800223 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700224 }
225
Sunny Goyal95899162019-03-27 16:03:06 -0700226 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
227 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700228 }
229
Sunny Goyal95899162019-03-27 16:03:06 -0700230 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800231 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800232 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700233 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800234 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500235 }
Tony Wickham010d2552017-01-20 08:15:28 -0800236
Tony Wickhamf34bee82018-12-03 18:11:39 -0800237 applyDotState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800238 }
239
Sunny Goyal508da152014-08-14 10:53:27 -0700240 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800241 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700242
Sunny Goyal95899162019-03-27 16:03:06 -0700243 // We don't need to check the info since it's not a WorkspaceItemInfo
Winson Chung888b3a12015-03-13 11:14:16 -0700244 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700245
246 // Verify high res immediately
247 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800248
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700249 if (info instanceof PromiseAppInfo) {
250 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
251 applyProgressLevel(promiseAppInfo.level);
252 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800253 applyDotState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700254 }
255
Sunny Goyal0e08f162015-05-12 11:32:39 -0700256 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800257 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700258 // We don't need to check the info since it's not a WorkspaceItemInfo
Sunny Goyal0e08f162015-05-12 11:32:39 -0700259 super.setTag(info);
260
261 // Verify high res immediately
262 verifyHighRes();
263 }
264
Sunny Goyal179249d2017-12-19 16:49:24 -0800265 private void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal2b787e52018-08-20 15:01:03 -0700266 FastBitmapDrawable iconDrawable = DrawableFactory.INSTANCE.get(getContext())
267 .newIcon(getContext(), info);
Tony Wickham8912b042018-11-29 15:28:53 -0800268 mDotParams.color = IconPalette.getMutedColor(info.iconColor, 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800269
Sunny Goyalf4204382016-07-13 10:46:07 -0700270 setIcon(iconDrawable);
271 setText(info.title);
272 if (info.contentDescription != null) {
273 setContentDescription(info.isDisabled()
274 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
275 : info.contentDescription);
276 }
277 }
278
Winson Chung7501adf2015-06-02 11:24:28 -0700279 /**
280 * Overrides the default long press timeout.
281 */
Tony2ca999c2018-09-24 17:24:51 -0400282 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
283 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700284 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800287 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700288 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800289 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800290 }
Sunny Goyal508da152014-08-14 10:53:27 -0700291 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800292
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800293 @Override
294 protected int[] onCreateDrawableState(int extraSpace) {
295 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
296 if (mStayPressed) {
297 mergeDrawableStates(drawableState, STATE_PRESSED);
298 }
299 return drawableState;
300 }
301
Winson Chungb745afb2015-03-02 11:51:23 -0800302 /** Returns the icon for this view. */
303 public Drawable getIcon() {
304 return mIcon;
305 }
306
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700307 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800308 public boolean onTouchEvent(MotionEvent event) {
vadimtf02bef72019-06-13 14:04:31 -0700309 if (TestProtocol.sDebugTracing) {
310 Log.d(TestProtocol.NO_START_TAG, "BubbleTextView.onTouchEvent " + event);
311 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800312 // Call the superclass onTouchEvent first, because sometimes it changes the state to
313 // isPressed() on an ACTION_UP
314 boolean result = super.onTouchEvent(event);
315
Mady Melloref044dd2015-06-02 15:35:07 -0700316 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700317 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700318 mLongPressHelper.cancelLongPress();
319 result = true;
320 }
321
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800322 switch (event.getAction()) {
323 case MotionEvent.ACTION_DOWN:
Mady Melloref044dd2015-06-02 15:35:07 -0700324 // If we're in a stylus button press, don't check for long press.
325 if (!mStylusEventHelper.inStylusButtonPressed()) {
326 mLongPressHelper.postCheckForLongPress();
327 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800328 break;
329 case MotionEvent.ACTION_CANCEL:
330 case MotionEvent.ACTION_UP:
Winson Chung88f33452012-02-23 15:23:44 -0800331 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800332 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400333 case MotionEvent.ACTION_MOVE:
334 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
335 mLongPressHelper.cancelLongPress();
336 }
337 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800338 }
339 return result;
340 }
341
Michael Jurkaddd62e92011-02-16 17:49:14 -0800342 void setStayPressed(boolean stayPressed) {
343 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800344 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800345 }
346
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700347 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800348 public void onVisibilityAggregated(boolean isVisible) {
349 super.onVisibilityAggregated(isVisible);
350 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800351 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800352 }
353 }
354
355 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700356 public void onLauncherResume() {
357 // Reset the pressed state of icon that was locked in the press state while activity
358 // was launching
359 setStayPressed(false);
360 }
361
Sunny Goyal508da152014-08-14 10:53:27 -0700362 void clearPressedBackground() {
363 setPressed(false);
364 setStayPressed(false);
365 }
366
367 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700368 public boolean onKeyUp(int keyCode, KeyEvent event) {
369 // Unlike touch events, keypress event propagate pressed state change immediately,
370 // without waiting for onClickHandler to execute. Disable pressed state changes here
371 // to avoid flickering.
372 mIgnorePressedStateChange = true;
373 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700374 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800375 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700376 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800377 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800378
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700379 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800380 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700381 super.onDraw(canvas);
382 }
383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700385 public void onDraw(Canvas canvas) {
386 super.onDraw(canvas);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800387 drawDotIfNecessary(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800388 }
389
390 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800391 * Draws the notification dot in the top right corner of the icon bounds.
Tony Wickham1237df02017-02-24 08:59:36 -0800392 * @param canvas The canvas to draw to.
393 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800394 protected void drawDotIfNecessary(Canvas canvas) {
395 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800396 getIconBounds(mDotParams.iconBounds);
Tony Wickhame1cb93f2019-05-03 11:27:32 -0700397 Utilities.scaleRectAboutCenter(mDotParams.iconBounds, IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800398 final int scrollX = getScrollX();
399 final int scrollY = getScrollY();
400 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800401 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800402 canvas.translate(-scrollX, -scrollY);
403 }
404 }
405
Tony Wickhamf34bee82018-12-03 18:11:39 -0800406 public void forceHideDot(boolean forceHideDot) {
407 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;
468 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
469 }
470
Winson Chung5f8afe62013-08-12 16:19:28 -0700471 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700472 setTextAlpha(visible ? 1 : 0);
473 }
474
475 private void setTextAlpha(float alpha) {
476 mTextAlpha = alpha;
477 super.setTextColor(getModifiedColor());
478 }
479
480 private int getModifiedColor() {
481 if (mTextAlpha == 0) {
482 // Special case to prevent text shadows in high contrast mode
483 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700484 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800485 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700486 }
487
488 /**
489 * Creates an animator to fade the text in or out.
490 * @param fadeIn Whether the text should fade in or fade out.
491 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700492 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700493 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700494 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700495 }
496
Winson Chungaffd7b42010-08-20 15:11:56 -0700497 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800498 public void cancelLongPress() {
499 super.cancelLongPress();
500
501 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 Goyal2b787e52018-08-20 15:01:03 -0700539 preloadDrawable = DrawableFactory.INSTANCE.get(getContext())
540 .newPendingIcon(getContext(), info);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700541 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700542 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700543 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700544 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700545 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400546 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700547 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400548 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700549
Tony Wickhamf34bee82018-12-03 18:11:39 -0800550 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800551 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800552 boolean wasDotted = mDotInfo != null;
553 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
554 boolean isDotted = mDotInfo != null;
555 float newDotScale = isDotted ? 1f : 0;
Tony Wickham8912b042018-11-29 15:28:53 -0800556 mDotRenderer = mActivity.getDeviceProfile().mDotRenderer;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800557 if (wasDotted || isDotted) {
558 // Animate when a dot is first added or when it is removed.
559 if (animate && (wasDotted ^ isDotted) && isShown()) {
560 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800561 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800562 cancelDotScaleAnim();
563 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800564 invalidate();
565 }
566 }
Tony Wickham305e9202018-01-23 17:46:47 -0800567 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700568 if (itemInfo.isDisabled()) {
569 setContentDescription(getContext().getString(R.string.disabled_app_label,
570 itemInfo.contentDescription));
571 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800572 int count = mDotInfo.getNotificationCount();
Tony Wickham305e9202018-01-23 17:46:47 -0800573 setContentDescription(getContext().getResources().getQuantityString(
Tony Wickhamf34bee82018-12-03 18:11:39 -0800574 R.plurals.dotted_app_label, count, itemInfo.contentDescription, count));
Tony Wickham305e9202018-01-23 17:46:47 -0800575 } else {
576 setContentDescription(itemInfo.contentDescription);
577 }
578 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800579 }
580 }
581
Winson Chungb745afb2015-03-02 11:51:23 -0800582 /**
583 * Sets the icon for this view based on the layout direction.
584 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700585 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700586 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700587 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700588 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700589 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800590 if (mIcon != null) {
591 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
592 }
Tony17b7f9b2017-05-23 12:19:09 -0700593 }
594
595 public void setIconVisible(boolean visible) {
596 mIsIconVisible = visible;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700597 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700598 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700599 }
600
601 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700602 // If we had already set an icon before, disable relayout as the icon size is the
603 // same as before.
604 mDisableRelayout = mIcon != null;
605
606 icon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800607 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800608 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800609 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700610 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800611 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700612 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800613 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700614
Sunny Goyal69b75642015-05-15 17:00:24 -0700615 @Override
616 public void requestLayout() {
617 if (!mDisableRelayout) {
618 super.requestLayout();
619 }
620 }
621
Sunny Goyal34b65272015-03-11 16:56:52 -0700622 /**
623 * Applies the item info if it is same as what the view is pointing to currently.
624 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800625 @Override
626 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700627 if (getTag() == info) {
628 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700629 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700630
Jon Miranda7f522a22017-07-28 11:56:47 -0700631 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
632 info.iconBitmap.prepareToDraw();
633
Sunny Goyal34b65272015-03-11 16:56:52 -0700634 if (info instanceof AppInfo) {
635 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700636 } else if (info instanceof WorkspaceItemInfo) {
637 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700638 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700639 } else if (info instanceof PackageItemInfo) {
640 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700641 }
Winsonc0880492015-08-21 11:16:27 -0700642
Sunny Goyal69b75642015-05-15 17:00:24 -0700643 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700644 }
645 }
646
647 /**
648 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
649 */
650 public void verifyHighRes() {
651 if (mIconLoadRequest != null) {
652 mIconLoadRequest.cancel();
653 mIconLoadRequest = null;
654 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800655 if (getTag() instanceof ItemInfoWithIcon) {
656 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700657 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800658 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700659 .updateIconInBackground(BubbleTextView.this, info);
660 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700661 }
662 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700663
Jon Miranda47170112017-03-03 14:57:14 -0800664 public int getIconSize() {
665 return mIconSize;
666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667}