blob: 46d7227081a7dccdc725572b0cbd507d012de170 [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;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080023import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070025import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070026import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080027import android.graphics.Point;
28import android.graphics.Rect;
Tony17b7f9b2017-05-23 12:19:09 -070029import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.drawable.Drawable;
Tony8f402802017-06-16 17:24:54 -070031import android.support.v4.graphics.ColorUtils;
Winson Chung656d11c2010-11-29 17:15:47 -080032import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080033import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070034import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070035import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080036import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070037import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040038import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080039import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070040import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080041import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070042
Sunny Goyal34b65272015-03-11 16:56:52 -070043import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080044import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
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;
Jon Miranda529af302017-02-28 14:18:54 -080049import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080050import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070051import com.android.launcher3.graphics.HolographicOutlineHelper;
Tony Wickham1237df02017-02-24 08:59:36 -080052import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080053import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070054import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070055
Sunny Goyalc469aad2015-10-01 11:24:23 -070056import java.text.NumberFormat;
57
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058/**
59 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
60 * because we want to make the bubble taller than the text and TextView's clip is
61 * too aggressive.
62 */
Sunny Goyal3dce5f32017-10-05 11:40:05 -070063public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback {
Sunny Goyal95abbb32014-08-04 10:53:22 -070064
Sunny Goyaldfaccf62015-05-11 16:30:44 -070065 private static final int DISPLAY_WORKSPACE = 0;
66 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070067 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070068
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080069 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
70
Sunny Goyal53d7ee42015-05-22 12:25:45 -070071 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080072 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070073 private final boolean mCenterVertically;
Sunny Goyal4e5a8782017-06-23 09:34:06 -070074
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075 private final CheckLongPressHelper mLongPressHelper;
76 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070077 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal9314b7c2017-06-23 10:36:27 -070078 private final float mSlop;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079
Sunny Goyal508da152014-08-14 10:53:27 -070080 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080081
Winson Chung93f98ea2015-03-10 16:28:47 -070082 private final boolean mDeferShadowGenerationOnTouch;
Winson Chungb745afb2015-03-02 11:51:23 -080083 private final boolean mLayoutHorizontal;
84 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080085 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080086 private int mTextColor;
Tony17b7f9b2017-05-23 12:19:09 -070087 private boolean mIsIconVisible = true;
Winson Chung5f8afe62013-08-12 16:19:28 -070088
Tony Wickham1237df02017-02-24 08:59:36 -080089 private BadgeInfo mBadgeInfo;
90 private BadgeRenderer mBadgeRenderer;
Sunny Goyal179249d2017-12-19 16:49:24 -080091 private int mBadgeColor;
Tony Wickham1237df02017-02-24 08:59:36 -080092 private float mBadgeScale;
93 private boolean mForceHideBadge;
94 private Point mTempSpaceForBadgeOffset = new Point();
95 private Rect mTempIconBounds = new Rect();
96
97 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
98 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
99 @Override
100 public Float get(BubbleTextView bubbleTextView) {
101 return bubbleTextView.mBadgeScale;
102 }
103
104 @Override
105 public void set(BubbleTextView bubbleTextView, Float value) {
106 bubbleTextView.mBadgeScale = value;
107 bubbleTextView.invalidate();
108 }
109 };
110
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700111 public static final Property<BubbleTextView, Integer> TEXT_ALPHA_PROPERTY
Tony8f402802017-06-16 17:24:54 -0700112 = new Property<BubbleTextView, Integer>(Integer.class, "textAlpha") {
113 @Override
114 public Integer get(BubbleTextView bubbleTextView) {
115 return bubbleTextView.getTextAlpha();
116 }
117
118 @Override
119 public void set(BubbleTextView bubbleTextView, Integer alpha) {
120 bubbleTextView.setTextAlpha(alpha);
121 }
122 };
123
Sunny Goyal4ffec482016-02-09 11:28:52 -0800124 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800125 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800126 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700127 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800128 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700129 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500130
Sunny Goyal34b65272015-03-11 16:56:52 -0700131 private IconLoadRequest mIconLoadRequest;
132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700134 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 }
136
137 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700138 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 }
140
141 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
142 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700143 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700144 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700145 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Adam Cohen96bb7982014-07-07 11:58:56 -0700146
Adam Cohen96bb7982014-07-07 11:58:56 -0700147 TypedArray a = context.obtainStyledAttributes(attrs,
148 R.styleable.BubbleTextView, defStyle, 0);
Winson Chungb745afb2015-03-02 11:51:23 -0800149 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700150 mDeferShadowGenerationOnTouch =
151 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700152
153 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
154 int defaultIconSize = grid.iconSizePx;
155 if (display == DISPLAY_WORKSPACE) {
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 Goyaldfaccf62015-05-11 16:30:44 -0700158 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700159 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
160 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700161 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700162 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700163 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700164 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700165 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700166 }
Winson1f064272016-07-18 17:18:02 -0700167 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700168
169 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
170 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700171 a.recycle();
172
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800173 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700174 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800175
Sunny Goyal10629b02016-09-01 12:50:11 -0700176 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700177 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 }
180
Jon Miranda52549442017-10-26 11:28:06 -0700181 /**
182 * Resets the view so it can be recycled.
183 */
184 public void reset() {
185 mBadgeInfo = null;
Sunny Goyal179249d2017-12-19 16:49:24 -0800186 mBadgeColor = Color.TRANSPARENT;
Jon Miranda48043ee2017-11-01 15:40:22 -0700187 mBadgeScale = 0f;
Jon Miranda52549442017-10-26 11:28:06 -0700188 mForceHideBadge = false;
189 }
190
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800191 public void applyFromShortcutInfo(ShortcutInfo info) {
192 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700193 }
194
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800195 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800196 applyIconAndLabel(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800197 setTag(info);
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700198 if (promiseStateChanged || (info.hasPromiseIconUi())) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800199 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500200 }
Tony Wickham010d2552017-01-20 08:15:28 -0800201
Tony Wickham1e618492017-02-02 12:57:18 -0800202 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800203 }
204
Sunny Goyal508da152014-08-14 10:53:27 -0700205 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800206 applyIconAndLabel(info);
Sunny Goyalf4204382016-07-13 10:46:07 -0700207
Winson Chung888b3a12015-03-13 11:14:16 -0700208 // We don't need to check the info since it's not a ShortcutInfo
209 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700210
211 // Verify high res immediately
212 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800213
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700214 if (info instanceof PromiseAppInfo) {
215 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
216 applyProgressLevel(promiseAppInfo.level);
217 }
Tony Wickham1e618492017-02-02 12:57:18 -0800218 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700219 }
220
Sunny Goyal0e08f162015-05-12 11:32:39 -0700221 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal179249d2017-12-19 16:49:24 -0800222 applyIconAndLabel(info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700223 // We don't need to check the info since it's not a ShortcutInfo
224 super.setTag(info);
225
226 // Verify high res immediately
227 verifyHighRes();
228 }
229
Sunny Goyal179249d2017-12-19 16:49:24 -0800230 private void applyIconAndLabel(ItemInfoWithIcon info) {
231 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(info);
232 mBadgeColor = IconPalette.getMutedColor(info.iconColor, 0.54f);
233
Tony Wickham6b910a22016-11-08 10:40:34 -0800234 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700235 setIcon(iconDrawable);
236 setText(info.title);
237 if (info.contentDescription != null) {
238 setContentDescription(info.isDisabled()
239 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
240 : info.contentDescription);
241 }
242 }
243
Winson Chung7501adf2015-06-02 11:24:28 -0700244 /**
245 * Overrides the default long press timeout.
246 */
247 public void setLongPressTimeout(int longPressTimeout) {
248 mLongPressHelper.setLongPressTimeout(longPressTimeout);
249 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700252 public void setTag(Object tag) {
253 if (tag != null) {
254 LauncherModel.checkItemInfo((ItemInfo) tag);
255 }
256 super.setTag(tag);
257 }
258
259 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800260 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700261 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800262 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800263 }
Sunny Goyal508da152014-08-14 10:53:27 -0700264 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800265
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800266 @Override
267 protected int[] onCreateDrawableState(int extraSpace) {
268 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
269 if (mStayPressed) {
270 mergeDrawableStates(drawableState, STATE_PRESSED);
271 }
272 return drawableState;
273 }
274
Winson Chungb745afb2015-03-02 11:51:23 -0800275 /** Returns the icon for this view. */
276 public Drawable getIcon() {
277 return mIcon;
278 }
279
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700280 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800281 public boolean onTouchEvent(MotionEvent event) {
282 // Call the superclass onTouchEvent first, because sometimes it changes the state to
283 // isPressed() on an ACTION_UP
284 boolean result = super.onTouchEvent(event);
285
Mady Melloref044dd2015-06-02 15:35:07 -0700286 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700287 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700288 mLongPressHelper.cancelLongPress();
289 result = true;
290 }
291
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800292 switch (event.getAction()) {
293 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700294 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800295 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
296 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700297 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700298 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800299 }
Winson Chung88f33452012-02-23 15:23:44 -0800300
Mady Melloref044dd2015-06-02 15:35:07 -0700301 // If we're in a stylus button press, don't check for long press.
302 if (!mStylusEventHelper.inStylusButtonPressed()) {
303 mLongPressHelper.postCheckForLongPress();
304 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800305 break;
306 case MotionEvent.ACTION_CANCEL:
307 case MotionEvent.ACTION_UP:
308 // If we've touched down and up on an item, and it's still not "pressed", then
309 // destroy the pressed outline
310 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700311 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800312 }
Winson Chung88f33452012-02-23 15:23:44 -0800313
314 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800315 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400316 case MotionEvent.ACTION_MOVE:
317 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
318 mLongPressHelper.cancelLongPress();
319 }
320 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800321 }
322 return result;
323 }
324
Michael Jurkaddd62e92011-02-16 17:49:14 -0800325 void setStayPressed(boolean stayPressed) {
326 mStayPressed = stayPressed;
327 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700328 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700329 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700330 } else {
331 if (mPressedBackground == null) {
332 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
333 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800334 }
Sunny Goyal508da152014-08-14 10:53:27 -0700335
336 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700337 ViewParent parent = getParent();
338 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
339 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
340 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700341 }
342
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
359 public boolean onKeyDown(int keyCode, KeyEvent event) {
360 if (super.onKeyDown(keyCode, event)) {
361 // Pre-create shadow so show immediately on click.
362 if (mPressedBackground == null) {
363 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700364 }
Sunny Goyal508da152014-08-14 10:53:27 -0700365 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700366 }
Sunny Goyal508da152014-08-14 10:53:27 -0700367 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800368 }
369
Sunny Goyal508da152014-08-14 10:53:27 -0700370 @Override
371 public boolean onKeyUp(int keyCode, KeyEvent event) {
372 // Unlike touch events, keypress event propagate pressed state change immediately,
373 // without waiting for onClickHandler to execute. Disable pressed state changes here
374 // to avoid flickering.
375 mIgnorePressedStateChange = true;
376 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700377
Sunny Goyal508da152014-08-14 10:53:27 -0700378 mPressedBackground = null;
379 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800380 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700381 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800382 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800383
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700384 @SuppressWarnings("wrongcall")
385 protected void drawWithoutBadge(Canvas canvas) {
386 super.onDraw(canvas);
387 }
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700390 public void onDraw(Canvas canvas) {
391 super.onDraw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800392 drawBadgeIfNecessary(canvas);
393 }
394
395 /**
396 * Draws the icon badge in the top right corner of the icon bounds.
397 * @param canvas The canvas to draw to.
398 */
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700399 protected void drawBadgeIfNecessary(Canvas canvas) {
Tony Wickham1237df02017-02-24 08:59:36 -0800400 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
401 getIconBounds(mTempIconBounds);
402 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
403 final int scrollX = getScrollX();
404 final int scrollY = getScrollY();
405 canvas.translate(scrollX, scrollY);
Sunny Goyal179249d2017-12-19 16:49:24 -0800406 mBadgeRenderer.draw(canvas, mBadgeColor, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800407 mTempSpaceForBadgeOffset);
408 canvas.translate(-scrollX, -scrollY);
409 }
410 }
411
412 public void forceHideBadge(boolean forceHideBadge) {
413 if (mForceHideBadge == forceHideBadge) {
414 return;
415 }
416 mForceHideBadge = forceHideBadge;
417
418 if (forceHideBadge) {
419 invalidate();
420 } else if (hasBadge()) {
421 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
422 }
423 }
424
425 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700426 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800427 }
428
429 public void getIconBounds(Rect outBounds) {
430 int top = getPaddingTop();
431 int left = (getWidth() - mIconSize) / 2;
432 int right = left + mIconSize;
433 int bottom = top + mIconSize;
434 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400436
437 @Override
Winson1f064272016-07-18 17:18:02 -0700438 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
439 if (mCenterVertically) {
440 Paint.FontMetrics fm = getPaint().getFontMetrics();
441 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
442 (int) Math.ceil(fm.bottom - fm.top);
443 int height = MeasureSpec.getSize(heightMeasureSpec);
444 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
445 getPaddingBottom());
446 }
447 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
448 }
449
450 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700451 public void setTextColor(int color) {
452 mTextColor = color;
453 super.setTextColor(color);
454 }
455
Adam Cohen96bb7982014-07-07 11:58:56 -0700456 @Override
457 public void setTextColor(ColorStateList colors) {
458 mTextColor = colors.getDefaultColor();
459 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700460 }
461
Tony7308cde2017-06-27 22:38:33 -0700462 public boolean shouldTextBeVisible() {
463 // Text should be visible everywhere but the hotseat.
464 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
465 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
466 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
467 }
468
Winson Chung5f8afe62013-08-12 16:19:28 -0700469 public void setTextVisibility(boolean visible) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700470 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700471 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700472 } else {
Tony8f402802017-06-16 17:24:54 -0700473 setTextAlpha(0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700474 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700475 }
476
Tony8f402802017-06-16 17:24:54 -0700477 private void setTextAlpha(int alpha) {
478 super.setTextColor(ColorUtils.setAlphaComponent(mTextColor, alpha));
479 }
480
481 private int getTextAlpha() {
482 return Color.alpha(getCurrentTextColor());
483 }
484
485 /**
486 * Creates an animator to fade the text in or out.
487 * @param fadeIn Whether the text should fade in or fade out.
488 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700489 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Tony7308cde2017-06-27 22:38:33 -0700490 int toAlpha = shouldTextBeVisible() && fadeIn ? Color.alpha(mTextColor) : 0;
491 return ObjectAnimator.ofInt(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700492 }
493
Winson Chungaffd7b42010-08-20 15:11:56 -0700494 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800495 public void cancelLongPress() {
496 super.cancelLongPress();
497
498 mLongPressHelper.cancelLongPress();
499 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500500
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800501 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700502 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400503 ShortcutInfo info = (ShortcutInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700504 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700505 final int progressLevel = isPromise ?
506 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
507 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700508
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700509 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
510 if (preloadDrawable != null && promiseStateChanged) {
511 preloadDrawable.maybePerformFinishedAnimation();
512 }
513 }
514 }
515
516 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
517 if (getTag() instanceof ItemInfoWithIcon) {
518 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700519 if (progressLevel >= 100) {
520 setContentDescription(info.contentDescription != null
521 ? info.contentDescription : "");
522 } else if (progressLevel > 0) {
523 setContentDescription(getContext()
524 .getString(R.string.app_downloading_title, info.title,
525 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
526 } else {
527 setContentDescription(getContext()
528 .getString(R.string.app_waiting_download_title, info.title));
529 }
Winson Chungb745afb2015-03-02 11:51:23 -0800530 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700531 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800532 if (mIcon instanceof PreloadIconDrawable) {
533 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700534 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700535 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800536 preloadDrawable = DrawableFactory.get(getContext())
Sunny Goyal179249d2017-12-19 16:49:24 -0800537 .newPendingIcon(info, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700538 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700539 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700540 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700541 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700542 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400543 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700544 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400545 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700546
Tony Wickham1e618492017-02-02 12:57:18 -0800547 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800548 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800549 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700550 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
551 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800552 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700553 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800554 if (wasBadged || isBadged) {
Tony Wickham1237df02017-02-24 08:59:36 -0800555 // Animate when a badge is first added or when it is removed.
556 if (animate && (wasBadged ^ isBadged) && isShown()) {
557 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
558 } else {
559 mBadgeScale = newBadgeScale;
560 invalidate();
561 }
562 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800563 }
564 }
565
Winson Chungb745afb2015-03-02 11:51:23 -0800566 /**
567 * Sets the icon for this view based on the layout direction.
568 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700569 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800570 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800571 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700572 if (mIsIconVisible) {
573 applyCompoundDrawables(mIcon);
574 }
575 }
576
577 public void setIconVisible(boolean visible) {
578 mIsIconVisible = visible;
579 mDisableRelayout = true;
580 Drawable icon = mIcon;
581 if (!visible) {
582 icon = new ColorDrawable(Color.TRANSPARENT);
583 icon.setBounds(0, 0, mIconSize, mIconSize);
584 }
585 applyCompoundDrawables(icon);
586 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700587 }
588
589 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800590 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800591 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800592 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700593 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800594 }
Winson Chungb745afb2015-03-02 11:51:23 -0800595 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700596
Sunny Goyal69b75642015-05-15 17:00:24 -0700597 @Override
598 public void requestLayout() {
599 if (!mDisableRelayout) {
600 super.requestLayout();
601 }
602 }
603
Sunny Goyal34b65272015-03-11 16:56:52 -0700604 /**
605 * Applies the item info if it is same as what the view is pointing to currently.
606 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800607 @Override
608 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700609 if (getTag() == info) {
610 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700611 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700612
Jon Miranda7f522a22017-07-28 11:56:47 -0700613 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
614 info.iconBitmap.prepareToDraw();
615
Sunny Goyal34b65272015-03-11 16:56:52 -0700616 if (info instanceof AppInfo) {
617 applyFromApplicationInfo((AppInfo) info);
618 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800619 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800620 FolderIconPreviewVerifier verifier =
621 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
622 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700623 View folderIcon =
624 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
625 if (folderIcon != null) {
626 folderIcon.invalidate();
627 }
628 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700629 } else if (info instanceof PackageItemInfo) {
630 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700631 }
Winsonc0880492015-08-21 11:16:27 -0700632
Sunny Goyal69b75642015-05-15 17:00:24 -0700633 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700634 }
635 }
636
637 /**
638 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
639 */
640 public void verifyHighRes() {
641 if (mIconLoadRequest != null) {
642 mIconLoadRequest.cancel();
643 mIconLoadRequest = null;
644 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800645 if (getTag() instanceof ItemInfoWithIcon) {
646 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700647 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800648 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700649 .updateIconInBackground(BubbleTextView.this, info);
650 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700651 }
652 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700653
Jon Miranda47170112017-03-03 14:57:14 -0800654 public int getIconSize() {
655 return mIconSize;
656 }
657
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700658 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700659 * Interface to be implemented by the grand parent to allow click shadow effect.
660 */
Winsonc0880492015-08-21 11:16:27 -0700661 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700662 void setPressedIcon(BubbleTextView icon, Bitmap background);
663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664}