blob: 2f801e04b9f86684dbd228d75e0ebbc63afa272a [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;
Tony Wickhamf34bee82018-12-03 18:11:39 -080045import com.android.launcher3.dot.DotInfo;
Tony7308cde2017-06-27 22:38:33 -070046import com.android.launcher3.folder.FolderIcon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080047import com.android.launcher3.graphics.DrawableFactory;
Tony Wickham1237df02017-02-24 08:59:36 -080048import com.android.launcher3.graphics.IconPalette;
Tony Wickhame1cb93f2019-05-03 11:27:32 -070049import com.android.launcher3.graphics.IconShape;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080050import com.android.launcher3.graphics.PreloadIconDrawable;
Tony Wickham8912b042018-11-29 15:28:53 -080051import com.android.launcher3.icons.DotRenderer;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080052import com.android.launcher3.icons.IconCache.IconLoadRequest;
53import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070054import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080055import com.android.launcher3.views.ActivityContext;
Sunny Goyal34b65272015-03-11 16:56:52 -070056
Sunny Goyalc469aad2015-10-01 11:24:23 -070057import java.text.NumberFormat;
58
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059/**
60 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
61 * because we want to make the bubble taller than the text and TextView's clip is
62 * too aggressive.
63 */
Sunny Goyal3dce5f32017-10-05 11:40:05 -070064public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback {
Sunny Goyal95abbb32014-08-04 10:53:22 -070065
Sunny Goyaldfaccf62015-05-11 16:30:44 -070066 private static final int DISPLAY_WORKSPACE = 0;
67 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070068 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070069
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080070 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
71
Tony Wickham1237df02017-02-24 08:59:36 -080072
Tony Wickhamf34bee82018-12-03 18:11:39 -080073 private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY
74 = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") {
Tony Wickham1237df02017-02-24 08:59:36 -080075 @Override
76 public Float get(BubbleTextView bubbleTextView) {
Tony Wickham8912b042018-11-29 15:28:53 -080077 return bubbleTextView.mDotParams.scale;
Tony Wickham1237df02017-02-24 08:59:36 -080078 }
79
80 @Override
81 public void set(BubbleTextView bubbleTextView, Float value) {
Tony Wickham8912b042018-11-29 15:28:53 -080082 bubbleTextView.mDotParams.scale = value;
Tony Wickham1237df02017-02-24 08:59:36 -080083 bubbleTextView.invalidate();
84 }
85 };
86
Sunny Goyaled7a6932018-04-13 11:41:50 -070087 public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY
88 = new Property<BubbleTextView, Float>(Float.class, "textAlpha") {
Tony8f402802017-06-16 17:24:54 -070089 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070090 public Float get(BubbleTextView bubbleTextView) {
91 return bubbleTextView.mTextAlpha;
Tony8f402802017-06-16 17:24:54 -070092 }
93
94 @Override
Sunny Goyaled7a6932018-04-13 11:41:50 -070095 public void set(BubbleTextView bubbleTextView, Float alpha) {
Tony8f402802017-06-16 17:24:54 -070096 bubbleTextView.setTextAlpha(alpha);
97 }
98 };
99
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800100 private final ActivityContext mActivity;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700101 private Drawable mIcon;
102 private final boolean mCenterVertically;
103
104 private final CheckLongPressHelper mLongPressHelper;
105 private final StylusEventHelper mStylusEventHelper;
106 private final float mSlop;
107
108 private final boolean mLayoutHorizontal;
109 private final int mIconSize;
110
111 @ViewDebug.ExportedProperty(category = "launcher")
112 private boolean mIsIconVisible = true;
113 @ViewDebug.ExportedProperty(category = "launcher")
114 private int mTextColor;
115 @ViewDebug.ExportedProperty(category = "launcher")
116 private float mTextAlpha = 1;
117
Tony4043b052018-08-17 13:47:39 -0700118 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800119 private DotInfo mDotInfo;
Tony Wickham8912b042018-11-29 15:28:53 -0800120 private DotRenderer mDotRenderer;
121 @ViewDebug.ExportedProperty(category = "launcher", deepExport = true)
122 private DotRenderer.DrawParams mDotParams;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800123 private Animator mDotScaleAnim;
124 private boolean mForceHideDot;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700125
Sunny Goyal4ffec482016-02-09 11:28:52 -0800126 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800127 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800128 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700129 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800130 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700131 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500132
Sunny Goyal34b65272015-03-11 16:56:52 -0700133 private IconLoadRequest mIconLoadRequest;
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700136 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138
139 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700140 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 }
142
143 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
144 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800145 mActivity = ActivityContext.lookupContext(context);
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700146 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700147
Adam Cohen96bb7982014-07-07 11:58:56 -0700148 TypedArray a = context.obtainStyledAttributes(attrs,
149 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800150 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700151
152 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700153 final int defaultIconSize;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700154 if (display == DISPLAY_WORKSPACE) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700155 DeviceProfile grid = mActivity.getWallpaperDeviceProfile();
Winson Chung44d0aac2015-05-11 18:02:55 -0700156 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700157 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyalae6e3182019-04-30 12:04:37 -0700158 defaultIconSize = grid.iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700159 } else if (display == DISPLAY_ALL_APPS) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700160 DeviceProfile grid = mActivity.getDeviceProfile();
Winson1f064272016-07-18 17:18:02 -0700161 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
162 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700163 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700164 } else if (display == DISPLAY_FOLDER) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700165 DeviceProfile grid = mActivity.getDeviceProfile();
Jon Mirandabf7d8122016-11-03 15:29:29 -0700166 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700167 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700168 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyalae6e3182019-04-30 12:04:37 -0700169 } else {
170 defaultIconSize = mActivity.getDeviceProfile().iconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700171 }
Winson1f064272016-07-18 17:18:02 -0700172 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700173
174 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
175 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700176 a.recycle();
177
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800178 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700179 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800180
Tony Wickham8912b042018-11-29 15:28:53 -0800181 mDotParams = new DotRenderer.DrawParams();
182
Sunny Goyal66dccad2018-03-19 12:00:51 -0700183 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700184 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700185 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 }
187
Sunny Goyal66dccad2018-03-19 12:00:51 -0700188 @Override
189 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
190 // Disable marques when not focused to that, so that updating text does not cause relayout.
191 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
192 super.onFocusChanged(focused, direction, previouslyFocusedRect);
193 }
194
Jon Miranda52549442017-10-26 11:28:06 -0700195 /**
196 * Resets the view so it can be recycled.
197 */
198 public void reset() {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800199 mDotInfo = null;
Tony Wickham8912b042018-11-29 15:28:53 -0800200 mDotParams.color = Color.TRANSPARENT;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800201 cancelDotScaleAnim();
Tony Wickham8912b042018-11-29 15:28:53 -0800202 mDotParams.scale = 0f;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800203 mForceHideDot = false;
Jon Miranda52549442017-10-26 11:28:06 -0700204 }
205
Tony Wickhamf34bee82018-12-03 18:11:39 -0800206 private void cancelDotScaleAnim() {
207 if (mDotScaleAnim != null) {
208 mDotScaleAnim.cancel();
Tony4043b052018-08-17 13:47:39 -0700209 }
210 }
211
Tony Wickhamf34bee82018-12-03 18:11:39 -0800212 private void animateDotScale(float... dotScales) {
213 cancelDotScaleAnim();
214 mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales);
215 mDotScaleAnim.addListener(new AnimatorListenerAdapter() {
Tony4043b052018-08-17 13:47:39 -0700216 @Override
217 public void onAnimationEnd(Animator animation) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800218 mDotScaleAnim = null;
Tony4043b052018-08-17 13:47:39 -0700219 }
220 });
Tony Wickhamf34bee82018-12-03 18:11:39 -0800221 mDotScaleAnim.start();
Tony4043b052018-08-17 13:47:39 -0700222 }
223
Sunny Goyal95899162019-03-27 16:03:06 -0700224 public void applyFromWorkspaceItem(WorkspaceItemInfo info) {
225 applyFromWorkspaceItem(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700226 }
227
Sunny Goyal95899162019-03-27 16:03:06 -0700228 public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800229 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800230 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700231 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800232 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500233 }
Tony Wickham010d2552017-01-20 08:15:28 -0800234
Tony Wickhamf34bee82018-12-03 18:11:39 -0800235 applyDotState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800236 }
237
Sunny Goyal508da152014-08-14 10:53:27 -0700238 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800239 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700240
Sunny Goyal95899162019-03-27 16:03:06 -0700241 // We don't need to check the info since it's not a WorkspaceItemInfo
Winson Chung888b3a12015-03-13 11:14:16 -0700242 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700243
244 // Verify high res immediately
245 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800246
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700247 if (info instanceof PromiseAppInfo) {
248 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
249 applyProgressLevel(promiseAppInfo.level);
250 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800251 applyDotState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700252 }
253
Sunny Goyal0e08f162015-05-12 11:32:39 -0700254 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800255 applyIconAndLabel(info);
Sunny Goyal95899162019-03-27 16:03:06 -0700256 // We don't need to check the info since it's not a WorkspaceItemInfo
Sunny Goyal0e08f162015-05-12 11:32:39 -0700257 super.setTag(info);
258
259 // Verify high res immediately
260 verifyHighRes();
261 }
262
Sunny Goyal179249d2017-12-19 16:49:24 -0800263 private void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal2b787e52018-08-20 15:01:03 -0700264 FastBitmapDrawable iconDrawable = DrawableFactory.INSTANCE.get(getContext())
265 .newIcon(getContext(), info);
Tony Wickham8912b042018-11-29 15:28:53 -0800266 mDotParams.color = IconPalette.getMutedColor(info.iconColor, 0.54f);
Sunny Goyal179249d2017-12-19 16:49:24 -0800267
Sunny Goyalf4204382016-07-13 10:46:07 -0700268 setIcon(iconDrawable);
269 setText(info.title);
270 if (info.contentDescription != null) {
271 setContentDescription(info.isDisabled()
272 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
273 : info.contentDescription);
274 }
275 }
276
Winson Chung7501adf2015-06-02 11:24:28 -0700277 /**
278 * Overrides the default long press timeout.
279 */
Tony2ca999c2018-09-24 17:24:51 -0400280 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
281 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700282 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800285 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700286 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800287 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800288 }
Sunny Goyal508da152014-08-14 10:53:27 -0700289 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800290
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800291 @Override
292 protected int[] onCreateDrawableState(int extraSpace) {
293 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
294 if (mStayPressed) {
295 mergeDrawableStates(drawableState, STATE_PRESSED);
296 }
297 return drawableState;
298 }
299
Winson Chungb745afb2015-03-02 11:51:23 -0800300 /** Returns the icon for this view. */
301 public Drawable getIcon() {
302 return mIcon;
303 }
304
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700305 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800306 public boolean onTouchEvent(MotionEvent event) {
307 // Call the superclass onTouchEvent first, because sometimes it changes the state to
308 // isPressed() on an ACTION_UP
309 boolean result = super.onTouchEvent(event);
310
Mady Melloref044dd2015-06-02 15:35:07 -0700311 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700312 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700313 mLongPressHelper.cancelLongPress();
314 result = true;
315 }
316
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800317 switch (event.getAction()) {
318 case MotionEvent.ACTION_DOWN:
Mady Melloref044dd2015-06-02 15:35:07 -0700319 // If we're in a stylus button press, don't check for long press.
320 if (!mStylusEventHelper.inStylusButtonPressed()) {
321 mLongPressHelper.postCheckForLongPress();
322 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800323 break;
324 case MotionEvent.ACTION_CANCEL:
325 case MotionEvent.ACTION_UP:
Winson Chung88f33452012-02-23 15:23:44 -0800326 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800327 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400328 case MotionEvent.ACTION_MOVE:
329 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
330 mLongPressHelper.cancelLongPress();
331 }
332 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800333 }
334 return result;
335 }
336
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 void setStayPressed(boolean stayPressed) {
338 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800339 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800340 }
341
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700342 @Override
Hyunyoung Songef468d82019-01-03 01:02:43 -0800343 public void onVisibilityAggregated(boolean isVisible) {
344 super.onVisibilityAggregated(isVisible);
345 if (mIcon != null) {
Hyunyoung Songa2373852019-01-04 12:01:32 -0800346 mIcon.setVisible(isVisible, false);
Hyunyoung Songef468d82019-01-03 01:02:43 -0800347 }
348 }
349
350 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700351 public void onLauncherResume() {
352 // Reset the pressed state of icon that was locked in the press state while activity
353 // was launching
354 setStayPressed(false);
355 }
356
Sunny Goyal508da152014-08-14 10:53:27 -0700357 void clearPressedBackground() {
358 setPressed(false);
359 setStayPressed(false);
360 }
361
362 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700363 public boolean onKeyUp(int keyCode, KeyEvent event) {
364 // Unlike touch events, keypress event propagate pressed state change immediately,
365 // without waiting for onClickHandler to execute. Disable pressed state changes here
366 // to avoid flickering.
367 mIgnorePressedStateChange = true;
368 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700369 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800370 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700371 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800372 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800373
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700374 @SuppressWarnings("wrongcall")
Tony Wickhamf34bee82018-12-03 18:11:39 -0800375 protected void drawWithoutDot(Canvas canvas) {
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700376 super.onDraw(canvas);
377 }
378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700380 public void onDraw(Canvas canvas) {
381 super.onDraw(canvas);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800382 drawDotIfNecessary(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800383 }
384
385 /**
Tony Wickhamf34bee82018-12-03 18:11:39 -0800386 * Draws the notification dot in the top right corner of the icon bounds.
Tony Wickham1237df02017-02-24 08:59:36 -0800387 * @param canvas The canvas to draw to.
388 */
Tony Wickhamf34bee82018-12-03 18:11:39 -0800389 protected void drawDotIfNecessary(Canvas canvas) {
390 if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) {
Tony Wickham8912b042018-11-29 15:28:53 -0800391 getIconBounds(mDotParams.iconBounds);
Tony Wickhame1cb93f2019-05-03 11:27:32 -0700392 Utilities.scaleRectAboutCenter(mDotParams.iconBounds, IconShape.getNormalizationScale());
Tony Wickham1237df02017-02-24 08:59:36 -0800393 final int scrollX = getScrollX();
394 final int scrollY = getScrollY();
395 canvas.translate(scrollX, scrollY);
Tony Wickham8912b042018-11-29 15:28:53 -0800396 mDotRenderer.draw(canvas, mDotParams);
Tony Wickham1237df02017-02-24 08:59:36 -0800397 canvas.translate(-scrollX, -scrollY);
398 }
399 }
400
Tony Wickhamf34bee82018-12-03 18:11:39 -0800401 public void forceHideDot(boolean forceHideDot) {
402 if (mForceHideDot == forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800403 return;
404 }
Tony Wickhamf34bee82018-12-03 18:11:39 -0800405 mForceHideDot = forceHideDot;
Tony Wickham1237df02017-02-24 08:59:36 -0800406
Tony Wickhamf34bee82018-12-03 18:11:39 -0800407 if (forceHideDot) {
Tony Wickham1237df02017-02-24 08:59:36 -0800408 invalidate();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800409 } else if (hasDot()) {
410 animateDotScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800411 }
412 }
413
Tony Wickhamf34bee82018-12-03 18:11:39 -0800414 private boolean hasDot() {
415 return mDotInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800416 }
417
418 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700419 getIconBounds(this, outBounds, mIconSize);
420 }
421
422 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
423 int top = iconView.getPaddingTop();
424 int left = (iconView.getWidth() - iconSize) / 2;
425 int right = left + iconSize;
426 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800427 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429
430 @Override
Winson1f064272016-07-18 17:18:02 -0700431 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
432 if (mCenterVertically) {
433 Paint.FontMetrics fm = getPaint().getFontMetrics();
434 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
435 (int) Math.ceil(fm.bottom - fm.top);
436 int height = MeasureSpec.getSize(heightMeasureSpec);
437 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
438 getPaddingBottom());
439 }
440 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
441 }
442
443 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700444 public void setTextColor(int color) {
445 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700446 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700447 }
448
Adam Cohen96bb7982014-07-07 11:58:56 -0700449 @Override
450 public void setTextColor(ColorStateList colors) {
451 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700452 if (Float.compare(mTextAlpha, 1) == 0) {
453 super.setTextColor(colors);
454 } else {
455 super.setTextColor(getModifiedColor());
456 }
Adam Cohen477828c2013-09-20 12:05:49 -0700457 }
458
Tony7308cde2017-06-27 22:38:33 -0700459 public boolean shouldTextBeVisible() {
460 // Text should be visible everywhere but the hotseat.
461 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
462 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
463 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
464 }
465
Winson Chung5f8afe62013-08-12 16:19:28 -0700466 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700467 setTextAlpha(visible ? 1 : 0);
468 }
469
470 private void setTextAlpha(float alpha) {
471 mTextAlpha = alpha;
472 super.setTextColor(getModifiedColor());
473 }
474
475 private int getModifiedColor() {
476 if (mTextAlpha == 0) {
477 // Special case to prevent text shadows in high contrast mode
478 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700479 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800480 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700481 }
482
483 /**
484 * Creates an animator to fade the text in or out.
485 * @param fadeIn Whether the text should fade in or fade out.
486 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700487 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700488 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
Jon Miranda83cdca62019-04-15 13:01:05 -0700489 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700490 }
491
Winson Chungaffd7b42010-08-20 15:11:56 -0700492 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800493 public void cancelLongPress() {
494 super.cancelLongPress();
495
496 mLongPressHelper.cancelLongPress();
497 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500498
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800499 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyal95899162019-03-27 16:03:06 -0700500 if (getTag() instanceof WorkspaceItemInfo) {
501 WorkspaceItemInfo info = (WorkspaceItemInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700502 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700503 final int progressLevel = isPromise ?
Sunny Goyal95899162019-03-27 16:03:06 -0700504 ((info.hasStatusFlag(WorkspaceItemInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
Sunny Goyal34942622014-08-29 17:20:55 -0700505 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700506
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700507 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
508 if (preloadDrawable != null && promiseStateChanged) {
509 preloadDrawable.maybePerformFinishedAnimation();
510 }
511 }
512 }
513
514 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
515 if (getTag() instanceof ItemInfoWithIcon) {
516 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700517 if (progressLevel >= 100) {
518 setContentDescription(info.contentDescription != null
519 ? info.contentDescription : "");
520 } else if (progressLevel > 0) {
521 setContentDescription(getContext()
522 .getString(R.string.app_downloading_title, info.title,
523 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
524 } else {
525 setContentDescription(getContext()
526 .getString(R.string.app_waiting_download_title, info.title));
527 }
Winson Chungb745afb2015-03-02 11:51:23 -0800528 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700529 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800530 if (mIcon instanceof PreloadIconDrawable) {
531 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700532 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700533 } else {
Sunny Goyal2b787e52018-08-20 15:01:03 -0700534 preloadDrawable = DrawableFactory.INSTANCE.get(getContext())
535 .newPendingIcon(getContext(), info);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700536 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700537 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700538 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700539 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700540 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400541 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700542 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400543 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700544
Tony Wickhamf34bee82018-12-03 18:11:39 -0800545 public void applyDotState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800546 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800547 boolean wasDotted = mDotInfo != null;
548 mDotInfo = mActivity.getDotInfoForItem(itemInfo);
549 boolean isDotted = mDotInfo != null;
550 float newDotScale = isDotted ? 1f : 0;
Tony Wickham8912b042018-11-29 15:28:53 -0800551 mDotRenderer = mActivity.getDeviceProfile().mDotRenderer;
Tony Wickhamf34bee82018-12-03 18:11:39 -0800552 if (wasDotted || isDotted) {
553 // Animate when a dot is first added or when it is removed.
554 if (animate && (wasDotted ^ isDotted) && isShown()) {
555 animateDotScale(newDotScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800556 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800557 cancelDotScaleAnim();
558 mDotParams.scale = newDotScale;
Tony Wickham1237df02017-02-24 08:59:36 -0800559 invalidate();
560 }
561 }
Tony Wickham305e9202018-01-23 17:46:47 -0800562 if (itemInfo.contentDescription != null) {
vadimtda6dad52019-05-21 13:24:23 -0700563 if (itemInfo.isDisabled()) {
564 setContentDescription(getContext().getString(R.string.disabled_app_label,
565 itemInfo.contentDescription));
566 } else if (hasDot()) {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800567 int count = mDotInfo.getNotificationCount();
Tony Wickham305e9202018-01-23 17:46:47 -0800568 setContentDescription(getContext().getResources().getQuantityString(
Tony Wickhamf34bee82018-12-03 18:11:39 -0800569 R.plurals.dotted_app_label, count, itemInfo.contentDescription, count));
Tony Wickham305e9202018-01-23 17:46:47 -0800570 } else {
571 setContentDescription(itemInfo.contentDescription);
572 }
573 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800574 }
575 }
576
Winson Chungb745afb2015-03-02 11:51:23 -0800577 /**
578 * Sets the icon for this view based on the layout direction.
579 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700580 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700581 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700582 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700583 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700584 mIcon = icon;
Hyunyoung Songa2373852019-01-04 12:01:32 -0800585 if (mIcon != null) {
586 mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false);
587 }
Tony17b7f9b2017-05-23 12:19:09 -0700588 }
589
590 public void setIconVisible(boolean visible) {
591 mIsIconVisible = visible;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700592 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700593 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700594 }
595
596 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700597 // If we had already set an icon before, disable relayout as the icon size is the
598 // same as before.
599 mDisableRelayout = mIcon != null;
600
601 icon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800602 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800603 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800604 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700605 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800606 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700607 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800608 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700609
Sunny Goyal69b75642015-05-15 17:00:24 -0700610 @Override
611 public void requestLayout() {
612 if (!mDisableRelayout) {
613 super.requestLayout();
614 }
615 }
616
Sunny Goyal34b65272015-03-11 16:56:52 -0700617 /**
618 * Applies the item info if it is same as what the view is pointing to currently.
619 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800620 @Override
621 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700622 if (getTag() == info) {
623 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700624 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700625
Jon Miranda7f522a22017-07-28 11:56:47 -0700626 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
627 info.iconBitmap.prepareToDraw();
628
Sunny Goyal34b65272015-03-11 16:56:52 -0700629 if (info instanceof AppInfo) {
630 applyFromApplicationInfo((AppInfo) info);
Sunny Goyal95899162019-03-27 16:03:06 -0700631 } else if (info instanceof WorkspaceItemInfo) {
632 applyFromWorkspaceItem((WorkspaceItemInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700633 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700634 } else if (info instanceof PackageItemInfo) {
635 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700636 }
Winsonc0880492015-08-21 11:16:27 -0700637
Sunny Goyal69b75642015-05-15 17:00:24 -0700638 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700639 }
640 }
641
642 /**
643 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
644 */
645 public void verifyHighRes() {
646 if (mIconLoadRequest != null) {
647 mIconLoadRequest.cancel();
648 mIconLoadRequest = null;
649 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800650 if (getTag() instanceof ItemInfoWithIcon) {
651 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700652 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800653 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700654 .updateIconInBackground(BubbleTextView.this, info);
655 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700656 }
657 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700658
Jon Miranda47170112017-03-03 14:57:14 -0800659 public int getIconSize() {
660 return mIconSize;
661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662}