blob: 8b6d9f825f4238fdf8bb8815a43efb77b916d555 [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
Tony Wickham1237df02017-02-24 08:59:36 -080019import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070021import android.content.res.ColorStateList;
Adam Cohen96bb7982014-07-07 11:58:56 -070022import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070024import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070025import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080026import android.graphics.Point;
27import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070028import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.drawable.Drawable;
Tony8f402802017-06-16 17:24:54 -070030import android.support.v4.graphics.ColorUtils;
Winson Chung656d11c2010-11-29 17:15:47 -080031import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080032import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070033import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070034import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080035import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070036import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040037import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080038import android.view.ViewDebug;
Michael Jurkabdb5c532011-02-01 15:05:06 -080039import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070040
Sunny Goyal34b65272015-03-11 16:56:52 -070041import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080042import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070043import com.android.launcher3.Launcher.OnResumeCallback;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080044import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080045import com.android.launcher3.badge.BadgeRenderer;
Tony7308cde2017-06-27 22:38:33 -070046import com.android.launcher3.folder.FolderIcon;
Jon Miranda529af302017-02-28 14:18:54 -080047import com.android.launcher3.folder.FolderIconPreviewVerifier;
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;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080050import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070051import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070052
Sunny Goyalc469aad2015-10-01 11:24:23 -070053import java.text.NumberFormat;
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055/**
56 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
57 * because we want to make the bubble taller than the text and TextView's clip is
58 * too aggressive.
59 */
Sunny Goyal3dce5f32017-10-05 11:40:05 -070060public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback {
Sunny Goyal95abbb32014-08-04 10:53:22 -070061
Sunny Goyaldfaccf62015-05-11 16:30:44 -070062 private static final int DISPLAY_WORKSPACE = 0;
63 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070064 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070065
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080066 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
67
Sunny Goyal53d7ee42015-05-22 12:25:45 -070068 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080069 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070070 private final boolean mCenterVertically;
Sunny Goyal4e5a8782017-06-23 09:34:06 -070071
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080072 private final CheckLongPressHelper mLongPressHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070073 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal9314b7c2017-06-23 10:36:27 -070074 private final float mSlop;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075
Winson Chungb745afb2015-03-02 11:51:23 -080076 private final boolean mLayoutHorizontal;
77 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080078 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080079 private int mTextColor;
Tony17b7f9b2017-05-23 12:19:09 -070080 private boolean mIsIconVisible = true;
Winson Chung5f8afe62013-08-12 16:19:28 -070081
Tony Wickham1237df02017-02-24 08:59:36 -080082 private BadgeInfo mBadgeInfo;
83 private BadgeRenderer mBadgeRenderer;
Sunny Goyal179249d2017-12-19 16:49:24 -080084 private int mBadgeColor;
Tony Wickham1237df02017-02-24 08:59:36 -080085 private float mBadgeScale;
86 private boolean mForceHideBadge;
87 private Point mTempSpaceForBadgeOffset = new Point();
88 private Rect mTempIconBounds = new Rect();
89
90 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
91 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
92 @Override
93 public Float get(BubbleTextView bubbleTextView) {
94 return bubbleTextView.mBadgeScale;
95 }
96
97 @Override
98 public void set(BubbleTextView bubbleTextView, Float value) {
99 bubbleTextView.mBadgeScale = value;
100 bubbleTextView.invalidate();
101 }
102 };
103
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700104 public static final Property<BubbleTextView, Integer> TEXT_ALPHA_PROPERTY
Tony8f402802017-06-16 17:24:54 -0700105 = new Property<BubbleTextView, Integer>(Integer.class, "textAlpha") {
106 @Override
107 public Integer get(BubbleTextView bubbleTextView) {
108 return bubbleTextView.getTextAlpha();
109 }
110
111 @Override
112 public void set(BubbleTextView bubbleTextView, Integer alpha) {
113 bubbleTextView.setTextAlpha(alpha);
114 }
115 };
116
Sunny Goyal4ffec482016-02-09 11:28:52 -0800117 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800118 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800119 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700120 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800121 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700122 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500123
Sunny Goyal34b65272015-03-11 16:56:52 -0700124 private IconLoadRequest mIconLoadRequest;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700127 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 }
129
130 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700131 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 }
133
134 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
135 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700136 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700137 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700138 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700139
Adam Cohen96bb7982014-07-07 11:58:56 -0700140 TypedArray a = context.obtainStyledAttributes(attrs,
141 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800142 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700143
144 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
145 int defaultIconSize = grid.iconSizePx;
146 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700147 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Jon Miranda09660722017-06-14 14:20:14 -0700148 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700149 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700150 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
151 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700152 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700153 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700154 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700155 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700156 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700157 }
Winson1f064272016-07-18 17:18:02 -0700158 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700159
160 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
161 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700162 a.recycle();
163
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800164 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700165 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800166
Sunny Goyalae502842016-06-17 08:43:56 -0700167 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700168
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 }
170
Jon Miranda52549442017-10-26 11:28:06 -0700171 /**
172 * Resets the view so it can be recycled.
173 */
174 public void reset() {
175 mBadgeInfo = null;
Sunny Goyal179249d2017-12-19 16:49:24 -0800176 mBadgeColor = Color.TRANSPARENT;
Jon Miranda48043ee2017-11-01 15:40:22 -0700177 mBadgeScale = 0f;
Jon Miranda52549442017-10-26 11:28:06 -0700178 mForceHideBadge = false;
179 }
180
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800181 public void applyFromShortcutInfo(ShortcutInfo info) {
182 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700183 }
184
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800185 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800186 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800187 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700188 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800189 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500190 }
Tony Wickham010d2552017-01-20 08:15:28 -0800191
Tony Wickham1e618492017-02-02 12:57:18 -0800192 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800193 }
194
Sunny Goyal508da152014-08-14 10:53:27 -0700195 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800196 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700197
Winson Chung888b3a12015-03-13 11:14:16 -0700198 // We don't need to check the info since it's not a ShortcutInfo
199 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700200
201 // Verify high res immediately
202 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800203
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700204 if (info instanceof PromiseAppInfo) {
205 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
206 applyProgressLevel(promiseAppInfo.level);
207 }
Tony Wickham1e618492017-02-02 12:57:18 -0800208 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700209 }
210
Sunny Goyal0e08f162015-05-12 11:32:39 -0700211 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800212 applyIconAndLabel(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700213 // We don't need to check the info since it's not a ShortcutInfo
214 super.setTag(info);
215
216 // Verify high res immediately
217 verifyHighRes();
218 }
219
Sunny Goyal179249d2017-12-19 16:49:24 -0800220 private void applyIconAndLabel(ItemInfoWithIcon info) {
221 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(info);
222 mBadgeColor = IconPalette.getMutedColor(info.iconColor, 0.54f);
223
Sunny Goyalf4204382016-07-13 10:46:07 -0700224 setIcon(iconDrawable);
225 setText(info.title);
226 if (info.contentDescription != null) {
227 setContentDescription(info.isDisabled()
228 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
229 : info.contentDescription);
230 }
231 }
232
Winson Chung7501adf2015-06-02 11:24:28 -0700233 /**
234 * Overrides the default long press timeout.
235 */
236 public void setLongPressTimeout(int longPressTimeout) {
237 mLongPressHelper.setLongPressTimeout(longPressTimeout);
238 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700241 public void setTag(Object tag) {
242 if (tag != null) {
243 LauncherModel.checkItemInfo((ItemInfo) tag);
244 }
245 super.setTag(tag);
246 }
247
248 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800249 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700250 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800251 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800252 }
Sunny Goyal508da152014-08-14 10:53:27 -0700253 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800254
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800255 @Override
256 protected int[] onCreateDrawableState(int extraSpace) {
257 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
258 if (mStayPressed) {
259 mergeDrawableStates(drawableState, STATE_PRESSED);
260 }
261 return drawableState;
262 }
263
Winson Chungb745afb2015-03-02 11:51:23 -0800264 /** Returns the icon for this view. */
265 public Drawable getIcon() {
266 return mIcon;
267 }
268
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700269 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800270 public boolean onTouchEvent(MotionEvent event) {
271 // Call the superclass onTouchEvent first, because sometimes it changes the state to
272 // isPressed() on an ACTION_UP
273 boolean result = super.onTouchEvent(event);
274
Mady Melloref044dd2015-06-02 15:35:07 -0700275 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700276 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700277 mLongPressHelper.cancelLongPress();
278 result = true;
279 }
280
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800281 switch (event.getAction()) {
282 case MotionEvent.ACTION_DOWN:
Mady Melloref044dd2015-06-02 15:35:07 -0700283 // If we're in a stylus button press, don't check for long press.
284 if (!mStylusEventHelper.inStylusButtonPressed()) {
285 mLongPressHelper.postCheckForLongPress();
286 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800287 break;
288 case MotionEvent.ACTION_CANCEL:
289 case MotionEvent.ACTION_UP:
Winson Chung88f33452012-02-23 15:23:44 -0800290 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800291 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400292 case MotionEvent.ACTION_MOVE:
293 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
294 mLongPressHelper.cancelLongPress();
295 }
296 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800297 }
298 return result;
299 }
300
Michael Jurkaddd62e92011-02-16 17:49:14 -0800301 void setStayPressed(boolean stayPressed) {
302 mStayPressed = stayPressed;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800303 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800304 }
305
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700306 @Override
307 public void onLauncherResume() {
308 // Reset the pressed state of icon that was locked in the press state while activity
309 // was launching
310 setStayPressed(false);
311 }
312
Sunny Goyal508da152014-08-14 10:53:27 -0700313 void clearPressedBackground() {
314 setPressed(false);
315 setStayPressed(false);
316 }
317
318 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700319 public boolean onKeyUp(int keyCode, KeyEvent event) {
320 // Unlike touch events, keypress event propagate pressed state change immediately,
321 // without waiting for onClickHandler to execute. Disable pressed state changes here
322 // to avoid flickering.
323 mIgnorePressedStateChange = true;
324 boolean result = super.onKeyUp(keyCode, event);
Sunny Goyal508da152014-08-14 10:53:27 -0700325 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800326 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700327 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800328 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800329
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700330 @SuppressWarnings("wrongcall")
331 protected void drawWithoutBadge(Canvas canvas) {
332 super.onDraw(canvas);
333 }
334
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700336 public void onDraw(Canvas canvas) {
337 super.onDraw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800338 drawBadgeIfNecessary(canvas);
339 }
340
341 /**
342 * Draws the icon badge in the top right corner of the icon bounds.
343 * @param canvas The canvas to draw to.
344 */
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700345 protected void drawBadgeIfNecessary(Canvas canvas) {
Tony Wickham1237df02017-02-24 08:59:36 -0800346 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
347 getIconBounds(mTempIconBounds);
348 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
349 final int scrollX = getScrollX();
350 final int scrollY = getScrollY();
351 canvas.translate(scrollX, scrollY);
Sunny Goyal179249d2017-12-19 16:49:24 -0800352 mBadgeRenderer.draw(canvas, mBadgeColor, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800353 mTempSpaceForBadgeOffset);
354 canvas.translate(-scrollX, -scrollY);
355 }
356 }
357
358 public void forceHideBadge(boolean forceHideBadge) {
359 if (mForceHideBadge == forceHideBadge) {
360 return;
361 }
362 mForceHideBadge = forceHideBadge;
363
364 if (forceHideBadge) {
365 invalidate();
366 } else if (hasBadge()) {
367 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
368 }
369 }
370
371 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700372 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800373 }
374
375 public void getIconBounds(Rect outBounds) {
376 int top = getPaddingTop();
377 int left = (getWidth() - mIconSize) / 2;
378 int right = left + mIconSize;
379 int bottom = top + mIconSize;
380 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400382
383 @Override
Winson1f064272016-07-18 17:18:02 -0700384 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
385 if (mCenterVertically) {
386 Paint.FontMetrics fm = getPaint().getFontMetrics();
387 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
388 (int) Math.ceil(fm.bottom - fm.top);
389 int height = MeasureSpec.getSize(heightMeasureSpec);
390 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
391 getPaddingBottom());
392 }
393 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
394 }
395
396 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700397 public void setTextColor(int color) {
398 mTextColor = color;
399 super.setTextColor(color);
400 }
401
Adam Cohen96bb7982014-07-07 11:58:56 -0700402 @Override
403 public void setTextColor(ColorStateList colors) {
404 mTextColor = colors.getDefaultColor();
405 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700406 }
407
Tony7308cde2017-06-27 22:38:33 -0700408 public boolean shouldTextBeVisible() {
409 // Text should be visible everywhere but the hotseat.
410 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
411 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
412 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
413 }
414
Winson Chung5f8afe62013-08-12 16:19:28 -0700415 public void setTextVisibility(boolean visible) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700416 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700417 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700418 } else {
Tony8f402802017-06-16 17:24:54 -0700419 setTextAlpha(0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700420 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700421 }
422
Tony8f402802017-06-16 17:24:54 -0700423 private void setTextAlpha(int alpha) {
424 super.setTextColor(ColorUtils.setAlphaComponent(mTextColor, alpha));
425 }
426
427 private int getTextAlpha() {
428 return Color.alpha(getCurrentTextColor());
429 }
430
431 /**
432 * Creates an animator to fade the text in or out.
433 * @param fadeIn Whether the text should fade in or fade out.
434 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700435 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Tony7308cde2017-06-27 22:38:33 -0700436 int toAlpha = shouldTextBeVisible() && fadeIn ? Color.alpha(mTextColor) : 0;
437 return ObjectAnimator.ofInt(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700438 }
439
Winson Chungaffd7b42010-08-20 15:11:56 -0700440 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800441 public void cancelLongPress() {
442 super.cancelLongPress();
443
444 mLongPressHelper.cancelLongPress();
445 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500446
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800447 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700448 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400449 ShortcutInfo info = (ShortcutInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700450 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700451 final int progressLevel = isPromise ?
452 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
453 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700454
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700455 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
456 if (preloadDrawable != null && promiseStateChanged) {
457 preloadDrawable.maybePerformFinishedAnimation();
458 }
459 }
460 }
461
462 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
463 if (getTag() instanceof ItemInfoWithIcon) {
464 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700465 if (progressLevel >= 100) {
466 setContentDescription(info.contentDescription != null
467 ? info.contentDescription : "");
468 } else if (progressLevel > 0) {
469 setContentDescription(getContext()
470 .getString(R.string.app_downloading_title, info.title,
471 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
472 } else {
473 setContentDescription(getContext()
474 .getString(R.string.app_waiting_download_title, info.title));
475 }
Winson Chungb745afb2015-03-02 11:51:23 -0800476 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700477 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800478 if (mIcon instanceof PreloadIconDrawable) {
479 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700480 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700481 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800482 preloadDrawable = DrawableFactory.get(getContext())
Sunny Goyal179249d2017-12-19 16:49:24 -0800483 .newPendingIcon(info, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700484 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700485 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700486 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700487 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700488 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400489 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700490 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400491 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700492
Tony Wickham1e618492017-02-02 12:57:18 -0800493 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800494 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800495 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700496 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
497 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800498 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700499 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800500 if (wasBadged || isBadged) {
Tony Wickham1237df02017-02-24 08:59:36 -0800501 // Animate when a badge is first added or when it is removed.
502 if (animate && (wasBadged ^ isBadged) && isShown()) {
503 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
504 } else {
505 mBadgeScale = newBadgeScale;
506 invalidate();
507 }
508 }
Tony Wickham305e9202018-01-23 17:46:47 -0800509 if (itemInfo.contentDescription != null) {
510 if (hasBadge()) {
511 int count = mBadgeInfo.getNotificationCount();
512 setContentDescription(getContext().getResources().getQuantityString(
513 R.plurals.badged_app_label, count, itemInfo.contentDescription, count));
514 } else {
515 setContentDescription(itemInfo.contentDescription);
516 }
517 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800518 }
519 }
520
Winson Chungb745afb2015-03-02 11:51:23 -0800521 /**
522 * Sets the icon for this view based on the layout direction.
523 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700524 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800525 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800526 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700527 if (mIsIconVisible) {
528 applyCompoundDrawables(mIcon);
529 }
530 }
531
532 public void setIconVisible(boolean visible) {
533 mIsIconVisible = visible;
534 mDisableRelayout = true;
535 Drawable icon = mIcon;
536 if (!visible) {
537 icon = new ColorDrawable(Color.TRANSPARENT);
538 icon.setBounds(0, 0, mIconSize, mIconSize);
539 }
540 applyCompoundDrawables(icon);
541 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700542 }
543
544 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800545 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800546 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800547 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700548 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800549 }
Winson Chungb745afb2015-03-02 11:51:23 -0800550 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700551
Sunny Goyal69b75642015-05-15 17:00:24 -0700552 @Override
553 public void requestLayout() {
554 if (!mDisableRelayout) {
555 super.requestLayout();
556 }
557 }
558
Sunny Goyal34b65272015-03-11 16:56:52 -0700559 /**
560 * Applies the item info if it is same as what the view is pointing to currently.
561 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800562 @Override
563 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700564 if (getTag() == info) {
565 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700566 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700567
Jon Miranda7f522a22017-07-28 11:56:47 -0700568 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
569 info.iconBitmap.prepareToDraw();
570
Sunny Goyal34b65272015-03-11 16:56:52 -0700571 if (info instanceof AppInfo) {
572 applyFromApplicationInfo((AppInfo) info);
573 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800574 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800575 FolderIconPreviewVerifier verifier =
576 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
577 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700578 View folderIcon =
579 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
580 if (folderIcon != null) {
581 folderIcon.invalidate();
582 }
583 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700584 } else if (info instanceof PackageItemInfo) {
585 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700586 }
Winsonc0880492015-08-21 11:16:27 -0700587
Sunny Goyal69b75642015-05-15 17:00:24 -0700588 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700589 }
590 }
591
592 /**
593 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
594 */
595 public void verifyHighRes() {
596 if (mIconLoadRequest != null) {
597 mIconLoadRequest.cancel();
598 mIconLoadRequest = null;
599 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800600 if (getTag() instanceof ItemInfoWithIcon) {
601 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700602 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800603 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700604 .updateIconInBackground(BubbleTextView.this, info);
605 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700606 }
607 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700608
Jon Miranda47170112017-03-03 14:57:14 -0800609 public int getIconSize() {
610 return mIconSize;
611 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612}