The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 19 | import android.animation.ObjectAnimator; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 21 | import android.content.res.ColorStateList; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.graphics.Canvas; |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 25 | import android.graphics.Color; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 26 | import android.graphics.Paint; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 27 | import android.graphics.Point; |
| 28 | import android.graphics.Rect; |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.ColorDrawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.graphics.drawable.Drawable; |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 31 | import android.support.v4.graphics.ColorUtils; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 32 | import android.util.AttributeSet; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 33 | import android.util.Property; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 34 | import android.util.TypedValue; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 35 | import android.view.KeyEvent; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 36 | import android.view.MotionEvent; |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 37 | import android.view.View; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 38 | import android.view.ViewConfiguration; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 39 | import android.view.ViewDebug; |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 40 | import android.view.ViewParent; |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 41 | import android.widget.TextView; |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 42 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 43 | import com.android.launcher3.IconCache.IconLoadRequest; |
Sunny Goyal | 2d7cca1 | 2017-01-03 16:52:43 -0800 | [diff] [blame] | 44 | import com.android.launcher3.IconCache.ItemInfoUpdateReceiver; |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 45 | import com.android.launcher3.badge.BadgeInfo; |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 46 | import com.android.launcher3.badge.BadgeRenderer; |
Jon Miranda | 529af30 | 2017-02-28 14:18:54 -0800 | [diff] [blame] | 47 | import com.android.launcher3.folder.FolderIconPreviewVerifier; |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 48 | import com.android.launcher3.graphics.DrawableFactory; |
Sunny Goyal | 10629b0 | 2016-09-01 12:50:11 -0700 | [diff] [blame] | 49 | import com.android.launcher3.graphics.HolographicOutlineHelper; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 50 | import com.android.launcher3.graphics.IconPalette; |
Sunny Goyal | 96ac68a | 2017-02-02 16:37:21 -0800 | [diff] [blame] | 51 | import com.android.launcher3.graphics.PreloadIconDrawable; |
Hyunyoung Song | 2bd3d7d | 2015-05-21 13:04:53 -0700 | [diff] [blame] | 52 | import com.android.launcher3.model.PackageItemInfo; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 53 | |
Sunny Goyal | c469aad | 2015-10-01 11:24:23 -0700 | [diff] [blame] | 54 | import java.text.NumberFormat; |
| 55 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 56 | /** |
| 57 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 58 | * because we want to make the bubble taller than the text and TextView's clip is |
| 59 | * too aggressive. |
| 60 | */ |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 61 | public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver { |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 62 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 63 | private static final int DISPLAY_WORKSPACE = 0; |
| 64 | private static final int DISPLAY_ALL_APPS = 1; |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 65 | private static final int DISPLAY_FOLDER = 2; |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 66 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 67 | private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed}; |
| 68 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 69 | private final Launcher mLauncher; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 70 | private Drawable mIcon; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 71 | private final boolean mCenterVertically; |
Sunny Goyal | 4e5a878 | 2017-06-23 09:34:06 -0700 | [diff] [blame^] | 72 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 73 | private final CheckLongPressHelper mLongPressHelper; |
| 74 | private final HolographicOutlineHelper mOutlineHelper; |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 75 | private final StylusEventHelper mStylusEventHelper; |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 76 | private final float mSlop; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 77 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 78 | private Bitmap mPressedBackground; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 79 | |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 80 | private final boolean mDeferShadowGenerationOnTouch; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 81 | private final boolean mLayoutHorizontal; |
| 82 | private final int mIconSize; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 83 | @ViewDebug.ExportedProperty(category = "launcher") |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 84 | private int mTextColor; |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 85 | private boolean mIsIconVisible = true; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 86 | |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 87 | private BadgeInfo mBadgeInfo; |
| 88 | private BadgeRenderer mBadgeRenderer; |
Tony Wickham | 7092db0 | 2017-06-07 14:32:23 -0700 | [diff] [blame] | 89 | private IconPalette mBadgePalette; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 90 | private float mBadgeScale; |
| 91 | private boolean mForceHideBadge; |
| 92 | private Point mTempSpaceForBadgeOffset = new Point(); |
| 93 | private Rect mTempIconBounds = new Rect(); |
| 94 | |
| 95 | private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY |
| 96 | = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") { |
| 97 | @Override |
| 98 | public Float get(BubbleTextView bubbleTextView) { |
| 99 | return bubbleTextView.mBadgeScale; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void set(BubbleTextView bubbleTextView, Float value) { |
| 104 | bubbleTextView.mBadgeScale = value; |
| 105 | bubbleTextView.invalidate(); |
| 106 | } |
| 107 | }; |
| 108 | |
Sunny Goyal | 4e5a878 | 2017-06-23 09:34:06 -0700 | [diff] [blame^] | 109 | public static final Property<BubbleTextView, Integer> TEXT_ALPHA_PROPERTY |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 110 | = new Property<BubbleTextView, Integer>(Integer.class, "textAlpha") { |
| 111 | @Override |
| 112 | public Integer get(BubbleTextView bubbleTextView) { |
| 113 | return bubbleTextView.getTextAlpha(); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void set(BubbleTextView bubbleTextView, Integer alpha) { |
| 118 | bubbleTextView.setTextAlpha(alpha); |
| 119 | } |
| 120 | }; |
| 121 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 122 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 123 | private boolean mStayPressed; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 124 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 125 | private boolean mIgnorePressedStateChange; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 126 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 127 | private boolean mDisableRelayout = false; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 128 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 129 | private IconLoadRequest mIconLoadRequest; |
| 130 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 131 | public BubbleTextView(Context context) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 132 | this(context, null, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | public BubbleTextView(Context context, AttributeSet attrs) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 136 | this(context, attrs, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 140 | super(context, attrs, defStyle); |
Andrew Sapperstein | abef55a | 2016-06-19 12:49:00 -0700 | [diff] [blame] | 141 | mLauncher = Launcher.getLauncher(context); |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 142 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 143 | mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 144 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 145 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 146 | R.styleable.BubbleTextView, defStyle, 0); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 147 | mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false); |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 148 | mDeferShadowGenerationOnTouch = |
| 149 | a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 150 | |
| 151 | int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE); |
| 152 | int defaultIconSize = grid.iconSizePx; |
| 153 | if (display == DISPLAY_WORKSPACE) { |
Winson Chung | 44d0aac | 2015-05-11 18:02:55 -0700 | [diff] [blame] | 154 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx); |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 155 | setCompoundDrawablePadding(grid.iconDrawablePaddingPx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 156 | } else if (display == DISPLAY_ALL_APPS) { |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 157 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx); |
| 158 | setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 159 | defaultIconSize = grid.allAppsIconSizePx; |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 160 | } else if (display == DISPLAY_FOLDER) { |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 161 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx); |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 162 | setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx); |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 163 | defaultIconSize = grid.folderChildIconSizePx; |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 164 | } |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 165 | mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 166 | |
| 167 | mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride, |
| 168 | defaultIconSize); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 169 | a.recycle(); |
| 170 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 171 | mLongPressHelper = new CheckLongPressHelper(this); |
Mady Mellor | bb83520 | 2015-07-15 16:34:34 -0700 | [diff] [blame] | 172 | mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 173 | |
Sunny Goyal | 10629b0 | 2016-09-01 12:50:11 -0700 | [diff] [blame] | 174 | mOutlineHelper = HolographicOutlineHelper.getInstance(getContext()); |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 175 | setAccessibilityDelegate(mLauncher.getAccessibilityDelegate()); |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 176 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 179 | public void applyFromShortcutInfo(ShortcutInfo info) { |
| 180 | applyFromShortcutInfo(info, false); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 183 | public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) { |
| 184 | applyIconAndLabel(info.iconBitmap, info); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 185 | setTag(info); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 186 | if (promiseStateChanged || info.isPromise()) { |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 187 | applyPromiseState(promiseStateChanged); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 188 | } |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 189 | |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame] | 190 | applyBadgeState(info, false /* animate */); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 193 | public void applyFromApplicationInfo(AppInfo info) { |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 194 | applyIconAndLabel(info.iconBitmap, info); |
| 195 | |
Winson Chung | 888b3a1 | 2015-03-13 11:14:16 -0700 | [diff] [blame] | 196 | // We don't need to check the info since it's not a ShortcutInfo |
| 197 | super.setTag(info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 198 | |
| 199 | // Verify high res immediately |
| 200 | verifyHighRes(); |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 201 | |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 202 | if (info instanceof PromiseAppInfo) { |
| 203 | PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info; |
| 204 | applyProgressLevel(promiseAppInfo.level); |
| 205 | } |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame] | 206 | applyBadgeState(info, false /* animate */); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 209 | public void applyFromPackageItemInfo(PackageItemInfo info) { |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 210 | applyIconAndLabel(info.iconBitmap, info); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 211 | // We don't need to check the info since it's not a ShortcutInfo |
| 212 | super.setTag(info); |
| 213 | |
| 214 | // Verify high res immediately |
| 215 | verifyHighRes(); |
| 216 | } |
| 217 | |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 218 | private void applyIconAndLabel(Bitmap icon, ItemInfo info) { |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 219 | FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info); |
Tony Wickham | 6b910a2 | 2016-11-08 10:40:34 -0800 | [diff] [blame] | 220 | iconDrawable.setIsDisabled(info.isDisabled()); |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 221 | setIcon(iconDrawable); |
| 222 | setText(info.title); |
| 223 | if (info.contentDescription != null) { |
| 224 | setContentDescription(info.isDisabled() |
| 225 | ? getContext().getString(R.string.disabled_app_label, info.contentDescription) |
| 226 | : info.contentDescription); |
| 227 | } |
| 228 | } |
| 229 | |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 230 | /** |
| 231 | * Overrides the default long press timeout. |
| 232 | */ |
| 233 | public void setLongPressTimeout(int longPressTimeout) { |
| 234 | mLongPressHelper.setLongPressTimeout(longPressTimeout); |
| 235 | } |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 236 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 237 | @Override |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 238 | public void setTag(Object tag) { |
| 239 | if (tag != null) { |
| 240 | LauncherModel.checkItemInfo((ItemInfo) tag); |
| 241 | } |
| 242 | super.setTag(tag); |
| 243 | } |
| 244 | |
| 245 | @Override |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 246 | public void refreshDrawableState() { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 247 | if (!mIgnorePressedStateChange) { |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 248 | super.refreshDrawableState(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 249 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 250 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 251 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 252 | @Override |
| 253 | protected int[] onCreateDrawableState(int extraSpace) { |
| 254 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 255 | if (mStayPressed) { |
| 256 | mergeDrawableStates(drawableState, STATE_PRESSED); |
| 257 | } |
| 258 | return drawableState; |
| 259 | } |
| 260 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 261 | /** Returns the icon for this view. */ |
| 262 | public Drawable getIcon() { |
| 263 | return mIcon; |
| 264 | } |
| 265 | |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 266 | @Override |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 267 | public boolean onTouchEvent(MotionEvent event) { |
| 268 | // Call the superclass onTouchEvent first, because sometimes it changes the state to |
| 269 | // isPressed() on an ACTION_UP |
| 270 | boolean result = super.onTouchEvent(event); |
| 271 | |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 272 | // Check for a stylus button press, if it occurs cancel any long press checks. |
Mady Mellor | bb83520 | 2015-07-15 16:34:34 -0700 | [diff] [blame] | 273 | if (mStylusEventHelper.onMotionEvent(event)) { |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 274 | mLongPressHelper.cancelLongPress(); |
| 275 | result = true; |
| 276 | } |
| 277 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 278 | switch (event.getAction()) { |
| 279 | case MotionEvent.ACTION_DOWN: |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 280 | // So that the pressed outline is visible immediately on setStayPressed(), |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 281 | // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time |
| 282 | // to create it) |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 283 | if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 284 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 285 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 286 | |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 287 | // If we're in a stylus button press, don't check for long press. |
| 288 | if (!mStylusEventHelper.inStylusButtonPressed()) { |
| 289 | mLongPressHelper.postCheckForLongPress(); |
| 290 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 291 | break; |
| 292 | case MotionEvent.ACTION_CANCEL: |
| 293 | case MotionEvent.ACTION_UP: |
| 294 | // If we've touched down and up on an item, and it's still not "pressed", then |
| 295 | // destroy the pressed outline |
| 296 | if (!isPressed()) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 297 | mPressedBackground = null; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 298 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 299 | |
| 300 | mLongPressHelper.cancelLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 301 | break; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 302 | case MotionEvent.ACTION_MOVE: |
| 303 | if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) { |
| 304 | mLongPressHelper.cancelLongPress(); |
| 305 | } |
| 306 | break; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 307 | } |
| 308 | return result; |
| 309 | } |
| 310 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 311 | void setStayPressed(boolean stayPressed) { |
| 312 | mStayPressed = stayPressed; |
| 313 | if (!stayPressed) { |
Sunny Goyal | 10629b0 | 2016-09-01 12:50:11 -0700 | [diff] [blame] | 314 | HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 315 | mPressedBackground = null; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 316 | } else { |
| 317 | if (mPressedBackground == null) { |
| 318 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
| 319 | } |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 320 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 321 | |
| 322 | // Only show the shadow effect when persistent pressed state is set. |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 323 | ViewParent parent = getParent(); |
| 324 | if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) { |
| 325 | ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon( |
| 326 | this, mPressedBackground); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 329 | refreshDrawableState(); |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 332 | void clearPressedBackground() { |
| 333 | setPressed(false); |
| 334 | setStayPressed(false); |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 339 | if (super.onKeyDown(keyCode, event)) { |
| 340 | // Pre-create shadow so show immediately on click. |
| 341 | if (mPressedBackground == null) { |
| 342 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 343 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 344 | return true; |
Patrick Dubroy | d69e113 | 2011-03-15 10:29:01 -0700 | [diff] [blame] | 345 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 346 | return false; |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 349 | @Override |
| 350 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 351 | // Unlike touch events, keypress event propagate pressed state change immediately, |
| 352 | // without waiting for onClickHandler to execute. Disable pressed state changes here |
| 353 | // to avoid flickering. |
| 354 | mIgnorePressedStateChange = true; |
| 355 | boolean result = super.onKeyUp(keyCode, event); |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 356 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 357 | mPressedBackground = null; |
| 358 | mIgnorePressedStateChange = false; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 359 | refreshDrawableState(); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 360 | return result; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 361 | } |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 362 | |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 363 | @SuppressWarnings("wrongcall") |
| 364 | protected void drawWithoutBadge(Canvas canvas) { |
| 365 | super.onDraw(canvas); |
| 366 | } |
| 367 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 368 | @Override |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 369 | public void onDraw(Canvas canvas) { |
| 370 | super.onDraw(canvas); |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 371 | drawBadgeIfNecessary(canvas); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Draws the icon badge in the top right corner of the icon bounds. |
| 376 | * @param canvas The canvas to draw to. |
| 377 | */ |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 378 | protected void drawBadgeIfNecessary(Canvas canvas) { |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 379 | if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) { |
| 380 | getIconBounds(mTempIconBounds); |
| 381 | mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop()); |
| 382 | final int scrollX = getScrollX(); |
| 383 | final int scrollY = getScrollY(); |
| 384 | canvas.translate(scrollX, scrollY); |
Tony Wickham | 7092db0 | 2017-06-07 14:32:23 -0700 | [diff] [blame] | 385 | mBadgeRenderer.draw(canvas, mBadgePalette, mBadgeInfo, mTempIconBounds, mBadgeScale, |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 386 | mTempSpaceForBadgeOffset); |
| 387 | canvas.translate(-scrollX, -scrollY); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | public void forceHideBadge(boolean forceHideBadge) { |
| 392 | if (mForceHideBadge == forceHideBadge) { |
| 393 | return; |
| 394 | } |
| 395 | mForceHideBadge = forceHideBadge; |
| 396 | |
| 397 | if (forceHideBadge) { |
| 398 | invalidate(); |
| 399 | } else if (hasBadge()) { |
| 400 | ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, 0, 1).start(); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | private boolean hasBadge() { |
Tony Wickham | 0530e8c | 2017-04-26 18:13:56 -0700 | [diff] [blame] | 405 | return mBadgeInfo != null; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | public void getIconBounds(Rect outBounds) { |
| 409 | int top = getPaddingTop(); |
| 410 | int left = (getWidth() - mIconSize) / 2; |
| 411 | int right = left + mIconSize; |
| 412 | int bottom = top + mIconSize; |
| 413 | outBounds.set(left, top, right, bottom); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 414 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 415 | |
| 416 | @Override |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 417 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 418 | if (mCenterVertically) { |
| 419 | Paint.FontMetrics fm = getPaint().getFontMetrics(); |
| 420 | int cellHeightPx = mIconSize + getCompoundDrawablePadding() + |
| 421 | (int) Math.ceil(fm.bottom - fm.top); |
| 422 | int height = MeasureSpec.getSize(heightMeasureSpec); |
| 423 | setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(), |
| 424 | getPaddingBottom()); |
| 425 | } |
| 426 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 427 | } |
| 428 | |
| 429 | @Override |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 430 | public void setTextColor(int color) { |
| 431 | mTextColor = color; |
| 432 | super.setTextColor(color); |
| 433 | } |
| 434 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 435 | @Override |
| 436 | public void setTextColor(ColorStateList colors) { |
| 437 | mTextColor = colors.getDefaultColor(); |
| 438 | super.setTextColor(colors); |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 441 | public void setTextVisibility(boolean visible) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 442 | if (visible) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 443 | super.setTextColor(mTextColor); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 444 | } else { |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 445 | setTextAlpha(0); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 446 | } |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 449 | private void setTextAlpha(int alpha) { |
| 450 | super.setTextColor(ColorUtils.setAlphaComponent(mTextColor, alpha)); |
| 451 | } |
| 452 | |
| 453 | private int getTextAlpha() { |
| 454 | return Color.alpha(getCurrentTextColor()); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Creates an animator to fade the text in or out. |
| 459 | * @param fadeIn Whether the text should fade in or fade out. |
| 460 | */ |
Sunny Goyal | 4e5a878 | 2017-06-23 09:34:06 -0700 | [diff] [blame^] | 461 | public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) { |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 462 | return ObjectAnimator.ofInt(this, TEXT_ALPHA_PROPERTY, fadeIn ? Color.alpha(mTextColor) : 0); |
| 463 | } |
| 464 | |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 465 | @Override |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 466 | public void cancelLongPress() { |
| 467 | super.cancelLongPress(); |
| 468 | |
| 469 | mLongPressHelper.cancelLongPress(); |
| 470 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 471 | |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 472 | public void applyPromiseState(boolean promiseStateChanged) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 473 | if (getTag() instanceof ShortcutInfo) { |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 474 | ShortcutInfo info = (ShortcutInfo) getTag(); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 475 | final boolean isPromise = info.isPromise(); |
| 476 | final int progressLevel = isPromise ? |
| 477 | ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ? |
| 478 | info.getInstallProgress() : 0)) : 100; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 479 | |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 480 | PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel); |
| 481 | if (preloadDrawable != null && promiseStateChanged) { |
| 482 | preloadDrawable.maybePerformFinishedAnimation(); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | public PreloadIconDrawable applyProgressLevel(int progressLevel) { |
| 488 | if (getTag() instanceof ItemInfoWithIcon) { |
| 489 | ItemInfoWithIcon info = (ItemInfoWithIcon) getTag(); |
| 490 | setContentDescription(progressLevel > 0 |
| 491 | ? getContext().getString(R.string.app_downloading_title, info.title, |
| 492 | NumberFormat.getPercentInstance().format(progressLevel * 0.01)) |
| 493 | : getContext().getString(R.string.app_waiting_download_title, info.title)); |
Sunny Goyal | c469aad | 2015-10-01 11:24:23 -0700 | [diff] [blame] | 494 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 495 | if (mIcon != null) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 496 | final PreloadIconDrawable preloadDrawable; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 497 | if (mIcon instanceof PreloadIconDrawable) { |
| 498 | preloadDrawable = (PreloadIconDrawable) mIcon; |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 499 | preloadDrawable.setLevel(progressLevel); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 500 | } else { |
Sunny Goyal | 96ac68a | 2017-02-02 16:37:21 -0800 | [diff] [blame] | 501 | preloadDrawable = DrawableFactory.get(getContext()) |
| 502 | .newPendingIcon(info.iconBitmap, getContext()); |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 503 | preloadDrawable.setLevel(progressLevel); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 504 | setIcon(preloadDrawable); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 505 | } |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 506 | return preloadDrawable; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 507 | } |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 508 | } |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 509 | return null; |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 510 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 511 | |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame] | 512 | public void applyBadgeState(ItemInfo itemInfo, boolean animate) { |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 513 | if (mIcon instanceof FastBitmapDrawable) { |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 514 | boolean wasBadged = mBadgeInfo != null; |
Tony Wickham | 2fe09f2 | 2017-04-25 12:46:04 -0700 | [diff] [blame] | 515 | mBadgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo); |
| 516 | boolean isBadged = mBadgeInfo != null; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 517 | float newBadgeScale = isBadged ? 1f : 0; |
Tony Wickham | 2fe09f2 | 2017-04-25 12:46:04 -0700 | [diff] [blame] | 518 | mBadgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 519 | if (wasBadged || isBadged) { |
Tony Wickham | 7092db0 | 2017-06-07 14:32:23 -0700 | [diff] [blame] | 520 | mBadgePalette = IconPalette.getBadgePalette(getResources()); |
| 521 | if (mBadgePalette == null) { |
| 522 | mBadgePalette = ((FastBitmapDrawable) mIcon).getIconPalette(); |
| 523 | } |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 524 | // Animate when a badge is first added or when it is removed. |
| 525 | if (animate && (wasBadged ^ isBadged) && isShown()) { |
| 526 | ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, newBadgeScale).start(); |
| 527 | } else { |
| 528 | mBadgeScale = newBadgeScale; |
| 529 | invalidate(); |
| 530 | } |
| 531 | } |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Tony Wickham | 7092db0 | 2017-06-07 14:32:23 -0700 | [diff] [blame] | 535 | public IconPalette getBadgePalette() { |
| 536 | return mBadgePalette; |
| 537 | } |
| 538 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 539 | /** |
| 540 | * Sets the icon for this view based on the layout direction. |
| 541 | */ |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 542 | private void setIcon(Drawable icon) { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 543 | mIcon = icon; |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 544 | mIcon.setBounds(0, 0, mIconSize, mIconSize); |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 545 | if (mIsIconVisible) { |
| 546 | applyCompoundDrawables(mIcon); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | public void setIconVisible(boolean visible) { |
| 551 | mIsIconVisible = visible; |
| 552 | mDisableRelayout = true; |
| 553 | Drawable icon = mIcon; |
| 554 | if (!visible) { |
| 555 | icon = new ColorDrawable(Color.TRANSPARENT); |
| 556 | icon.setBounds(0, 0, mIconSize, mIconSize); |
| 557 | } |
| 558 | applyCompoundDrawables(icon); |
| 559 | mDisableRelayout = false; |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | protected void applyCompoundDrawables(Drawable icon) { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 563 | if (mLayoutHorizontal) { |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 564 | setCompoundDrawablesRelative(icon, null, null, null); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 565 | } else { |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 566 | setCompoundDrawables(null, icon, null, null); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 567 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 568 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 569 | |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 570 | @Override |
| 571 | public void requestLayout() { |
| 572 | if (!mDisableRelayout) { |
| 573 | super.requestLayout(); |
| 574 | } |
| 575 | } |
| 576 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 577 | /** |
| 578 | * Applies the item info if it is same as what the view is pointing to currently. |
| 579 | */ |
Sunny Goyal | 2d7cca1 | 2017-01-03 16:52:43 -0800 | [diff] [blame] | 580 | @Override |
| 581 | public void reapplyItemInfo(ItemInfoWithIcon info) { |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 582 | if (getTag() == info) { |
| 583 | mIconLoadRequest = null; |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 584 | mDisableRelayout = true; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 585 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 586 | if (info instanceof AppInfo) { |
| 587 | applyFromApplicationInfo((AppInfo) info); |
| 588 | } else if (info instanceof ShortcutInfo) { |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 589 | applyFromShortcutInfo((ShortcutInfo) info); |
Jon Miranda | 529af30 | 2017-02-28 14:18:54 -0800 | [diff] [blame] | 590 | FolderIconPreviewVerifier verifier = |
| 591 | new FolderIconPreviewVerifier(mLauncher.getDeviceProfile().inv); |
| 592 | if (verifier.isItemInPreview(info.rank) && (info.container >= 0)) { |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 593 | View folderIcon = |
| 594 | mLauncher.getWorkspace().getHomescreenIconByItemId(info.container); |
| 595 | if (folderIcon != null) { |
| 596 | folderIcon.invalidate(); |
| 597 | } |
| 598 | } |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 599 | } else if (info instanceof PackageItemInfo) { |
| 600 | applyFromPackageItemInfo((PackageItemInfo) info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 601 | } |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 602 | |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 603 | mDisableRelayout = false; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Verifies that the current icon is high-res otherwise posts a request to load the icon. |
| 609 | */ |
| 610 | public void verifyHighRes() { |
| 611 | if (mIconLoadRequest != null) { |
| 612 | mIconLoadRequest.cancel(); |
| 613 | mIconLoadRequest = null; |
| 614 | } |
Sunny Goyal | 2d7cca1 | 2017-01-03 16:52:43 -0800 | [diff] [blame] | 615 | if (getTag() instanceof ItemInfoWithIcon) { |
| 616 | ItemInfoWithIcon info = (ItemInfoWithIcon) getTag(); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 617 | if (info.usingLowResIcon) { |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 618 | mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache() |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 619 | .updateIconInBackground(BubbleTextView.this, info); |
| 620 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 621 | } |
| 622 | } |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 623 | |
Jon Miranda | 4717011 | 2017-03-03 14:57:14 -0800 | [diff] [blame] | 624 | public int getIconSize() { |
| 625 | return mIconSize; |
| 626 | } |
| 627 | |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 628 | /** |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 629 | * Interface to be implemented by the grand parent to allow click shadow effect. |
| 630 | */ |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 631 | public interface BubbleTextShadowHandler { |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 632 | void setPressedIcon(BubbleTextView icon, Bitmap background); |
| 633 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 634 | } |