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