blob: cd72fbace74d91b23288170dfa93f4e5afaefbae [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;
Tony Wickham7092db02017-06-07 14:32:23 -070091 private IconPalette mBadgePalette;
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
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) {
186 applyIconAndLabel(info.iconBitmap, 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 Goyalf4204382016-07-13 10:46:07 -0700196 applyIconAndLabel(info.iconBitmap, info);
197
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 Goyalf4204382016-07-13 10:46:07 -0700212 applyIconAndLabel(info.iconBitmap, 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 Goyalf4204382016-07-13 10:46:07 -0700220 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800221 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800222 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700223 setIcon(iconDrawable);
224 setText(info.title);
225 if (info.contentDescription != null) {
226 setContentDescription(info.isDisabled()
227 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
228 : info.contentDescription);
229 }
230 }
231
Winson Chung7501adf2015-06-02 11:24:28 -0700232 /**
233 * Overrides the default long press timeout.
234 */
235 public void setLongPressTimeout(int longPressTimeout) {
236 mLongPressHelper.setLongPressTimeout(longPressTimeout);
237 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700240 public void setTag(Object tag) {
241 if (tag != null) {
242 LauncherModel.checkItemInfo((ItemInfo) tag);
243 }
244 super.setTag(tag);
245 }
246
247 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800248 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700249 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800250 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800251 }
Sunny Goyal508da152014-08-14 10:53:27 -0700252 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800253
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800254 @Override
255 protected int[] onCreateDrawableState(int extraSpace) {
256 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
257 if (mStayPressed) {
258 mergeDrawableStates(drawableState, STATE_PRESSED);
259 }
260 return drawableState;
261 }
262
Winson Chungb745afb2015-03-02 11:51:23 -0800263 /** Returns the icon for this view. */
264 public Drawable getIcon() {
265 return mIcon;
266 }
267
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700268 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800269 public boolean onTouchEvent(MotionEvent event) {
270 // Call the superclass onTouchEvent first, because sometimes it changes the state to
271 // isPressed() on an ACTION_UP
272 boolean result = super.onTouchEvent(event);
273
Mady Melloref044dd2015-06-02 15:35:07 -0700274 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700275 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700276 mLongPressHelper.cancelLongPress();
277 result = true;
278 }
279
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800280 switch (event.getAction()) {
281 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700282 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800283 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
284 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700285 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700286 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800287 }
Winson Chung88f33452012-02-23 15:23:44 -0800288
Mady Melloref044dd2015-06-02 15:35:07 -0700289 // If we're in a stylus button press, don't check for long press.
290 if (!mStylusEventHelper.inStylusButtonPressed()) {
291 mLongPressHelper.postCheckForLongPress();
292 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800293 break;
294 case MotionEvent.ACTION_CANCEL:
295 case MotionEvent.ACTION_UP:
296 // If we've touched down and up on an item, and it's still not "pressed", then
297 // destroy the pressed outline
298 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700299 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800300 }
Winson Chung88f33452012-02-23 15:23:44 -0800301
302 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800303 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400304 case MotionEvent.ACTION_MOVE:
305 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
306 mLongPressHelper.cancelLongPress();
307 }
308 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800309 }
310 return result;
311 }
312
Michael Jurkaddd62e92011-02-16 17:49:14 -0800313 void setStayPressed(boolean stayPressed) {
314 mStayPressed = stayPressed;
315 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700316 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700317 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700318 } else {
319 if (mPressedBackground == null) {
320 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
321 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800322 }
Sunny Goyal508da152014-08-14 10:53:27 -0700323
324 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700325 ViewParent parent = getParent();
326 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
327 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
328 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700329 }
330
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800331 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800332 }
333
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700334 @Override
335 public void onLauncherResume() {
336 // Reset the pressed state of icon that was locked in the press state while activity
337 // was launching
338 setStayPressed(false);
339 }
340
Sunny Goyal508da152014-08-14 10:53:27 -0700341 void clearPressedBackground() {
342 setPressed(false);
343 setStayPressed(false);
344 }
345
346 @Override
347 public boolean onKeyDown(int keyCode, KeyEvent event) {
348 if (super.onKeyDown(keyCode, event)) {
349 // Pre-create shadow so show immediately on click.
350 if (mPressedBackground == null) {
351 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700352 }
Sunny Goyal508da152014-08-14 10:53:27 -0700353 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700354 }
Sunny Goyal508da152014-08-14 10:53:27 -0700355 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800356 }
357
Sunny Goyal508da152014-08-14 10:53:27 -0700358 @Override
359 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);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700365
Sunny Goyal508da152014-08-14 10:53:27 -0700366 mPressedBackground = null;
367 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800368 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700369 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800370 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800371
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700372 @SuppressWarnings("wrongcall")
373 protected void drawWithoutBadge(Canvas canvas) {
374 super.onDraw(canvas);
375 }
376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700378 public void onDraw(Canvas canvas) {
379 super.onDraw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800380 drawBadgeIfNecessary(canvas);
381 }
382
383 /**
384 * Draws the icon badge in the top right corner of the icon bounds.
385 * @param canvas The canvas to draw to.
386 */
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700387 protected void drawBadgeIfNecessary(Canvas canvas) {
Tony Wickham1237df02017-02-24 08:59:36 -0800388 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
389 getIconBounds(mTempIconBounds);
390 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
391 final int scrollX = getScrollX();
392 final int scrollY = getScrollY();
393 canvas.translate(scrollX, scrollY);
Tony Wickham7092db02017-06-07 14:32:23 -0700394 mBadgeRenderer.draw(canvas, mBadgePalette, mBadgeInfo, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800395 mTempSpaceForBadgeOffset);
396 canvas.translate(-scrollX, -scrollY);
397 }
398 }
399
400 public void forceHideBadge(boolean forceHideBadge) {
401 if (mForceHideBadge == forceHideBadge) {
402 return;
403 }
404 mForceHideBadge = forceHideBadge;
405
406 if (forceHideBadge) {
407 invalidate();
408 } else if (hasBadge()) {
409 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
410 }
411 }
412
413 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700414 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800415 }
416
417 public void getIconBounds(Rect outBounds) {
418 int top = getPaddingTop();
419 int left = (getWidth() - mIconSize) / 2;
420 int right = left + mIconSize;
421 int bottom = top + mIconSize;
422 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400424
425 @Override
Winson1f064272016-07-18 17:18:02 -0700426 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
427 if (mCenterVertically) {
428 Paint.FontMetrics fm = getPaint().getFontMetrics();
429 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
430 (int) Math.ceil(fm.bottom - fm.top);
431 int height = MeasureSpec.getSize(heightMeasureSpec);
432 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
433 getPaddingBottom());
434 }
435 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
436 }
437
438 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700439 public void setTextColor(int color) {
440 mTextColor = color;
441 super.setTextColor(color);
442 }
443
Adam Cohen96bb7982014-07-07 11:58:56 -0700444 @Override
445 public void setTextColor(ColorStateList colors) {
446 mTextColor = colors.getDefaultColor();
447 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700448 }
449
Tony7308cde2017-06-27 22:38:33 -0700450 public boolean shouldTextBeVisible() {
451 // Text should be visible everywhere but the hotseat.
452 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
453 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
454 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
455 }
456
Winson Chung5f8afe62013-08-12 16:19:28 -0700457 public void setTextVisibility(boolean visible) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700458 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700459 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700460 } else {
Tony8f402802017-06-16 17:24:54 -0700461 setTextAlpha(0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700462 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700463 }
464
Tony8f402802017-06-16 17:24:54 -0700465 private void setTextAlpha(int alpha) {
466 super.setTextColor(ColorUtils.setAlphaComponent(mTextColor, alpha));
467 }
468
469 private int getTextAlpha() {
470 return Color.alpha(getCurrentTextColor());
471 }
472
473 /**
474 * Creates an animator to fade the text in or out.
475 * @param fadeIn Whether the text should fade in or fade out.
476 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700477 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Tony7308cde2017-06-27 22:38:33 -0700478 int toAlpha = shouldTextBeVisible() && fadeIn ? Color.alpha(mTextColor) : 0;
479 return ObjectAnimator.ofInt(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700480 }
481
Winson Chungaffd7b42010-08-20 15:11:56 -0700482 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800483 public void cancelLongPress() {
484 super.cancelLongPress();
485
486 mLongPressHelper.cancelLongPress();
487 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500488
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800489 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700490 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400491 ShortcutInfo info = (ShortcutInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700492 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700493 final int progressLevel = isPromise ?
494 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
495 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700496
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700497 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
498 if (preloadDrawable != null && promiseStateChanged) {
499 preloadDrawable.maybePerformFinishedAnimation();
500 }
501 }
502 }
503
504 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
505 if (getTag() instanceof ItemInfoWithIcon) {
506 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700507 if (progressLevel >= 100) {
508 setContentDescription(info.contentDescription != null
509 ? info.contentDescription : "");
510 } else if (progressLevel > 0) {
511 setContentDescription(getContext()
512 .getString(R.string.app_downloading_title, info.title,
513 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
514 } else {
515 setContentDescription(getContext()
516 .getString(R.string.app_waiting_download_title, info.title));
517 }
Winson Chungb745afb2015-03-02 11:51:23 -0800518 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700519 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800520 if (mIcon instanceof PreloadIconDrawable) {
521 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700522 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700523 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800524 preloadDrawable = DrawableFactory.get(getContext())
525 .newPendingIcon(info.iconBitmap, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700526 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700527 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700528 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700529 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700530 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400531 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700532 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400533 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700534
Tony Wickham1e618492017-02-02 12:57:18 -0800535 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800536 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800537 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700538 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
539 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800540 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700541 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800542 if (wasBadged || isBadged) {
Tony Wickham7092db02017-06-07 14:32:23 -0700543 mBadgePalette = IconPalette.getBadgePalette(getResources());
544 if (mBadgePalette == null) {
545 mBadgePalette = ((FastBitmapDrawable) mIcon).getIconPalette();
546 }
Tony Wickham1237df02017-02-24 08:59:36 -0800547 // Animate when a badge is first added or when it is removed.
548 if (animate && (wasBadged ^ isBadged) && isShown()) {
549 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
550 } else {
551 mBadgeScale = newBadgeScale;
552 invalidate();
553 }
554 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800555 }
556 }
557
Tony Wickham7092db02017-06-07 14:32:23 -0700558 public IconPalette getBadgePalette() {
559 return mBadgePalette;
560 }
561
Winson Chungb745afb2015-03-02 11:51:23 -0800562 /**
563 * Sets the icon for this view based on the layout direction.
564 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700565 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800566 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800567 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700568 if (mIsIconVisible) {
569 applyCompoundDrawables(mIcon);
570 }
571 }
572
573 public void setIconVisible(boolean visible) {
574 mIsIconVisible = visible;
575 mDisableRelayout = true;
576 Drawable icon = mIcon;
577 if (!visible) {
578 icon = new ColorDrawable(Color.TRANSPARENT);
579 icon.setBounds(0, 0, mIconSize, mIconSize);
580 }
581 applyCompoundDrawables(icon);
582 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700583 }
584
585 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800586 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800587 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800588 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700589 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800590 }
Winson Chungb745afb2015-03-02 11:51:23 -0800591 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700592
Sunny Goyal69b75642015-05-15 17:00:24 -0700593 @Override
594 public void requestLayout() {
595 if (!mDisableRelayout) {
596 super.requestLayout();
597 }
598 }
599
Sunny Goyal34b65272015-03-11 16:56:52 -0700600 /**
601 * Applies the item info if it is same as what the view is pointing to currently.
602 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800603 @Override
604 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700605 if (getTag() == info) {
606 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700607 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700608
Jon Miranda7f522a22017-07-28 11:56:47 -0700609 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
610 info.iconBitmap.prepareToDraw();
611
Sunny Goyal34b65272015-03-11 16:56:52 -0700612 if (info instanceof AppInfo) {
613 applyFromApplicationInfo((AppInfo) info);
614 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800615 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800616 FolderIconPreviewVerifier verifier =
617 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
618 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700619 View folderIcon =
620 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
621 if (folderIcon != null) {
622 folderIcon.invalidate();
623 }
624 }
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 Goyal0e08f162015-05-12 11:32:39 -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 }
653
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700654 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700655 * Interface to be implemented by the grand parent to allow click shadow effect.
656 */
Winsonc0880492015-08-21 11:16:27 -0700657 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700658 void setPressedIcon(BubbleTextView icon, Bitmap background);
659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660}