blob: a590504033c7ca578afe25904fc2b86712923524 [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
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;
186 mBadgePalette = null;
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) {
196 applyIconAndLabel(info.iconBitmap, 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 Goyalf4204382016-07-13 10:46:07 -0700206 applyIconAndLabel(info.iconBitmap, info);
207
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 Goyalf4204382016-07-13 10:46:07 -0700222 applyIconAndLabel(info.iconBitmap, 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 Goyalf4204382016-07-13 10:46:07 -0700230 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800231 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800232 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700233 setIcon(iconDrawable);
234 setText(info.title);
235 if (info.contentDescription != null) {
236 setContentDescription(info.isDisabled()
237 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
238 : info.contentDescription);
239 }
240 }
241
Winson Chung7501adf2015-06-02 11:24:28 -0700242 /**
243 * Overrides the default long press timeout.
244 */
245 public void setLongPressTimeout(int longPressTimeout) {
246 mLongPressHelper.setLongPressTimeout(longPressTimeout);
247 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700250 public void setTag(Object tag) {
251 if (tag != null) {
252 LauncherModel.checkItemInfo((ItemInfo) tag);
253 }
254 super.setTag(tag);
255 }
256
257 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800258 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700259 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800260 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800261 }
Sunny Goyal508da152014-08-14 10:53:27 -0700262 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800263
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800264 @Override
265 protected int[] onCreateDrawableState(int extraSpace) {
266 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
267 if (mStayPressed) {
268 mergeDrawableStates(drawableState, STATE_PRESSED);
269 }
270 return drawableState;
271 }
272
Winson Chungb745afb2015-03-02 11:51:23 -0800273 /** Returns the icon for this view. */
274 public Drawable getIcon() {
275 return mIcon;
276 }
277
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700278 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800279 public boolean onTouchEvent(MotionEvent event) {
280 // Call the superclass onTouchEvent first, because sometimes it changes the state to
281 // isPressed() on an ACTION_UP
282 boolean result = super.onTouchEvent(event);
283
Mady Melloref044dd2015-06-02 15:35:07 -0700284 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700285 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700286 mLongPressHelper.cancelLongPress();
287 result = true;
288 }
289
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800290 switch (event.getAction()) {
291 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700292 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800293 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
294 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700295 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700296 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800297 }
Winson Chung88f33452012-02-23 15:23:44 -0800298
Mady Melloref044dd2015-06-02 15:35:07 -0700299 // If we're in a stylus button press, don't check for long press.
300 if (!mStylusEventHelper.inStylusButtonPressed()) {
301 mLongPressHelper.postCheckForLongPress();
302 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800303 break;
304 case MotionEvent.ACTION_CANCEL:
305 case MotionEvent.ACTION_UP:
306 // If we've touched down and up on an item, and it's still not "pressed", then
307 // destroy the pressed outline
308 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700309 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800310 }
Winson Chung88f33452012-02-23 15:23:44 -0800311
312 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800313 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400314 case MotionEvent.ACTION_MOVE:
315 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
316 mLongPressHelper.cancelLongPress();
317 }
318 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800319 }
320 return result;
321 }
322
Michael Jurkaddd62e92011-02-16 17:49:14 -0800323 void setStayPressed(boolean stayPressed) {
324 mStayPressed = stayPressed;
325 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700326 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700327 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700328 } else {
329 if (mPressedBackground == null) {
330 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
331 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800332 }
Sunny Goyal508da152014-08-14 10:53:27 -0700333
334 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700335 ViewParent parent = getParent();
336 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
337 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
338 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700339 }
340
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800341 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800342 }
343
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700344 @Override
345 public void onLauncherResume() {
346 // Reset the pressed state of icon that was locked in the press state while activity
347 // was launching
348 setStayPressed(false);
349 }
350
Sunny Goyal508da152014-08-14 10:53:27 -0700351 void clearPressedBackground() {
352 setPressed(false);
353 setStayPressed(false);
354 }
355
356 @Override
357 public boolean onKeyDown(int keyCode, KeyEvent event) {
358 if (super.onKeyDown(keyCode, event)) {
359 // Pre-create shadow so show immediately on click.
360 if (mPressedBackground == null) {
361 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700362 }
Sunny Goyal508da152014-08-14 10:53:27 -0700363 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700364 }
Sunny Goyal508da152014-08-14 10:53:27 -0700365 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800366 }
367
Sunny Goyal508da152014-08-14 10:53:27 -0700368 @Override
369 public boolean onKeyUp(int keyCode, KeyEvent event) {
370 // Unlike touch events, keypress event propagate pressed state change immediately,
371 // without waiting for onClickHandler to execute. Disable pressed state changes here
372 // to avoid flickering.
373 mIgnorePressedStateChange = true;
374 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700375
Sunny Goyal508da152014-08-14 10:53:27 -0700376 mPressedBackground = null;
377 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800378 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700379 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800380 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800381
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700382 @SuppressWarnings("wrongcall")
383 protected void drawWithoutBadge(Canvas canvas) {
384 super.onDraw(canvas);
385 }
386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 @Override
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700388 public void onDraw(Canvas canvas) {
389 super.onDraw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800390 drawBadgeIfNecessary(canvas);
391 }
392
393 /**
394 * Draws the icon badge in the top right corner of the icon bounds.
395 * @param canvas The canvas to draw to.
396 */
Sunny Goyal9314b7c2017-06-23 10:36:27 -0700397 protected void drawBadgeIfNecessary(Canvas canvas) {
Tony Wickham1237df02017-02-24 08:59:36 -0800398 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
399 getIconBounds(mTempIconBounds);
400 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
401 final int scrollX = getScrollX();
402 final int scrollY = getScrollY();
403 canvas.translate(scrollX, scrollY);
Tony Wickham7092db02017-06-07 14:32:23 -0700404 mBadgeRenderer.draw(canvas, mBadgePalette, mBadgeInfo, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800405 mTempSpaceForBadgeOffset);
406 canvas.translate(-scrollX, -scrollY);
407 }
408 }
409
410 public void forceHideBadge(boolean forceHideBadge) {
411 if (mForceHideBadge == forceHideBadge) {
412 return;
413 }
414 mForceHideBadge = forceHideBadge;
415
416 if (forceHideBadge) {
417 invalidate();
418 } else if (hasBadge()) {
419 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
420 }
421 }
422
423 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700424 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800425 }
426
427 public void getIconBounds(Rect outBounds) {
428 int top = getPaddingTop();
429 int left = (getWidth() - mIconSize) / 2;
430 int right = left + mIconSize;
431 int bottom = top + mIconSize;
432 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400434
435 @Override
Winson1f064272016-07-18 17:18:02 -0700436 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
437 if (mCenterVertically) {
438 Paint.FontMetrics fm = getPaint().getFontMetrics();
439 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
440 (int) Math.ceil(fm.bottom - fm.top);
441 int height = MeasureSpec.getSize(heightMeasureSpec);
442 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
443 getPaddingBottom());
444 }
445 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
446 }
447
448 @Override
Adam Cohen477828c2013-09-20 12:05:49 -0700449 public void setTextColor(int color) {
450 mTextColor = color;
451 super.setTextColor(color);
452 }
453
Adam Cohen96bb7982014-07-07 11:58:56 -0700454 @Override
455 public void setTextColor(ColorStateList colors) {
456 mTextColor = colors.getDefaultColor();
457 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700458 }
459
Tony7308cde2017-06-27 22:38:33 -0700460 public boolean shouldTextBeVisible() {
461 // Text should be visible everywhere but the hotseat.
462 Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag();
463 ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null;
464 return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT;
465 }
466
Winson Chung5f8afe62013-08-12 16:19:28 -0700467 public void setTextVisibility(boolean visible) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700468 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700469 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700470 } else {
Tony8f402802017-06-16 17:24:54 -0700471 setTextAlpha(0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700472 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700473 }
474
Tony8f402802017-06-16 17:24:54 -0700475 private void setTextAlpha(int alpha) {
476 super.setTextColor(ColorUtils.setAlphaComponent(mTextColor, alpha));
477 }
478
479 private int getTextAlpha() {
480 return Color.alpha(getCurrentTextColor());
481 }
482
483 /**
484 * Creates an animator to fade the text in or out.
485 * @param fadeIn Whether the text should fade in or fade out.
486 */
Sunny Goyal4e5a8782017-06-23 09:34:06 -0700487 public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) {
Tony7308cde2017-06-27 22:38:33 -0700488 int toAlpha = shouldTextBeVisible() && fadeIn ? Color.alpha(mTextColor) : 0;
489 return ObjectAnimator.ofInt(this, TEXT_ALPHA_PROPERTY, toAlpha);
Tony8f402802017-06-16 17:24:54 -0700490 }
491
Winson Chungaffd7b42010-08-20 15:11:56 -0700492 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800493 public void cancelLongPress() {
494 super.cancelLongPress();
495
496 mLongPressHelper.cancelLongPress();
497 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500498
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800499 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700500 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400501 ShortcutInfo info = (ShortcutInfo) getTag();
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -0700502 final boolean isPromise = info.hasPromiseIconUi();
Sunny Goyal34942622014-08-29 17:20:55 -0700503 final int progressLevel = isPromise ?
504 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
505 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700506
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700507 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
508 if (preloadDrawable != null && promiseStateChanged) {
509 preloadDrawable.maybePerformFinishedAnimation();
510 }
511 }
512 }
513
514 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
515 if (getTag() instanceof ItemInfoWithIcon) {
516 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Mario Bertschler230612f2017-09-27 17:05:21 -0700517 if (progressLevel >= 100) {
518 setContentDescription(info.contentDescription != null
519 ? info.contentDescription : "");
520 } else if (progressLevel > 0) {
521 setContentDescription(getContext()
522 .getString(R.string.app_downloading_title, info.title,
523 NumberFormat.getPercentInstance().format(progressLevel * 0.01)));
524 } else {
525 setContentDescription(getContext()
526 .getString(R.string.app_waiting_download_title, info.title));
527 }
Winson Chungb745afb2015-03-02 11:51:23 -0800528 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700529 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800530 if (mIcon instanceof PreloadIconDrawable) {
531 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700532 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700533 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800534 preloadDrawable = DrawableFactory.get(getContext())
535 .newPendingIcon(info.iconBitmap, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700536 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700537 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700538 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700539 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700540 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400541 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700542 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400543 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700544
Tony Wickham1e618492017-02-02 12:57:18 -0800545 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800546 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800547 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700548 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
549 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800550 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700551 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800552 if (wasBadged || isBadged) {
Tony Wickham7092db02017-06-07 14:32:23 -0700553 mBadgePalette = IconPalette.getBadgePalette(getResources());
554 if (mBadgePalette == null) {
555 mBadgePalette = ((FastBitmapDrawable) mIcon).getIconPalette();
556 }
Tony Wickham1237df02017-02-24 08:59:36 -0800557 // Animate when a badge is first added or when it is removed.
558 if (animate && (wasBadged ^ isBadged) && isShown()) {
559 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
560 } else {
561 mBadgeScale = newBadgeScale;
562 invalidate();
563 }
564 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800565 }
566 }
567
Tony Wickham7092db02017-06-07 14:32:23 -0700568 public IconPalette getBadgePalette() {
569 return mBadgePalette;
570 }
571
Winson Chungb745afb2015-03-02 11:51:23 -0800572 /**
573 * Sets the icon for this view based on the layout direction.
574 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700575 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800576 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800577 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700578 if (mIsIconVisible) {
579 applyCompoundDrawables(mIcon);
580 }
581 }
582
583 public void setIconVisible(boolean visible) {
584 mIsIconVisible = visible;
585 mDisableRelayout = true;
586 Drawable icon = mIcon;
587 if (!visible) {
588 icon = new ColorDrawable(Color.TRANSPARENT);
589 icon.setBounds(0, 0, mIconSize, mIconSize);
590 }
591 applyCompoundDrawables(icon);
592 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700593 }
594
595 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800596 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800597 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800598 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700599 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800600 }
Winson Chungb745afb2015-03-02 11:51:23 -0800601 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700602
Sunny Goyal69b75642015-05-15 17:00:24 -0700603 @Override
604 public void requestLayout() {
605 if (!mDisableRelayout) {
606 super.requestLayout();
607 }
608 }
609
Sunny Goyal34b65272015-03-11 16:56:52 -0700610 /**
611 * Applies the item info if it is same as what the view is pointing to currently.
612 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800613 @Override
614 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700615 if (getTag() == info) {
616 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700617 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700618
Jon Miranda7f522a22017-07-28 11:56:47 -0700619 // Optimization: Starting in N, pre-uploads the bitmap to RenderThread.
620 info.iconBitmap.prepareToDraw();
621
Sunny Goyal34b65272015-03-11 16:56:52 -0700622 if (info instanceof AppInfo) {
623 applyFromApplicationInfo((AppInfo) info);
624 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800625 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800626 FolderIconPreviewVerifier verifier =
627 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
628 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700629 View folderIcon =
630 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
631 if (folderIcon != null) {
632 folderIcon.invalidate();
633 }
634 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700635 } else if (info instanceof PackageItemInfo) {
636 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700637 }
Winsonc0880492015-08-21 11:16:27 -0700638
Sunny Goyal69b75642015-05-15 17:00:24 -0700639 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700640 }
641 }
642
643 /**
644 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
645 */
646 public void verifyHighRes() {
647 if (mIconLoadRequest != null) {
648 mIconLoadRequest.cancel();
649 mIconLoadRequest = null;
650 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800651 if (getTag() instanceof ItemInfoWithIcon) {
652 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700653 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800654 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700655 .updateIconInBackground(BubbleTextView.this, info);
656 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700657 }
658 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700659
Jon Miranda47170112017-03-03 14:57:14 -0800660 public int getIconSize() {
661 return mIconSize;
662 }
663
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700664 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700665 * Interface to be implemented by the grand parent to allow click shadow effect.
666 */
Winsonc0880492015-08-21 11:16:27 -0700667 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700668 void setPressedIcon(BubbleTextView icon, Bitmap background);
669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670}