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