blob: 1f7eba812e8f14895c620612fc4472314ce69c9a [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;
Winson Chung656d11c2010-11-29 17:15:47 -080022import android.content.res.Resources;
Adam Cohen96bb7982014-07-07 11:58:56 -070023import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080024import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Canvas;
Tony17b7f9b2017-05-23 12:19:09 -070026import android.graphics.Color;
Winson1f064272016-07-18 17:18:02 -070027import android.graphics.Paint;
Tony Wickham1237df02017-02-24 08:59:36 -080028import android.graphics.Point;
29import android.graphics.Rect;
Michael Jurka137142e2011-01-05 20:57:04 -080030import android.graphics.Region;
Tony17b7f9b2017-05-23 12:19:09 -070031import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080033import android.util.AttributeSet;
Tony Wickham1237df02017-02-24 08:59:36 -080034import android.util.Property;
Winson Chung5f8afe62013-08-12 16:19:28 -070035import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070036import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080037import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070038import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040039import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080040import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070041import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080042import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070043
Sunny Goyal34b65272015-03-11 16:56:52 -070044import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080045import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
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;
Jon Miranda529af302017-02-28 14:18:54 -080048import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080049import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070050import com.android.launcher3.graphics.HolographicOutlineHelper;
Tony Wickham1237df02017-02-24 08:59:36 -080051import com.android.launcher3.graphics.IconPalette;
Sunny Goyal96ac68a2017-02-02 16:37:21 -080052import com.android.launcher3.graphics.PreloadIconDrawable;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070053import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070054
Sunny Goyalc469aad2015-10-01 11:24:23 -070055import java.text.NumberFormat;
56
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057/**
58 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
59 * because we want to make the bubble taller than the text and TextView's clip is
60 * too aggressive.
61 */
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080062public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070063
Sunny Goyal8f8f3982016-07-25 17:08:38 -070064 // Dimensions in DP
65 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
66 private static final float KEY_SHADOW_RADIUS = 1f;
67 private static final float KEY_SHADOW_OFFSET = 0.5f;
68 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
69 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080070
Sunny Goyaldfaccf62015-05-11 16:30:44 -070071 private static final int DISPLAY_WORKSPACE = 0;
72 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070073 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070074
Sunny Goyal2a76e3f2017-02-16 13:33:15 -080075 private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed};
76
Sunny Goyal53d7ee42015-05-22 12:25:45 -070077 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080078 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070079 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070081 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080082 private final CheckLongPressHelper mLongPressHelper;
83 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070084 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080085
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080086 private boolean mBackgroundSizeChanged;
87
Sunny Goyal508da152014-08-14 10:53:27 -070088 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080089
Jason Monk02dd7ae2014-04-15 15:23:31 -040090 private float mSlop;
91
Winson Chung93f98ea2015-03-10 16:28:47 -070092 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070093 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080094 private final boolean mLayoutHorizontal;
95 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080097 private int mTextColor;
Tony17b7f9b2017-05-23 12:19:09 -070098 private boolean mIsIconVisible = true;
Winson Chung5f8afe62013-08-12 16:19:28 -070099
Tony Wickham1237df02017-02-24 08:59:36 -0800100 private BadgeInfo mBadgeInfo;
101 private BadgeRenderer mBadgeRenderer;
102 private IconPalette mIconPalette;
103 private float mBadgeScale;
104 private boolean mForceHideBadge;
105 private Point mTempSpaceForBadgeOffset = new Point();
106 private Rect mTempIconBounds = new Rect();
107
108 private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY
109 = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") {
110 @Override
111 public Float get(BubbleTextView bubbleTextView) {
112 return bubbleTextView.mBadgeScale;
113 }
114
115 @Override
116 public void set(BubbleTextView bubbleTextView, Float value) {
117 bubbleTextView.mBadgeScale = value;
118 bubbleTextView.invalidate();
119 }
120 };
121
Sunny Goyal4ffec482016-02-09 11:28:52 -0800122 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -0800123 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800124 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -0700125 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -0800126 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700127 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500128
Sunny Goyal34b65272015-03-11 16:56:52 -0700129 private IconLoadRequest mIconLoadRequest;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700132 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 }
134
135 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700136 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138
139 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
140 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700141 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700142 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700143
Adam Cohen96bb7982014-07-07 11:58:56 -0700144 TypedArray a = context.obtainStyledAttributes(attrs,
145 R.styleable.BubbleTextView, defStyle, 0);
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800146 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, false);
Winson Chungb745afb2015-03-02 11:51:23 -0800147 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700148 mDeferShadowGenerationOnTouch =
149 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700150
151 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
152 int defaultIconSize = grid.iconSizePx;
153 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700154 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700155 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700156 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
157 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700158 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700159 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700160 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700161 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700162 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700163 }
Winson1f064272016-07-18 17:18:02 -0700164 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700165
166 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
167 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700168 a.recycle();
169
Sunny Goyal15872da2014-07-08 15:43:54 -0700170 if (mCustomShadowsEnabled) {
171 // Draw the background itself as the parent is drawn twice.
172 mBackground = getBackground();
173 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700174
175 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
176 float density = getResources().getDisplayMetrics().density;
177 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700178 } else {
179 mBackground = null;
180 }
Winson Chungb745afb2015-03-02 11:51:23 -0800181
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800182 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700183 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800184
Sunny Goyal10629b02016-09-01 12:50:11 -0700185 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700186 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 }
188
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800189 public void applyFromShortcutInfo(ShortcutInfo info) {
190 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700191 }
192
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800193 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
194 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800195 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700196 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800197 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500198 }
Tony Wickham010d2552017-01-20 08:15:28 -0800199
Tony Wickham1e618492017-02-02 12:57:18 -0800200 applyBadgeState(info, false /* animate */);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800201 }
202
Sunny Goyal508da152014-08-14 10:53:27 -0700203 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700204 applyIconAndLabel(info.iconBitmap, info);
205
Winson Chung888b3a12015-03-13 11:14:16 -0700206 // We don't need to check the info since it's not a ShortcutInfo
207 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700208
209 // Verify high res immediately
210 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800211
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700212 if (info instanceof PromiseAppInfo) {
213 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
214 applyProgressLevel(promiseAppInfo.level);
215 }
Tony Wickham1e618492017-02-02 12:57:18 -0800216 applyBadgeState(info, false /* animate */);
Sunny Goyal508da152014-08-14 10:53:27 -0700217 }
218
Sunny Goyal0e08f162015-05-12 11:32:39 -0700219 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700220 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700221 // We don't need to check the info since it's not a ShortcutInfo
222 super.setTag(info);
223
224 // Verify high res immediately
225 verifyHighRes();
226 }
227
Sunny Goyalf4204382016-07-13 10:46:07 -0700228 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800229 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800230 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700231 setIcon(iconDrawable);
232 setText(info.title);
233 if (info.contentDescription != null) {
234 setContentDescription(info.isDisabled()
235 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
236 : info.contentDescription);
237 }
238 }
239
Winson Chung7501adf2015-06-02 11:24:28 -0700240 /**
241 * Overrides the default long press timeout.
242 */
243 public void setLongPressTimeout(int longPressTimeout) {
244 mLongPressHelper.setLongPressTimeout(longPressTimeout);
245 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 @Override
248 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700249 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 mBackgroundSizeChanged = true;
251 }
252 return super.setFrame(left, top, right, bottom);
253 }
254
255 @Override
256 protected boolean verifyDrawable(Drawable who) {
257 return who == mBackground || super.verifyDrawable(who);
258 }
259
260 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700261 public void setTag(Object tag) {
262 if (tag != null) {
263 LauncherModel.checkItemInfo((ItemInfo) tag);
264 }
265 super.setTag(tag);
266 }
267
268 @Override
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800269 public void refreshDrawableState() {
Sunny Goyal508da152014-08-14 10:53:27 -0700270 if (!mIgnorePressedStateChange) {
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800271 super.refreshDrawableState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800272 }
Sunny Goyal508da152014-08-14 10:53:27 -0700273 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800274
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800275 @Override
276 protected int[] onCreateDrawableState(int extraSpace) {
277 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
278 if (mStayPressed) {
279 mergeDrawableStates(drawableState, STATE_PRESSED);
280 }
281 return drawableState;
282 }
283
Winson Chungb745afb2015-03-02 11:51:23 -0800284 /** Returns the icon for this view. */
285 public Drawable getIcon() {
286 return mIcon;
287 }
288
289 /** Returns whether the layout is horizontal. */
290 public boolean isLayoutHorizontal() {
291 return mLayoutHorizontal;
292 }
293
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800294 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700295 public void setOnLongClickListener(OnLongClickListener l) {
296 super.setOnLongClickListener(l);
297 mOnLongClickListener = l;
298 }
299
300 public OnLongClickListener getOnLongClickListener() {
301 return mOnLongClickListener;
302 }
303
304 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800305 public boolean onTouchEvent(MotionEvent event) {
306 // Call the superclass onTouchEvent first, because sometimes it changes the state to
307 // isPressed() on an ACTION_UP
308 boolean result = super.onTouchEvent(event);
309
Mady Melloref044dd2015-06-02 15:35:07 -0700310 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700311 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700312 mLongPressHelper.cancelLongPress();
313 result = true;
314 }
315
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800316 switch (event.getAction()) {
317 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700318 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800319 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
320 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700321 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700322 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800323 }
Winson Chung88f33452012-02-23 15:23:44 -0800324
Mady Melloref044dd2015-06-02 15:35:07 -0700325 // If we're in a stylus button press, don't check for long press.
326 if (!mStylusEventHelper.inStylusButtonPressed()) {
327 mLongPressHelper.postCheckForLongPress();
328 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800329 break;
330 case MotionEvent.ACTION_CANCEL:
331 case MotionEvent.ACTION_UP:
332 // If we've touched down and up on an item, and it's still not "pressed", then
333 // destroy the pressed outline
334 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700335 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800336 }
Winson Chung88f33452012-02-23 15:23:44 -0800337
338 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800339 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400340 case MotionEvent.ACTION_MOVE:
341 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
342 mLongPressHelper.cancelLongPress();
343 }
344 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800345 }
346 return result;
347 }
348
Michael Jurkaddd62e92011-02-16 17:49:14 -0800349 void setStayPressed(boolean stayPressed) {
350 mStayPressed = stayPressed;
351 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700352 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700353 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700354 } else {
355 if (mPressedBackground == null) {
356 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
357 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800358 }
Sunny Goyal508da152014-08-14 10:53:27 -0700359
360 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700361 ViewParent parent = getParent();
362 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
363 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
364 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700365 }
366
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800367 refreshDrawableState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800368 }
369
Sunny Goyal508da152014-08-14 10:53:27 -0700370 void clearPressedBackground() {
371 setPressed(false);
372 setStayPressed(false);
373 }
374
375 @Override
376 public boolean onKeyDown(int keyCode, KeyEvent event) {
377 if (super.onKeyDown(keyCode, event)) {
378 // Pre-create shadow so show immediately on click.
379 if (mPressedBackground == null) {
380 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700381 }
Sunny Goyal508da152014-08-14 10:53:27 -0700382 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700383 }
Sunny Goyal508da152014-08-14 10:53:27 -0700384 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800385 }
386
Sunny Goyal508da152014-08-14 10:53:27 -0700387 @Override
388 public boolean onKeyUp(int keyCode, KeyEvent event) {
389 // Unlike touch events, keypress event propagate pressed state change immediately,
390 // without waiting for onClickHandler to execute. Disable pressed state changes here
391 // to avoid flickering.
392 mIgnorePressedStateChange = true;
393 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700394
Sunny Goyal508da152014-08-14 10:53:27 -0700395 mPressedBackground = null;
396 mIgnorePressedStateChange = false;
Sunny Goyal2a76e3f2017-02-16 13:33:15 -0800397 refreshDrawableState();
Sunny Goyal508da152014-08-14 10:53:27 -0700398 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800399 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 @Override
402 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700403 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700404 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800405 drawBadgeIfNecessary(canvas);
Adam Cohen477828c2013-09-20 12:05:49 -0700406 return;
407 }
408
Michael Jurkabdb5c532011-02-01 15:05:06 -0800409 final Drawable background = mBackground;
410 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700411 final int scrollX = getScrollX();
412 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800413
Michael Jurkabdb5c532011-02-01 15:05:06 -0800414 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700415 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800416 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800418
419 if ((scrollX | scrollY) == 0) {
420 background.draw(canvas);
421 } else {
422 canvas.translate(scrollX, scrollY);
423 background.draw(canvas);
424 canvas.translate(-scrollX, -scrollY);
425 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800427
428 // If text is transparent, don't draw any shadow
Sunny Goyal1f3f07d2017-02-10 16:52:16 -0800429 if ((getCurrentTextColor() >> 24) == 0) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800430 getPaint().clearShadowLayer();
431 super.draw(canvas);
Tony Wickham1237df02017-02-24 08:59:36 -0800432 drawBadgeIfNecessary(canvas);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800433 return;
434 }
435
Michael Jurkabdb5c532011-02-01 15:05:06 -0800436 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700437 float density = getResources().getDisplayMetrics().density;
438 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800439 super.draw(canvas);
440 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700441 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
442 getScrollX() + getWidth(),
443 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700444 getPaint().setShadowLayer(
445 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800446 super.draw(canvas);
447 canvas.restore();
Tony Wickham1237df02017-02-24 08:59:36 -0800448
449 drawBadgeIfNecessary(canvas);
450 }
451
452 /**
453 * Draws the icon badge in the top right corner of the icon bounds.
454 * @param canvas The canvas to draw to.
455 */
456 private void drawBadgeIfNecessary(Canvas canvas) {
457 if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
458 getIconBounds(mTempIconBounds);
459 mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
460 final int scrollX = getScrollX();
461 final int scrollY = getScrollY();
462 canvas.translate(scrollX, scrollY);
Tony Wickhame6a790e2017-05-16 12:07:38 -0700463 mBadgeRenderer.draw(canvas, mBadgeInfo, mTempIconBounds, mBadgeScale,
Tony Wickham1237df02017-02-24 08:59:36 -0800464 mTempSpaceForBadgeOffset);
465 canvas.translate(-scrollX, -scrollY);
466 }
467 }
468
469 public void forceHideBadge(boolean forceHideBadge) {
470 if (mForceHideBadge == forceHideBadge) {
471 return;
472 }
473 mForceHideBadge = forceHideBadge;
474
475 if (forceHideBadge) {
476 invalidate();
477 } else if (hasBadge()) {
478 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start();
479 }
480 }
481
482 private boolean hasBadge() {
Tony Wickham0530e8c2017-04-26 18:13:56 -0700483 return mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800484 }
485
486 public void getIconBounds(Rect outBounds) {
487 int top = getPaddingTop();
488 int left = (getWidth() - mIconSize) / 2;
489 int right = left + mIconSize;
490 int bottom = top + mIconSize;
491 outBounds.set(left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400493
494 @Override
495 protected void onAttachedToWindow() {
496 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700497
Winson Chung656d11c2010-11-29 17:15:47 -0800498 if (mBackground != null) mBackground.setCallback(this);
Jason Monk02dd7ae2014-04-15 15:23:31 -0400499 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400500 }
501
502 @Override
Winson1f064272016-07-18 17:18:02 -0700503 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
504 if (mCenterVertically) {
505 Paint.FontMetrics fm = getPaint().getFontMetrics();
506 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
507 (int) Math.ceil(fm.bottom - fm.top);
508 int height = MeasureSpec.getSize(heightMeasureSpec);
509 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
510 getPaddingBottom());
511 }
512 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
513 }
514
515 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 protected void onDetachedFromWindow() {
517 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800518 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400519 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700520
Adam Cohen477828c2013-09-20 12:05:49 -0700521 @Override
522 public void setTextColor(int color) {
523 mTextColor = color;
524 super.setTextColor(color);
525 }
526
Adam Cohen96bb7982014-07-07 11:58:56 -0700527 @Override
528 public void setTextColor(ColorStateList colors) {
529 mTextColor = colors.getDefaultColor();
530 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700531 }
532
Winson Chung5f8afe62013-08-12 16:19:28 -0700533 public void setTextVisibility(boolean visible) {
534 Resources res = getResources();
535 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700536 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700537 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700538 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700539 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700540 }
541
Winson Chungaffd7b42010-08-20 15:11:56 -0700542 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800543 public void cancelLongPress() {
544 super.cancelLongPress();
545
546 mLongPressHelper.cancelLongPress();
547 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500548
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800549 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700550 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400551 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700552 final boolean isPromise = info.isPromise();
553 final int progressLevel = isPromise ?
554 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
555 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700556
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700557 PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel);
558 if (preloadDrawable != null && promiseStateChanged) {
559 preloadDrawable.maybePerformFinishedAnimation();
560 }
561 }
562 }
563
564 public PreloadIconDrawable applyProgressLevel(int progressLevel) {
565 if (getTag() instanceof ItemInfoWithIcon) {
566 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
567 setContentDescription(progressLevel > 0
568 ? getContext().getString(R.string.app_downloading_title, info.title,
569 NumberFormat.getPercentInstance().format(progressLevel * 0.01))
570 : getContext().getString(R.string.app_waiting_download_title, info.title));
Sunny Goyalc469aad2015-10-01 11:24:23 -0700571
Winson Chungb745afb2015-03-02 11:51:23 -0800572 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700573 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800574 if (mIcon instanceof PreloadIconDrawable) {
575 preloadDrawable = (PreloadIconDrawable) mIcon;
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700576 preloadDrawable.setLevel(progressLevel);
Sunny Goyale755d462014-07-22 13:48:29 -0700577 } else {
Sunny Goyal96ac68a2017-02-02 16:37:21 -0800578 preloadDrawable = DrawableFactory.get(getContext())
579 .newPendingIcon(info.iconBitmap, getContext());
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700580 preloadDrawable.setLevel(progressLevel);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700581 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700582 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700583 return preloadDrawable;
Sunny Goyale755d462014-07-22 13:48:29 -0700584 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400585 }
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700586 return null;
Chris Wren40c5ed32014-06-24 18:24:23 -0400587 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700588
Tony Wickham1e618492017-02-02 12:57:18 -0800589 public void applyBadgeState(ItemInfo itemInfo, boolean animate) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800590 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham1237df02017-02-24 08:59:36 -0800591 boolean wasBadged = mBadgeInfo != null;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700592 mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
593 boolean isBadged = mBadgeInfo != null;
Tony Wickham1237df02017-02-24 08:59:36 -0800594 float newBadgeScale = isBadged ? 1f : 0;
Tony Wickham2fe09f22017-04-25 12:46:04 -0700595 mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
Tony Wickham1237df02017-02-24 08:59:36 -0800596 if (wasBadged || isBadged) {
597 mIconPalette = ((FastBitmapDrawable) mIcon).getIconPalette();
598 // Animate when a badge is first added or when it is removed.
599 if (animate && (wasBadged ^ isBadged) && isShown()) {
600 ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start();
601 } else {
602 mBadgeScale = newBadgeScale;
603 invalidate();
604 }
605 }
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800606 }
607 }
608
Winson Chungb745afb2015-03-02 11:51:23 -0800609 /**
610 * Sets the icon for this view based on the layout direction.
611 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700612 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800613 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800614 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony17b7f9b2017-05-23 12:19:09 -0700615 if (mIsIconVisible) {
616 applyCompoundDrawables(mIcon);
617 }
618 }
619
620 public void setIconVisible(boolean visible) {
621 mIsIconVisible = visible;
622 mDisableRelayout = true;
623 Drawable icon = mIcon;
624 if (!visible) {
625 icon = new ColorDrawable(Color.TRANSPARENT);
626 icon.setBounds(0, 0, mIconSize, mIconSize);
627 }
628 applyCompoundDrawables(icon);
629 mDisableRelayout = false;
Tony Wickham377ed3f2016-07-20 15:21:04 -0700630 }
631
632 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800633 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800634 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800635 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700636 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800637 }
Winson Chungb745afb2015-03-02 11:51:23 -0800638 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700639
Sunny Goyal69b75642015-05-15 17:00:24 -0700640 @Override
641 public void requestLayout() {
642 if (!mDisableRelayout) {
643 super.requestLayout();
644 }
645 }
646
Sunny Goyal34b65272015-03-11 16:56:52 -0700647 /**
648 * Applies the item info if it is same as what the view is pointing to currently.
649 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800650 @Override
651 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700652 if (getTag() == info) {
653 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700654 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700655
Sunny Goyal34b65272015-03-11 16:56:52 -0700656 if (info instanceof AppInfo) {
657 applyFromApplicationInfo((AppInfo) info);
658 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800659 applyFromShortcutInfo((ShortcutInfo) info);
Jon Miranda529af302017-02-28 14:18:54 -0800660 FolderIconPreviewVerifier verifier =
661 new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv);
662 if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) {
Sunny Goyal317698b2015-07-29 11:45:41 -0700663 View folderIcon =
664 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
665 if (folderIcon != null) {
666 folderIcon.invalidate();
667 }
668 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700669 } else if (info instanceof PackageItemInfo) {
670 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700671 }
Winsonc0880492015-08-21 11:16:27 -0700672
Sunny Goyal69b75642015-05-15 17:00:24 -0700673 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700674 }
675 }
676
677 /**
678 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
679 */
680 public void verifyHighRes() {
681 if (mIconLoadRequest != null) {
682 mIconLoadRequest.cancel();
683 mIconLoadRequest = null;
684 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800685 if (getTag() instanceof ItemInfoWithIcon) {
686 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700687 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800688 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700689 .updateIconInBackground(BubbleTextView.this, info);
690 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700691 }
692 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700693
Jon Miranda47170112017-03-03 14:57:14 -0800694 public int getIconSize() {
695 return mIconSize;
696 }
697
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700698 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700699 * Interface to be implemented by the grand parent to allow click shadow effect.
700 */
Winsonc0880492015-08-21 11:16:27 -0700701 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700702 void setPressedIcon(BubbleTextView icon, Bitmap background);
703 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704}