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