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