blob: f20ded364b2e909ae56835347b10f8a698435755 [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.Point;
31import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070032import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.graphics.drawable.Drawable;
Sunny Goyal66dccad2018-03-19 12:00:51 -070034import android.text.TextUtils.TruncateAt;
Winson Chung656d11c2010-11-29 17:15:47 -080035import android.util.AttributeSet;
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 Wickham9a8d11f2017-01-11 09:53:12 -080046import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080047import com.android.launcher3.badge.BadgeRenderer;
Tony7308cde2017-06-27 22:38:33 -070048import com.android.launcher3.folder.FolderIcon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080049import com.android.launcher3.graphics.DrawableFactory;
Tony Wickham1237df02017-02-24 08:59:36 -080050import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080051import com.android.launcher3.graphics.PreloadIconDrawable;
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
73 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
74 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
75 @Override
76 public Float get(BubbleTextView bubbleTextView) {
77 return bubbleTextView.mBadgeScale;
78 }
79
80 @Override
81 public void set(BubbleTextView bubbleTextView, Float value) {
82 bubbleTextView.mBadgeScale = value;
83 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")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700119 private BadgeInfo mBadgeInfo;
120 private BadgeRenderer mBadgeRenderer;
121 private int mBadgeColor;
Tony4043b052018-08-17 13:47:39 -0700122 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyaled7a6932018-04-13 11:41:50 -0700123 private float mBadgeScale;
Tony4043b052018-08-17 13:47:39 -0700124 private Animator mBadgeScaleAnim;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700125 private boolean mForceHideBadge;
126 private Point mTempSpaceForBadgeOffset = new Point();
127 private Rect mTempIconBounds = new Rect();
128
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
Sunny Goyal34b65272015-03-11 16:56:52 -0700136 private IconLoadRequest mIconLoadRequest;
137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700139 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 }
141
142 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700143 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 }
145
146 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
147 super(context, attrs, defStyle);
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800148 mActivity = ActivityContext.lookupContext(context);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700149 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700150 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700151
Adam Cohen96bb7982014-07-07 11:58:56 -0700152 TypedArray a = context.obtainStyledAttributes(attrs,
153 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800154 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700155
156 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
157 int defaultIconSize = grid.iconSizePx;
158 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700159 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700160 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700161 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700162 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
163 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700164 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700165 } else if (display == DISPLAY_FOLDER) {
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 Goyaldfaccf62015-05-11 16:30:44 -0700169 }
Winson1f064272016-07-18 17:18:02 -0700170 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700171
172 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
173 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700174 a.recycle();
175
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800176 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700177 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800178
Sunny Goyal66dccad2018-03-19 12:00:51 -0700179 setEllipsize(TruncateAt.END);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700180 setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
Sunny Goyaled7a6932018-04-13 11:41:50 -0700181 setTextAlpha(1f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 }
183
Sunny Goyal66dccad2018-03-19 12:00:51 -0700184 @Override
185 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
186 // Disable marques when not focused to that, so that updating text does not cause relayout.
187 setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END);
188 super.onFocusChanged(focused, direction, previouslyFocusedRect);
189 }
190
Jon Miranda52549442017-10-26 11:28:06 -0700191 /**
192 * Resets the view so it can be recycled.
193 */
194 public void reset() {
195 mBadgeInfo = null;
Sunny Goyal179249d2017-12-19 16:49:24 -0800196 mBadgeColor = Color.TRANSPARENT;
Tony4043b052018-08-17 13:47:39 -0700197 cancelBadgeScaleAnim();
Jon Miranda48043ee2017-11-01 15:40:22 -0700198 mBadgeScale = 0f;
Jon Miranda52549442017-10-26 11:28:06 -0700199 mForceHideBadge = false;
200 }
201
Tony4043b052018-08-17 13:47:39 -0700202 private void cancelBadgeScaleAnim() {
203 if (mBadgeScaleAnim != null) {
204 mBadgeScaleAnim.cancel();
205 }
206 }
207
208 private void animateBadgeScale(float... badgeScales) {
209 cancelBadgeScaleAnim();
210 mBadgeScaleAnim = ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, badgeScales);
211 mBadgeScaleAnim.addListener(new AnimatorListenerAdapter() {
212 @Override
213 public void onAnimationEnd(Animator animation) {
214 mBadgeScaleAnim = null;
215 }
216 });
217 mBadgeScaleAnim.start();
218 }
219
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800220 public void applyFromShortcutInfo(ShortcutInfo info) {
221 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700222 }
223
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800224 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800225 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800226 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700227 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800228 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500229 }
Tony Wickham010d2552017-01-20 08:15:28 -0800230
Tony Wickham1e618492017-02-02 12:57:18 -0800231 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800232 }
233
Sunny Goyal508da152014-08-14 10:53:27 -0700234 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800235 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700236
Winson Chung888b3a12015-03-13 11:14:16 -0700237 // We don't need to check the info since it's not a ShortcutInfo
238 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700239
240 // Verify high res immediately
241 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800242
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700243 if (info instanceof PromiseAppInfo) {
244 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
245 applyProgressLevel(promiseAppInfo.level);
246 }
Tony Wickham1e618492017-02-02 12:57:18 -0800247 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700248 }
249
Sunny Goyal0e08f162015-05-12 11:32:39 -0700250 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800251 applyIconAndLabel(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700252 // We don't need to check the info since it's not a ShortcutInfo
253 super.setTag(info);
254
255 // Verify high res immediately
256 verifyHighRes();
257 }
258
Sunny Goyal179249d2017-12-19 16:49:24 -0800259 private void applyIconAndLabel(ItemInfoWithIcon info) {
Sunny Goyal2b787e52018-08-20 15:01:03 -0700260 FastBitmapDrawable iconDrawable = DrawableFactory.INSTANCE.get(getContext())
261 .newIcon(getContext(), info);
Sunny Goyal179249d2017-12-19 16:49:24 -0800262 mBadgeColor = IconPalette.getMutedColor(info.iconColor, 0.54f);
263
Sunny Goyalf4204382016-07-13 10:46:07 -0700264 setIcon(iconDrawable);
265 setText(info.title);
266 if (info.contentDescription != null) {
267 setContentDescription(info.isDisabled()
268 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
269 : info.contentDescription);
270 }
271 }
272
Winson Chung7501adf2015-06-02 11:24:28 -0700273 /**
274 * Overrides the default long press timeout.
275 */
Tony2ca999c2018-09-24 17:24:51 -0400276 public void setLongPressTimeoutFactor(float longPressTimeoutFactor) {
277 mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor);
Winson Chung7501adf2015-06-02 11:24:28 -0700278 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700281 public void setTag(Object tag) {
282 if (tag != null) {
283 LauncherModel.checkItemInfo((ItemInfo) tag);
284 }
285 super.setTag(tag);
286 }
287
288 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800289 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700290 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800291 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800292 }
Sunny Goyal508da152014-08-14 10:53:27 -0700293 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800294
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800295 @Override
296 protected int[] onCreateDrawableState(int extraSpace) {
297 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
298 if (mStayPressed) {
299 mergeDrawableStates(drawableState, STATE_PRESSED);
300 }
301 return drawableState;
302 }
303
Winson Chungb745afb2015-03-02 11:51:23 -0800304 /** Returns the icon for this view. */
305 public Drawable getIcon() {
306 return mIcon;
307 }
308
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700309 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800310 public boolean onTouchEvent(MotionEvent event) {
311 // Call the superclass onTouchEvent first, because sometimes it changes the state to
312 // isPressed() on an ACTION_UP
313 boolean result = super.onTouchEvent(event);
314
Mady Melloref044dd2015-06-02 15:35:07 -0700315 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700316 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700317 mLongPressHelper.cancelLongPress();
318 result = true;
319 }
320
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800321 switch (event.getAction()) {
322 case MotionEvent.ACTION_DOWN:
Mady Melloref044dd2015-06-02 15:35:07 -0700323 // If we're in a stylus button press, don't check for long press.
324 if (!mStylusEventHelper.inStylusButtonPressed()) {
325 mLongPressHelper.postCheckForLongPress();
326 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800327 break;
328 case MotionEvent.ACTION_CANCEL:
329 case MotionEvent.ACTION_UP:
Winson Chung88f33452012-02-23 15:23:44 -0800330 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800331 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400332 case MotionEvent.ACTION_MOVE:
333 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
334 mLongPressHelper.cancelLongPress();
335 }
336 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800337 }
338 return result;
339 }
340
Michael Jurkaddd62e92011-02-16 17:49:14 -0800341 void setStayPressed(boolean stayPressed) {
342 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800343 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800344 }
345
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700346 @Override
347 public void onLauncherResume() {
348 // Reset the pressed state of icon that was locked in the press state while activity
349 // was launching
350 setStayPressed(false);
351 }
352
Sunny Goyal508da152014-08-14 10:53:27 -0700353 void clearPressedBackground() {
354 setPressed(false);
355 setStayPressed(false);
356 }
357
358 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700359 public boolean onKeyUp(int keyCode, KeyEvent event) {
360 // Unlike touch events, keypress event propagate pressed state change immediately,
361 // without waiting for onClickHandler to execute. Disable pressed state changes here
362 // to avoid flickering.
363 mIgnorePressedStateChange = true;
364 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700365 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800366 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700367 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800368 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800369
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700370 @SuppressWarnings("wrongcall")
371 protected void drawWithoutBadge(Canvas canvas) {
372 super.onDraw(canvas);
373 }
374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700376 public void onDraw(Canvas canvas) {
377 super.onDraw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800378 drawBadgeIfNecessary(canvas);
379 }
380
381 /**
382 * Draws the icon badge in the top right corner of the icon bounds.
383 * @param canvas The canvas to draw to.
384 */
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700385 protected void drawBadgeIfNecessary(Canvas canvas) {
Tony Wickham1237df02017-02-24 08:59:36 -0800386 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
387 getIconBounds(mTempIconBounds);
388 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
389 final int scrollX = getScrollX();
390 final int scrollY = getScrollY();
391 canvas.translate(scrollX, scrollY);
Sunny Goyal179249d2017-12-19 16:49:24 -0800392 mBadgeRenderer.draw(canvas, mBadgeColor, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800393 mTempSpaceForBadgeOffset);
394 canvas.translate(-scrollX, -scrollY);
395 }
396 }
397
398 public void forceHideBadge(boolean forceHideBadge) {
399 if (mForceHideBadge == forceHideBadge) {
400 return;
401 }
402 mForceHideBadge = forceHideBadge;
403
404 if (forceHideBadge) {
405 invalidate();
406 } else if (hasBadge()) {
Tony4043b052018-08-17 13:47:39 -0700407 animateBadgeScale(0, 1);
Tony Wickham1237df02017-02-24 08:59:36 -0800408 }
409 }
410
411 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700412 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800413 }
414
415 public void getIconBounds(Rect outBounds) {
Tony05d98c22018-07-23 08:01:15 -0700416 getIconBounds(this, outBounds, mIconSize);
417 }
418
419 public static void getIconBounds(View iconView, Rect outBounds, int iconSize) {
420 int top = iconView.getPaddingTop();
421 int left = (iconView.getWidth() - iconSize) / 2;
422 int right = left + iconSize;
423 int bottom = top + iconSize;
Tony Wickham1237df02017-02-24 08:59:36 -0800424 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400426
427 @Override
Winson1f064272016-07-18 17:18:02 -0700428 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
429 if (mCenterVertically) {
430 Paint.FontMetrics fm = getPaint().getFontMetrics();
431 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
432 (int) Math.ceil(fm.bottom - fm.top);
433 int height = MeasureSpec.getSize(heightMeasureSpec);
434 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
435 getPaddingBottom());
436 }
437 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
438 }
439
440 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700441 public void setTextColor(int color) {
442 mTextColor = color;
Sunny Goyaled7a6932018-04-13 11:41:50 -0700443 super.setTextColor(getModifiedColor());
Adam Cohen477828c2013-09-20 12:05:49 -0700444 }
445
Adam Cohen96bb7982014-07-07 11:58:56 -0700446 @Override
447 public void setTextColor(ColorStateList colors) {
448 mTextColor = colors.getDefaultColor();
Sunny Goyaled7a6932018-04-13 11:41:50 -0700449 if (Float.compare(mTextAlpha, 1) == 0) {
450 super.setTextColor(colors);
451 } else {
452 super.setTextColor(getModifiedColor());
453 }
Adam Cohen477828c2013-09-20 12:05:49 -0700454 }
455
Tony7308cde2017-06-27 22:38:33 -0700456 public boolean shouldTextBeVisible() {
457 // Text should be visible everywhere but the hotseat.
458 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
459 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
460 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
461 }
462
Winson Chung5f8afe62013-08-12 16:19:28 -0700463 public void setTextVisibility(boolean visible) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700464 setTextAlpha(visible ? 1 : 0);
465 }
466
467 private void setTextAlpha(float alpha) {
468 mTextAlpha = alpha;
469 super.setTextColor(getModifiedColor());
470 }
471
472 private int getModifiedColor() {
473 if (mTextAlpha == 0) {
474 // Special case to prevent text shadows in high contrast mode
475 return Color.TRANSPARENT;
Winson Chung5f8afe62013-08-12 16:19:28 -0700476 }
Sunny Goyal066ace12018-11-05 11:08:31 -0800477 return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha));
Tony8f402802017-06-16 17:24:54 -0700478 }
479
480 /**
481 * Creates an animator to fade the text in or out.
482 * @param fadeIn Whether the text should fade in or fade out.
483 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700484 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Sunny Goyaled7a6932018-04-13 11:41:50 -0700485 float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0;
486 return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700487 }
488
Winson Chungaffd7b42010-08-20 15:11:56 -0700489 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800490 public void cancelLongPress() {
491 super.cancelLongPress();
492
493 mLongPressHelper.cancelLongPress();
494 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500495
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800496 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700497 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400498 ShortcutInfo info = (ShortcutInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700499 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700500 final int progressLevel = isPromise ?
501 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
502 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700503
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700504 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
505 if (preloadDrawable != null && promiseStateChanged) {
506 preloadDrawable.maybePerformFinishedAnimation();
507 }
508 }
509 }
510
511 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
512 if (getTag() instanceof ItemInfoWithIcon) {
513 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700514 if (progressLevel >= 100) {
515 setContentDescription(info.contentDescription != null
516 ? info.contentDescription : "");
517 } else if (progressLevel > 0) {
518 setContentDescription(getContext()
519 .getString(R.string.app_downloading_title, info.title,
520 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
521 } else {
522 setContentDescription(getContext()
523 .getString(R.string.app_waiting_download_title, info.title));
524 }
Winson Chungb745afb2015-03-02 11:51:23 -0800525 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700526 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800527 if (mIcon instanceof PreloadIconDrawable) {
528 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700529 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700530 } else {
Sunny Goyal2b787e52018-08-20 15:01:03 -0700531 preloadDrawable = DrawableFactory.INSTANCE.get(getContext())
532 .newPendingIcon(getContext(), info);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700533 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700534 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700535 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700536 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700537 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400538 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700539 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400540 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700541
Tony Wickham1e618492017-02-02 12:57:18 -0800542 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800543 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800544 boolean wasBadged = mBadgeInfo != null;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700545 mBadgeInfo = mActivity.getBadgeInfoForItem(itemInfo);
Tony Wickham2fe09f22017-04-25 12:46:04 -0700546 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800547 float newBadgeScale = isBadged ? 1f : 0;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700548 mBadgeRenderer = mActivity.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800549 if (wasBadged || isBadged) {
Tony Wickham1237df02017-02-24 08:59:36 -0800550 // Animate when a badge is first added or when it is removed.
551 if (animate && (wasBadged ^ isBadged) && isShown()) {
Tony4043b052018-08-17 13:47:39 -0700552 animateBadgeScale(newBadgeScale);
Tony Wickham1237df02017-02-24 08:59:36 -0800553 } else {
Tony4043b052018-08-17 13:47:39 -0700554 cancelBadgeScaleAnim();
Tony Wickham1237df02017-02-24 08:59:36 -0800555 mBadgeScale = newBadgeScale;
556 invalidate();
557 }
558 }
Tony Wickham305e9202018-01-23 17:46:47 -0800559 if (itemInfo.contentDescription != null) {
560 if (hasBadge()) {
561 int count = mBadgeInfo.getNotificationCount();
562 setContentDescription(getContext().getResources().getQuantityString(
563 R.plurals.badged_app_label, count, itemInfo.contentDescription, count));
564 } else {
565 setContentDescription(itemInfo.contentDescription);
566 }
567 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800568 }
569 }
570
Winson Chungb745afb2015-03-02 11:51:23 -0800571 /**
572 * Sets the icon for this view based on the layout direction.
573 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700574 private void setIcon(Drawable icon) {
Tony17b7f9b2017-05-23 12:19:09 -0700575 if (mIsIconVisible) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700576 applyCompoundDrawables(icon);
Tony17b7f9b2017-05-23 12:19:09 -0700577 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700578 mIcon = icon;
Tony17b7f9b2017-05-23 12:19:09 -0700579 }
580
581 public void setIconVisible(boolean visible) {
582 mIsIconVisible = visible;
Sunny Goyal66dccad2018-03-19 12:00:51 -0700583 Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT);
Tony17b7f9b2017-05-23 12:19:09 -0700584 applyCompoundDrawables(icon);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700585 }
586
587 protected void applyCompoundDrawables(Drawable icon) {
Sunny Goyal66dccad2018-03-19 12:00:51 -0700588 // If we had already set an icon before, disable relayout as the icon size is the
589 // same as before.
590 mDisableRelayout = mIcon != null;
591
592 icon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800593 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800594 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800595 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700596 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800597 }
Sunny Goyal66dccad2018-03-19 12:00:51 -0700598 mDisableRelayout = false;
Winson Chungb745afb2015-03-02 11:51:23 -0800599 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700600
Sunny Goyal69b75642015-05-15 17:00:24 -0700601 @Override
602 public void requestLayout() {
603 if (!mDisableRelayout) {
604 super.requestLayout();
605 }
606 }
607
Sunny Goyal34b65272015-03-11 16:56:52 -0700608 /**
609 * Applies the item info if it is same as what the view is pointing to currently.
610 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800611 @Override
612 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700613 if (getTag() == info) {
614 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700615 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700616
Jon Miranda7f522a22017-07-28 11:56:47 -0700617 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
618 info.iconBitmap.prepareToDraw();
619
Sunny Goyal34b65272015-03-11 16:56:52 -0700620 if (info instanceof AppInfo) {
621 applyFromApplicationInfo((AppInfo) info);
622 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800623 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700624 mActivity.invalidateParent(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700625 } else if (info instanceof PackageItemInfo) {
626 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700627 }
Winsonc0880492015-08-21 11:16:27 -0700628
Sunny Goyal69b75642015-05-15 17:00:24 -0700629 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700630 }
631 }
632
633 /**
634 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
635 */
636 public void verifyHighRes() {
637 if (mIconLoadRequest != null) {
638 mIconLoadRequest.cancel();
639 mIconLoadRequest = null;
640 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800641 if (getTag() instanceof ItemInfoWithIcon) {
642 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal2b787e52018-08-20 15:01:03 -0700643 if (info.usingLowResIcon()) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800644 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700645 .updateIconInBackground(BubbleTextView.this, info);
646 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700647 }
648 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700649
Jon Miranda47170112017-03-03 14:57:14 -0800650 public int getIconSize() {
651 return mIconSize;
652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653}